Last active
August 29, 2015 14:10
-
-
Save WayneCui/0bd81e8bf11d4f86a9fe to your computer and use it in GitHub Desktop.
Parse file uploads data by HTTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rebol [ | |
Title: "Parse file uploads data by HTTP" | |
Author: "Cui Wen" | |
Description: { | |
simulate file-uploading via curl: | |
curl --form upload=@D:/a.txt --form upload=@D:/a.jpg --form press=OK http://localhost:8080 | |
} | |
] | |
server: open tcp://:8080 | |
boundary: "" | |
segment: to-binary reduce [ crlf crlf ] | |
flag: to-binary {Content-Type: multipart/form-data; boundary=} | |
flag2: to-binary {filename="} | |
server/awake: func [ event /local port ][ | |
if event/type = 'accept [ | |
port: first event/port | |
port/awake: func [ event /local data ][ | |
switch event/type [ | |
read [ | |
print [ event/port/data ] | |
data: event/port/data | |
if find data to-binary "Expect: 100-continue" [ | |
write event/port to-binary {HTTP/1.1 100 Continue ^M^/^M^/} | |
] | |
either find data flag [ | |
;print "================Header data" | |
parse data [ | |
thru flag copy boundary to segment (print to-string boundary) | |
] | |
][ | |
;print "================file data" | |
parse data [ | |
any [ | |
thru boundary thru flag2 copy file-name to {"} | |
thru segment copy file-data to boundary ( write to-file file-name file-data) | |
] | |
] | |
] | |
clear event/port/data | |
write event/port to-binary {Some data ^/} | |
] | |
wrote [ | |
print "==============Server send some message!" | |
read event/port | |
] | |
close [ | |
close event/port | |
return true | |
] | |
] | |
false | |
] | |
read port | |
] | |
false | |
] | |
wait [ server 20 ] | |
close server |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment