Re: Sending a File Over HTTP

new topic     » goto parent     » topic index » view thread      » older message » newer message

Thanks for working with me, jim. My comments follow:

jimcbrown said...

I do have some questions regarding your code. I'll comment on them below.

euphoric said...
public function transfer(sequence fPath) 
sequence result = "FAIL" 
object filetxt 
 
	filetxt = read_file( fPath ) 
	if not atom(filetxt) then 
        sequence data, fname 
 
        fname = filename( fPath ) 
        data = { 
            { "name", fname }, 
            { "company", url:encode(COMPANY_NAME) }, 
            { "transfer", "true" }, 

What's all this stuff? company? transfer? Does your real cgi-script require them?

You only need two - "data" and "size".

All that "stuff" is data I need to send to the server side script so I can organize the file I'm sending.

For example, the cgi-script will take the company and create a directory for it on the server to store the data being sent.

jimcbrown said...
euphoric said...
         result = http_post( "http://my.ip.address/files/transfer.esp", { MULTIPART_FORM_DATA, data } ) 
	end if 
 
    if not equal(result[2],"SUCCESS!") then 
        ERR_MESSAGE = result[2] 
    else 
        ERR_MESSAGE = "" 
    end if 
	return equal(result[2],"SUCCESS!") 
end function 

Why are you checking if it equals "SUCCESS!" ?

My cgi-script returns "SUCCESS!" on a successful completion of the requested activity.

jimcbrown said...

Anyways, you will need to set a size parameter.

Even sending the size parameter, my cgi-script reports "Nothing received."

Here's the cgi-script I'm using (a modified version of yours that I'm using just for testing at the moment) called "transfer.esp":

#!c:/euphoria4.0.5/bin/eui.exe 
 
puts(1,"Content-type: text/html\n\n") 
 
without warning 
 
include std/filesys.e 
include std/dll.e 
include std/get.e 
include std/machine.e 
include std/sequence.e 
include std/io.e 
 
constant upload_dir = "licenses/" 
 
object nbytes, variables, notused, qstr 
 
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 = getenv("CONTENT_LENGTH") 
qstr = getenv("QUERY_STRING") 
 
-- testing received values here... 
if atom(qstr) then 
	puts(1,"No query string.") 
else 
	if length(qstr) > 0 then 
		puts(1,"Query string: '" & qstr & "'") 
	else 
		puts(1,"Query string is empty.") 
	end if 
end if 
 
if atom(nbytes) then 
	nbytes = "" 
end if 
 
nbytes = value(nbytes) 
if nbytes[1] = GET_SUCCESS then 
  nbytes = nbytes[2] 
else 
  puts(1,"\n<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 length(text) > 0 then 
	if equal(text[$],"") then text = text[1..$-1] end if 
	variables = text 
 
	variables = variables[2..length(variables)-1] 
	notused = match("filename=", variables[1]) 
	nbytes = filename(variables[1][notused+length("filename=")+1..length(variables[1])-3]) 
	variables = variables[3..$] 
	notused = getenv("REMOTE_ADDR") --remote ip 
 
	if atom(notused) then 
		notused = "" 
	end if 
 
	nbytes = upload_dir&notused&"-"&nbytes --remote ip & filename 
	notused = open(nbytes, "w") 
	if notused = -1 then 
	      printf(1, "\nCouldn't open file %s.", {nbytes}) 
	      abort(1) 
	end if 
	variables = variables[2..$] -- bizarre but ok - have random leftover blank line 
	variables[$] = variables[$][1..length(variables[$])-2] 
	if equal(variables[$],"") then variables = variables[1..$-1] end if 
	for j = 1 to length(variables) do 
	    puts(notused, variables[j]) 
	end for 
	close(notused) 
	puts(1, "SUCCESS!") 
else 
	puts(1,"\nNothing received.") 
end if 

Here's the function to call transfer.esp from the client side:

public function transfer(sequence fPath) 
sequence result = "FAIL" 
object filetxt 
 
	filetxt = read_file( fPath ) 
	if not atom(filetxt) then 
        sequence data, fname 
 
        fname = filename( fPath ) 
        data = { 
            { "name", fname }, 
            { "company", url:encode(COMPANY_NAME) }, 
            { "transfer", "true" }, 
            { "data", filetxt, fname, "text/plain", ENCODE_NONE }, 
            { "size", sprintf("%d",{length(filetxt)}) } 
        } 
     
    puts(1,"\nsending this data:\n") 
    pretty_print(1,data,{3}) 
     
        result = http_post( "http://localhost/transfer.esp", { MULTIPART_FORM_DATA, data } ) 
	end if 
 
puts(1,"\nFrom server:\n") 
pretty_print(1,result,{3}) 
 
    if not equal(result[2],"SUCCESS!") then 
        ERR_MESSAGE = result[2] 
    else 
        ERR_MESSAGE = "" 
    end if 
	return equal(result[2],"SUCCESS!") 
end function 
 

I'm always getting "Query string is empty. Nothing received." getlost

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu