http request doesn't receive the whole information - C - c

After connecting to a server, I need to use the GET command to get all the information from it. I am, for some reason, not allowed to put \r\n after my get command, so the command I'm sending to the server is something like GET http://somethingrandom.com HTTP/1.0.
After sending the request to the address, I do not receive the same output that a normal GET from a terminal would:
To be more specific, I receive the following information:
The http status
Some odd data:
Date
Server
Last-Modified
ETag
Accept-Ranges
Content-Length
Vary
Connection
Content-Type
I think that the role of \r\n is to exactly ignore that information. (In this example the extra <head> tag information). If it has something to do with my code(most probably) I'll provide it afterwards.

The first command you use on the terminal is actually the program GET which does a proper HTTP request using the Perl LWP library and gives you the response back. The HTTP/1.0 at the end of the command you gave is actually ignored because only the URL is expected. Thus GET is similar to curl or wget. You can even call GET --help for the exact usage.
The thing you are trying do in C is to deal with HTTP without any library. In this case you have to properly read and parse the response, i.e. exactly the thing which GET, curl or wget commands do for you if you use these commands. To do this properly you need to understand the HTTP protocol first. While HTTP/1.0 is not the latest standard I recommend to study this first because it is the simplest one and it is still accepted by web servers. See RFC 1945 for the standard.
From reading the standard you will see, that there is a HTTP response header and body, delimited by \r\n. Thus, you need to take the information from the header in order to interpret the body properly. In the simplest case you can just strip the header.

Related

HTTP keep connection alive without blocking on socket receive?

I am using the below code to make a HTTPS GET request.
I'd like to keep the connection alive to make multiple requests without having to connect each time, so I set "Connection: Keep-Alive\r\n". However, once I did this, the behaviour changed and the code now blocks on BIO_read() (equivalent to ::recv()). Now I cannot return to process the received data.
How can I only connect once, but not block on BIO_read()/::recv()?
You have to pay attention to what you read. Parse the response headers. There would be wither a "Content-Length: xxx", or "Transfer-Encoding: chunked". That woukd give you an information on where the body ends.
HTTP/2 uses, according to the specification, persistent connections. So it should work to replace HTTP 1.1 with HTTP/2. Note, that you then have to remove the Connection: Keep-Alive line, as it is prohibited in HTTP/2. It was just a hint anyway, and didn't guarantee a persistent connection (see MDN Web Docs).
Edit: Turns out, the HTTP/2 support of websites is less than 50% now, so my answer can't be a general solution in any way. So, uhm, take it as a FYI.

How GET process works in http server of MHC(MPLABĀ® Harmony Configurator)

I've tried to make http server with FatFS file system referring to web_server_sdcard_fatfs(https://github.com/GravitasCapstone/pic32-ethernet-sd/tree/master/web_server_sdcard_fatfs/firmware/src).
The problem is that i can't understand the process from receive request to send response.
The sample code doesn't contain file sending units. Then i guess how GET process works in http server. Figure shows the process that I guess.
Firstly, Web browser send GET request with url including valiables (url parameters).
Secondly, The request is reached the web server and the url parameters are stored in the connection data buffer.
Thirdly, File that linked to http requested file (index.html in this case) is automatically searched.
Fourth, The searched directly for these files is mounted by SYS_FS_Mount() function written in app.c.
Fifth, Response message consisted of http header and html body is automatically generated and send,and if the dynamic variables (inside a pair of tilde (~) ) are exists in html file, TCPIP_HTTP_Print fuunctions are automatically called and executed to replace the valiables.
Sixth, TCPIP_HTTP_GetExecute function is automatically called when received GET request.
The role of this function is not send requested file (index.html is automatically send by step 3,4 and 5 in this case).This function is implemented for analyze valiables and control output by the following the valiables.
Could someone please check my guess?
Thank you in advance.

How to send file contents in GET request

My robot has a web-based Lua IDE, and now I send the script contents with a GET request, Base64 Encoded. But for some reason, my server sometimes only decodes a part of the file, and sometimes it works just fine with a way longer file. I've written the server myself in C++ (it's on GitHub
), and it uses libmicrohttpd
Is there a more reliable way of sending files through a HTTP request than the method I'm using right now?

JavaMail correct incoming

I'm receiving inbox via pop3 using JavaMail (1.5.0). One of the servers sends the wrong-formated content-type string. There is encoding added after boundaries. It looks like:
Content-Type: multipart/mixed;
boundary="=_NextPart_2rfkindysadvnqw3nerasdf";koi8-r
It causes an error on parsing:
javax.mail.internet.ParseException: Expected '=', got "null" at
javax.mail.internet.ParameterList.<init>(ParameterList.java:250) at
javax.mail.internet.ContentType.<init>(ContentType.java:114) at
javax.mail.internet.MimeMultipart.parse(MimeMultipart.java:580) at
javax.mail.internet.MimeMultipart.getCount(MimeMultipart.java:325)
Is there any possible way to correct mail stream before actual parsing?
I've tried to compile sourses of the library to extend the functionality but this is not as easy as it should be (not sure where to settle bugfix).
See the description of the mail.mime.contenttypehandler property in the javadocs for the javax.mail.internet package. That allows you to write a class that cleans up the Content-Type value before JavaMail uses it.
And of course you should report this bogus header to either the server vendor or more likely the vendor of the mailer that created the message.

Not able to send request for HTTPS post server using CURL

Iam writing a C program to interact with HTTPs server. Server is expecting the data without
any assignments(Ex: normally a request can be "https://xz.aspx?name=google" where as is it
possible to send the name "https://xz.aspx?google"). Currently server is getting an entry
log for my request but not able to fetch request data.
1.Is it possible to send a value with out assignment?
2.Will .net look for default assignments?
3.Is there anything else to probe?
The data you're sending is just whatever you put in the query part of the request-uri in the HTTP request.
That data can be almost anything you like (as long as it is using letters that are valid according to RFC2616). The "assignments" concept is not something HTTP knows or uses, it is just a common way for clients and servers to deal with the data.
so... Yes, you can send a value "without assignment" with curl. Weather the receiver will like it or understand it is a completely different matter.

Resources