Re: Sending a File Over HTTP
- Posted by jimcbrown (admin) Aug 16, 2013
- 1675 views
euphoric said...
jimcbrown said...
euphoric said...
OK, HTTP POST experts... What does a server-side program look like when receiving a MULTIPART HTTP POST request?
You mean a cgi script running under Apache 2.2, or a full HTTP server written in Euphoria?
I just mean a Euphoria CGI script running under Apache!
Let me add that I was using GET and parsing it just fine with the getenv("QUERY_STRING"). However, QUERY_STRING is empty and STDIN is -1 when trying to use POST.
Thanks!
I have a very old one, written for Euphoria 2.3, but it should still work with 4.0
#!/usr/bin/env exu puts(1,"Content-type: text/html\n\n") without warning include file.e include dll.e include get.e include machine.e include misc.e function get_env(sequence s) return getenv(s) end function global function getenv(sequence x) object s s = get_env(x) if atom(s) then s = "" end if return s end function constant upload_dir = "../upload/" object nbytes, variables, notused function lowerit(object x) -- convert atom or sequence to lower case return x + (x >= 'A' and x <= 'Z') * 32 end function function get_base_file_name(sequence sf) integer i sf = reverse(sf) i = find('/', sf) if i then sf = sf[1..i-1] end if i = find('\\', sf) if i then sf = sf[1..i-1] end if i = find(':', sf) if i then sf = sf[1..i-1] end if while find(' ', sf) do sf[find(' ', sf)] = '_' end while sf = reverse(sf) return sf end function constant alphanum = "abcdefghijklmnopqrstuvwxyz" & "ABCDEFGHIJKLMNOPQRSTUVWXYZ" & "0123456789." & "\\/:" --a hack, not really alphanum, but it makes things easier -- not an issue as files can never have these anyways function cleaner(sequence s) integer k k = 1 while k <= length(s) do if not find(s[k], alphanum) then s = s[1..k-1] & "%" & sprintf("%x", s[k]) & s[k+1..length(s)] k += 2 --we can skip the next 2 chars, they are part of --the curent char end if k += 1 end while return s end function nbytes = get_env("CONTENT_LENGTH") nbytes = value(nbytes) if nbytes[1] = GET_SUCCESS then nbytes = nbytes[2] else puts(1,"<html><body>Internal Server Error1<p>" & "Please try again at a later time.</body></html>") abort(1) end if variables = get_bytes(0,nbytes) object text, line, char, last text = {} line = {} last = -1 for j = 1 to length(variables) do char = variables[j] line &= char if char = '\n' and last = '\r' then text &= {line} line = {} end if last = char end for if equal(text[length(text)],"") then text = text[1..length(text)-1] end if variables = text variables = variables[2..length(variables)-1] notused = match("filename=", variables[1]) nbytes = get_base_file_name(variables[1][notused+length("filename=")+1..length(variables[1])-3]) variables = variables[3..length(variables)] notused = getenv("REMOTE_ADDR") --remote ip nbytes = upload_dir¬used&"-"&nbytes --remote ip & filename notused = open(nbytes, "w") if notused = -1 then printf(1, "<html><body>Couldn't open file %s.</body></html>", {nbytes}) abort(1) end if variables = variables[2..length(variables)] -- bizarre but ok - have random leftover blank line variables[length(variables)] = variables[length(variables)][1..length(variables[length(variables)])-2] if equal(variables[length(variables)],"") then variables = variables[1..length(variables)-1] end if for j = 1 to length(variables) do puts(notused, variables[j]) end for close(notused) printf(1, "<html><body>Fine. File is located <a href=\"%s\">here</a>. Good day.</body></html>", {cleaner(nbytes)})