News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Im stuck for years

Started by Farabi, January 28, 2010, 11:24:08 AM

Previous topic - Next topic

oex

Hey Farabi,

This is very simple I promise maybe it just sounds hard.... Assuming the link to the server code in the previous post works (I haven't tested i'm sorry) All you have to do is create a html form <form action="http://127.0.0.1:6677"> (:6677 being the port that the code is set to listen on and 127.0.0.1 being 'localhost' (the address of your machine)). Post the data in your browser and debug directly after the recv function in the server code to see *exactly* what the browser is posting to the server.

Once you have done this you can either use curl etc or modify the 'client' code in that same forum post download to replicate the bowser posting the html form using the data you know it sent.... You might have to change 3 bits.... Content-Length Header, the server/port you are posting to and the file you are posting.

In some cases it is worth looking at what's gong on at a lower level and this is one of them.... It's not as complicated as it looks and understanding sockets, recv, send etc as shown in the link I posted will make the functions you are using much easier to understand.

http://www.masm32.com/board/index.php?topic=3109.msg24059#msg24059
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Farabi

Quote from: oex on February 02, 2010, 10:55:55 AM
Hey Farabi,

This is very simple I promise maybe it just sounds hard.... Assuming the link to the server code in the previous post works (I haven't tested i'm sorry) All you have to do is create a html form <form action="http://127.0.0.1:6677"> (:6677 being the port that the code is set to listen on and 127.0.0.1 being 'localhost' (the address of your machine)). Post the data in your browser and debug directly after the recv function in the server code to see *exactly* what the browser is posting to the server.

Once you have done this you can either use curl etc or modify the 'client' code in that same forum post download to replicate the bowser posting the html form using the data you know it sent.... You might have to change 3 bits.... Content-Length Header, the server/port you are posting to and the file you are posting.

In some cases it is worth looking at what's gong on at a lower level and this is one of them.... It's not as complicated as it looks and understanding sockets, recv, send etc as shown in the link I posted will make the functions you are using much easier to understand.

http://www.masm32.com/board/index.php?topic=3109.msg24059#msg24059

I did check what is posted by the browser, it had something called boundary. I reconstruct the header and did the same thing but got no luck. And there is no example for posting to a php page, I think I overlooked something.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

oex

A boundary is a unique identifier if I remember correctly to show where the parts of a *multi-type* form start and end.... The most likely issue is that you posted slightly wrong data.... try posting the exact same data first as your browser then start changing things
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Farabi

Quote from: oex on February 03, 2010, 01:04:12 AM
A boundary is a unique identifier if I remember correctly to show where the parts of a *multi-type* form start and end.... The most likely issue is that you posted slightly wrong data.... try posting the exact same data first as your browser then start changing things

Which to start?
I sent

Content-Type: application/x-www-form-urlencoded
Content-Disposition: form-data; name="uploadedfile"; filename="gun.jpg"

Thats all I sent.

I set up an apache server on my laptop. Do you know how to reconfigure it so I can see the apache response? Or maybe its memory. I'd like to know why the server reject my data.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

oex

There are many versions of apache server I haven't done an install snce 1.3.xx? There are however some good tutorials on the web for that.

With regard to the upload issue:
Content-Type: must be multipart/form-data, this basically means you are sending a form *plus* files.... the form and each individual file is split by the unique boundary.

There is a pretty good vb6.0 example here: http://www.vbforums.com/showthread.php?t=337424
Another delphi one here: http://delphi.about.com/od/delphitips2007/qt/httppost_delphi.htm

Also another intro here: http://www.15seconds.com/Issue/001003.htm

I'm not sure which side client or server your app is failing....

