1. Receiving a POSTed file
- Posted by bugmagnet May 06, 2014
- 1566 views
Is there any code out there implementing the receipt of a POSTed file? I can see in the docs the mechanism to issue GET and POST but not to respond on the server's side.
Bugmagnet
2. Re: Receiving a POSTed file
- Posted by jimcbrown (admin) May 06, 2014
- 1538 views
Is there any code out there implementing the receipt of a POSTed file? I can see in the docs the mechanism to issue GET and POST but not to respond on the server's side.
What are you using as the httpd server?
This is how it's done using apache and cgi:
#!/usr/bin/env eui include std/io.e include std/get.e puts(1, "Content-type: text/text\r\n\r\n") object nbytes, data nbytes = getenv("CONTENT_LENGTH") if atom(nbytes) then puts(1, "failure") abort(0) end if nbytes = value(nbytes) nbytes = nbytes[2] if sequence(nbytes) or nbytes < 1 then puts(1, "failed") abort(0) end if data = get_bytes(0,nbytes) integer fn = open("post_test.txt", "w") if fn = -1 then puts(1, "fail") abort(0) end if puts(fn, data) close(fn) puts(1, "success")
3. Re: Receiving a POSTed file
- Posted by bugmagnet May 06, 2014
- 1567 views
What are you using as the httpd server?
Actually, I'm running under IIS, but I'll port that code and see how I go.
Thanks.
bugmagnet
4. Re: Receiving a POSTed file
- Posted by bugmagnet May 06, 2014
- 1483 views
Jim, that works a treat. Thanks.
Next challenge is doing something useful with the header which in the test case is
------WebKitFormBoundarylHdLZsq10IBUKBPg Content-Disposition: form-data; name="file"; filename="Jen.docx" Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
Kind regards,
bugmagnet
5. Re: Receiving a POSTed file
- Posted by bugmagnet May 06, 2014
- 1479 views
include std/regex.e as re include std/pretty.e object r = re:new("(.+?)\r\nContent-Disposition: (.+?); name=\"(.+?)\"; filename=\"([^\"]+?)\"\r\nContent-Type: (.+?)\r\n") object matches = re:all_matches(r , "------WebKitFormBoundarylHdLZsq10IBUKBPg\r\n" & "Content-Disposition: form-data; name=\"file\"; filename=\"Jen.docx\"\r\n" & "Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document\r\n") pretty_print( 1, matches, {2})
Ok, that wasn't too hard. Was a bit taken aback by the absence of some regular expression syntax (like \S) but the above works and gives
{ { "------WebKitFormBoundarylHdLZsq10IBUKBPg\r\nContent-Disposition: form-data; name=\"file\"; filename=\"Jen.docx\"\r\nContent-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document\r\n", "------WebKitFormBoundarylHdLZsq10IBUKBPg", "form-data", "file", "Jen.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" } }
So I can recover the filename and content-type, and then remove the matched header from the data which was what I was after.
Am about to find out how big a file I upload with this.
Thanks again,
bugmagnet
6. Re: Receiving a POSTed file
- Posted by bugmagnet May 06, 2014
- 1576 views
Well, that was interesting ...
From the developer tools in Google Chrome:
... Content-Length:966070568 Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryLr9KPm0b7YDcJSe0 Host:localhost:9999 Origin:http://localhost:9999 Pragma:no-cache Referer:http://localhost:9999/index2.html User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1964.4 Safari/537.36 Request Payload ------WebKitFormBoundaryLr9KPm0b7YDcJSe0 Content-Disposition: form-data; name="file"; filename="Database2.mdb" Content-Type: application/msaccess
IIS responds to this with (in part)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>IIS 7.5 Detailed Error - 404.13 - Not Found</title>
Does that imply a limit to how much an Euphoria app can handle in a single call to get_bytes?
bugmagnet
7. Re: Receiving a POSTed file
- Posted by bugmagnet May 06, 2014
- 1471 views
Okay, it's stopped working completely and I can't figure out why. I get either 404 or 502 errors. Just now I tried sending a 2GB ISO file and crashed Firefox completely (pretty cool, no?)
If anyone's got an idea why this is failing, please post.
include std/io.e include std/get.e include std/regex.e as re include std/pretty.e include std/sequence.e puts(1, "Content-type: text/text\r\n\r\n") object nbytes, data nbytes = getenv("CONTENT_LENGTH") if atom(nbytes) then puts(1, "failure") abort(0) end if nbytes = value(nbytes) nbytes = nbytes[2] if sequence(nbytes) or nbytes < 1 then puts(1, "failed") abort(0) end if data = get_bytes(0,nbytes) object r = re:new("(.+?)\r\nContent-Disposition: (.+?); name=\"(.+?)\"; filename=\"([^\"]+?)\"\r\nContent-Type: (.+?)\r\n") object matches = re:all_matches(r , data) integer fn = open(matches[5], "w") if fn = -1 then puts(1, "fail") abort(0) end if puts(fn, slice(data, length(matches[1])+1)) close(fn) puts(1, "success")
bugmagnet
8. Re: Receiving a POSTed file
- Posted by bugmagnet May 06, 2014
- 1597 views
Weird ... read down a bit further in the response to uploading a 2GB ISO:
<div class="content-container"> <fieldset><legend>Error Summary</legend> <h2>HTTP Error 404.13 - Not Found</h2> <h3>The request filtering module is configured to deny a request that exceeds the request content length.</h3> </fieldset> </div>
bugmagnet
9. Re: Receiving a POSTed file
- Posted by gbonvehi May 07, 2014
- 1489 views
Glad you're sorting this out.
The requests limits (if you've permission on your server) can be modified using a simple web.config. You can find an example here: http://stackoverflow.com/questions/3853767/maximum-request-length-exceeded
Depending on the version of IIS and processes allowed RAM, request timeout, etc you may hit other limits which very large files.
10. Re: Receiving a POSTed file
- Posted by bugmagnet May 07, 2014
- 1451 views
Glad you're sorting this out.
I can see now why you're glad. It's driving me nuts!
The requests limits (if you've permission on your server) can be modified using a simple web.config. ... Depending on the version of IIS and processes allowed RAM, request timeout, etc you may hit other limits which very large files.
I'm hitting all kinds of walls at the moment. Now nothing's working!
bug(ged)magnet
11. Re: Receiving a POSTed file
- Posted by bugmagnet May 07, 2014
- 1474 views
Grrrrr ... even tried running the original script under eui.exe and putting the script into the action of the form, viz
<form id="file_upload_form" method="post" enctype="multipart/form-data" action="/eu-received.ex"> <input name="file" id="file" size="27" type="file" /><br /> <input type="submit" name="action" value="Upload" /><br /> </form>But koi nahin as they say.
bugmagnet
12. Re: Receiving a POSTed file
- Posted by gbonvehi May 07, 2014
- 1421 views
Sorry, I thought you were getting close :)
I'll try to setup a similar setup tomorrow morning to help you test this further, iirc I've never coded file upload in eu4 so that'll be interesting :)