You want to test the server side with a html form in a browser.... if this fails it probably means that you dont have permissions on the server (something to do with move_uploaded_file function most likely....

If it's the client side the tutorials above should help diagnose and fix the problem....

The example link I posted was a simple server so you could grab the data being sent via a browser and just repost that on, not a http server that would respond
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Farabi

Man, this is very difficult and I got no clue.
I tested using browser to upload to my local host and it works, but when I did that it does not works.


I made this function

Quote
fInet struct
   sock dword 0
   lpszurl dword 0
fInet ends

fRequest struct
   nLenght dword 0
   lpData   dword 0
fRequest ends

.code

fConnectToUrl proc uses esi edi lps:dword,hWnd:dword,lpszHostName:dword,nPort:dword
   LOCAL a:sockaddr_in   
   
   assume esi:ptr fInet
   mov esi,lps
   invoke socket,AF_INET,SOCK_STREAM,0     ; Create a stream socket for internet use
   .if eax!=INVALID_SOCKET
      
       mov [esi].sock,eax
      invoke WSAAsyncSelect,[esi].sock,hWnd,WM_FSOCKET,FD_CONNECT+FD_READ+FD_CLOSE+FD_WRITE+FD_ACCEPT
   .else
      invoke MessageBox,hWnd,CADD("Internet connection initialization error"),CADD("Unknown cause"),MB_OK
   .endif
   
   push lpszHostName
   pop [esi].lpszurl
   
   mov a.sin_family, AF_INET
   invoke htons, nPort
   mov a.sin_port,ax
   invoke gethostbyname, lpszHostName
   .if eax==0
      invoke PERR
      ret
   .endif
   mov eax,[eax+12]
   
   mov eax,[eax]                      ; copy the pointer to the actual IP address into eax
   mov eax,[eax]                      ; copy IP address into eax
   mov a.sin_addr,eax
   
   invoke connect,[esi].sock,addr a,sizeof a
   .if eax==SOCKET_ERROR
      invoke WSAGetLastError 
      ;invoke PERR
   .endif
   assume esi:nothing
   
   ret
fConnectToUrl endp

fSockAddHeader proc uses esi edi lps:dword, lptz:dword,nSize:dword
   LOCAL nReq:dword
   LOCAL nSizeT:dword
   
   mov esi,lps
   
   mov ecx,nSize
   
   .if ecx==-1
      invoke lstrlen,lptz
      push eax
      pop nSizeT
   .else
      push nSize
      pop nSizeT
   .endif
   
   .if [esi].fRequest.lpData==0
      invoke mAlloc,nSizeT
      mov [esi].fRequest.lpData,eax
      push nSizeT
      pop [esi].fRequest.nLenght
      invoke MemCopy,lptz,[esi].fRequest.lpData,[esi].fRequest.nLenght
      ret
   .endif
   
   mov ecx,[esi].fRequest.nLenght
   add ecx,nSizeT
   invoke mAlloc,ecx
   mov nReq,eax
   
   invoke MemCopy,[esi].fRequest.lpData,nReq,[esi].fRequest.nLenght
   mov eax,nReq
   add eax,[esi].fRequest.nLenght
   invoke MemCopy,lptz,eax,nSizeT
   
   invoke GlobalFree,[esi].fRequest.lpData
   push nReq
   pop [esi].fRequest.lpData
   mov eax,[esi].fRequest.nLenght
   add eax,nSizeT
   mov [esi].fRequest.nLenght,eax
   
   xor eax,eax
   
   ret
fSockAddHeader endp

fSockUpload proc uses esi edi lps:dword,lpszObject:dword,lptzFileName:dword
   LOCAL hReq:dword
   LOCAL t:fRequest
   LOCAL fl,filesz:dword
   LOCAL buff[256]:dword
   
   push 0
   pop t.nLenght
   push 0
   pop t.lpData
   
   mov hReq,0
   mov esi,lps
   
   invoke filesize,lptzFileName
   .if eax==-1
      jmp fail
   .endif
   mov filesz,eax
   
   invoke mAlloc,filesz
   mov fl,eax
   
   invoke read_disk_file,lptzFileName,addr fl,addr filesz
   
   invoke fSockAddHeader,addr t,CADD("POST "),-1
   invoke fSockAddHeader,addr t,[esi].fInet.lpszurl,-1
   invoke fSockAddHeader,addr t,CADD("/"),-1
   invoke fSockAddHeader,addr t,lpszObject,-1
   invoke fSockAddHeader,addr t,CADD(" HTTP/1.1"),-1
   invoke fSockAddHeader,addr t,CADD(0dh),1
   
   invoke fSockAddHeader,addr t,CADD("Host: "),-1
   invoke fSockAddHeader,addr t,[esi].fInet.lpszurl,-1
   invoke fSockAddHeader,addr t,CADD(0dh),1
   
   invoke fSockAddHeader,addr t,CADD("User-Agent: Mozilla/5.0"),-1
   invoke fSockAddHeader,addr t,CADD(0dh),1
   
   invoke fSockAddHeader,addr t,CADD("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"),-1
   invoke fSockAddHeader,addr t,CADD(0dh),1
   
   invoke fSockAddHeader,addr t,CADD("Content-Type: multipart/form-data; boundary=-----1234"),-1
   invoke fSockAddHeader,addr t,CADD(0dh),1
   
   invoke fSockAddHeader,addr t,CADD("Content-Length: "),-1
   invoke dw2a,filesz,addr buff
   invoke fSockAddHeader,addr t,addr buff,-1
   invoke fSockAddHeader,addr t,CADD(0dh),1
   
   
   invoke fSockAddHeader,addr t,CADD("-----1234"),-1
   invoke fSockAddHeader,addr t,CADD(0dh),1
   
   invoke fSockAddHeader,addr t,CADD('Content-Disposition: form-data; name="uploadedfile"; filename="test.inc"'),-1
   invoke fSockAddHeader,addr t,CADD(0dh),1
   
   invoke fSockAddHeader,addr t,CADD('Content-Type: application/octet-stream'),-1
   invoke fSockAddHeader,addr t,CADD(0dh),1
   invoke fSockAddHeader,addr t,CADD(0dh),1
   
   
   invoke fSockAddHeader,addr t,fl,filesz
   invoke fSockAddHeader,addr t,CADD(0dh),1
   
   invoke fSockAddHeader,addr t,CADD("-----1234"),-1
   
   
   invoke send,[esi].fInet.sock,t.lpData,t.nLenght,0
   
   invoke GlobalFree,t.lpData
   
   fail:
   
   ret
fSockUpload endp

Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

oex

If you post the dta through the browser to the server here:

http://www.masm32.com/board/index.php?topic=3109.msg24059#msg24059

and then post the data through your application to the link above also you will be able to see the difference in the data and see what's going wrong
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Farabi

Quote from: oex on February 04, 2010, 10:42:17 AM
If you post the dta through the browser to the server here:

http://www.masm32.com/board/index.php?topic=3109.msg24059#msg24059

and then post the data through your application to the link above also you will be able to see the difference in the data and see what's going wrong

The data I sent is the same, but the server is not responding. But if I access the server trough my browser it responding, dont know what is missing.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

oex

hmm, maybe your firewall is blocking your application and not your browser.... if the data matches it should work.... Maybe also your application could be posting to wrong IP?

PS should you get it working and while I think about it be aware of server security when you get it working ie file permissions
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Farabi

Quote from: oex on February 08, 2010, 11:42:49 AM
hmm, maybe your firewall is blocking your application and not your browser.... if the data matches it should work.... Maybe also your application could be posting to wrong IP?

PS should you get it working and while I think about it be aware of server security when you get it working ie file permissions

I tried 127.0.0.1 and also localhost, if I post to a wrong IP there would be a error message warn me if the socket is not responding. I also turn off firewall but still not responding. The only respond from the apache is when I sent header more than 8 kb, so the problem is on what I post. I peek the data using fidler, and it used CRLF for the new line, I used it too but still not work. This is harder than I tought.

Ever you tried it before?
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

oex

Yeah it was fiddly when I did it but as long as the data exactly matches what the browser posts you should have no problems.... Once I got the data to match it was quite easy to modify from there.... Unfortunately I no longer have the code :(
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Farabi

I uploaded the source.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

oex

I dont want to plagerise someone elses work :lol but I would follow the following steps:

1) Post a browser form to the server example in that forum link
2) Save the data received by the server to a file

3) Write something like the client network example in that same forum link to get file contents and send to the actual HTTP server

This should work if you are sending the exact same data.... if it doesn't keep checking something is being missed

I did take a look at your code and though I havent used curl it seems like you have much more functions than you need, I wrote file upload before in PHP with sockets which is about the same as ASM sockets code.... I think virtually all the code you need is in the forum example I posted.... If problems persist try:

1) Most basic Request no GET/POST
2) GET Request ie request x.php?id=Test Then <?php echo $_GET['id'] ?>
2) POST Request ie request x.php with contents id=Test Then <?php echo $_POST['id'] ?>
4) Finally try attaching files

You are most likely trying to do too many different parts at once.... Write it at the socket level dont be put off by people telling you it is harder, this way you know exactly what is output.
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Farabi

Can I just upload my own app server (The exe)? I think it is easier to make my own server.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

oex

Probably not easier but probably just as easy but you would have to watch out for any security holes you might leave in your server, so long as your client app sends the exact same data to the server as the browser there can be no problems, if you sniff results with a local server to test first you will know. Both headers and data must be exactly the same and correct so when you change file you will need to change headers like Content-Length
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv