[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: RE : RE: [lwip-users] webserver with method=POST
From: |
Grubb, Jared |
Subject: |
RE: RE : RE: [lwip-users] webserver with method=POST |
Date: |
Tue, 17 Jul 2007 15:04:45 -0700 |
This is some great instructions... need to add this to the docs!
-----Original Message-----
From: address@hidden [mailto:address@hidden On Behalf Of Frédéric BERNON
Sent: Tuesday, July 17, 2007 12:12 PM
To: Mailing list for lwIP users; address@hidden
Subject: RE : RE: [lwip-users] webserver with method=POST
Hi Urs,
I can give you a generic guideline to process "simply" GET & POST on your
embedded web server. What you have to do:
1/ On the accepted socket, read datas in a buffer until "\r\n\r\n" (included).
This part is the "http_headers". It can be useful to add a 0x00 at the end of
the block to use "string" functions from C runtime, or simply do a "printf".
2/ Extract from the first line (of the "http_headers" got in 1/) the method
(should be "POST" or "GET"), and the document (could be "/" for the root). The
method is the first word, the document the next.
3/ If the method (got in 2/) is "GET", copy (or use a pointer) all the datas
from the first line which are after a possible "?". Note it's possible to not
have any "?" in a request. At this point, note you can have between 0 to n
bytes for these datas. I will call this memory block "http_params_datas" in
next points.
4/ If the method (got in 2/) is "POST", you have first to find in
"http_headers" a line started by "Content-Length: ". The line have a form like
"Content-Length: 32\r\n". When you found this line, convert the value of the
line (32 is this sample) in a integer. This is the "post_data_size". One you
got that, read on the socket the next "post_data_size" bytes, and copy them (or
use a pointer) in the memory block named "http_params_datas".
5/ At this point, you should have a memory block named "http_params_datas"
(with its size). It can contain datas from GET parameters, or from POST datas.
Note than GET have a maximum size of 1024 bytes. That's why, most of time, POST
is used to get "big" datas. POST is also useful if you don't want to "show" on
the address bar your parameters. The aim is to have a memory block to process
on next point. It can be useful to add a 0x00 at the end of the block to use
"string" functions from C runtime, or simply do a "printf".
6/ You can now read param by param your "http_params_datas", you will got each
parameters from your "form". They are in the same form in GET or POST
(something like "param1=value1¶m2=value2").
That's all. This "framework" will work, but need some checkings. First is to
not read more bytes than your buffer can contains. Other tips: if you try to do
a UPLOAD, the processing is a little bit different from 6/, since you can to
process a "multipart" document, with "boundary" tags to limit the datas (which
can be binary datas). Look for "RFC1341 - 7.2.1 Multipart: The common syntax"
for this case.
I can propose you two simple functions to "parse" (a big word in this case)
http datas. They are basic tools to help to start, bug mainly used the C
runtime (note they were used on win32, I just change them with lwIP types, so,
it SHOULD work).
err_t http_getheadervalue( char* lpszHeaderName, char* lpszHeaders, char*
lpszBuffer, u32_t dwBufferSize)
{ err_t bResult = ERR_ARG;
char* lpszHeader = NULL;
memset( lpszBuffer, 0, dwBufferSize);
lpszHeader = strstr( lpszHeaders, lpszHeaderName);
if (lpszHeader!=NULL)
{ if (sscanf( lpszHeader+strlen(lpszHeaderName)+1/*:*/+1/*SPACE*/,
"%[^\r\n]", lpszBuffer)>=0)
{ bResult = ERR_OK;
}
}
return bResult;
}
err_t http_getlinevalue( char* lpszValueName, char* lpszLine, char* lpszBuffer,
u32_t dwBufferSize)
{ err_t bResult = ERR_ARG;
char* lpszValue = NULL;
char szValue[256] = "";
memset( lpszBuffer, 0, dwBufferSize);
memset( szValue, 0, sizeof(szValue));
lpszValue = strstr( lpszLine, lpszValueName);
if (lpszValue!=NULL)
{ if (sscanf( lpszValue+strlen(lpszValueName)+1/*=*/, "%[^ ;&\r\n]",
szValue)>=0)
{ /* Note you have to process special escape sequence, or special chars:
by example, any "+" have to be replace by a " " */
/* Can you find in your datas some sequence like "%20". You have to
replace them by the a matching ASCII value. For "%20", its 0x20, so, a "space"
/* etc...*/
bResult = ERR_OK
}
}
return bResult;
}
Good luke...
====================================
Frédéric BERNON
HYMATOM SA
Chef de projet informatique
Microsoft Certified Professional
Tél. : +33 (0)4-67-87-61-10
Fax. : +33 (0)4-67-70-85-44
Email : address@hidden
Web Site : http://www.hymatom.fr
====================================
P Avant d'imprimer, penser à l'environnement
-----Message d'origine-----
De : address@hidden [mailto:address@hidden De la part de Grubb, Jared Envoyé :
mardi 17 juillet 2007 18:01 À : address@hidden; Mailing list for lwIP users
Objet : RE: RE: [lwip-users] webserver with method=POST
The version in lwIP is *incredibly* simple. It serves up the same web page to
every incoming HTTP GET command - and doesn't even fully parse the input. A
quick Google search pulled up this website, which can explain more about the
HTTP protocol and how you can parse the things sent to the server:
http://www.jmarshall.com/easy/http/#postmethod
But as for using lwIP to do the network transfers, the online example can get
you started.
Jared
From: address@hidden [mailto:address@hidden On Behalf Of Urs Gerber
Sent: Tuesday, July 17, 2007 8:51 AM
To: Grubb, Jared; 'Mailing list for lwIP users'
Subject: AW: RE: [lwip-users] webserver with method=POST
Hi Jared
Thanks for short answer.
But all examples work with method "GET" and these is also no problem.
I try with method "POST" and do not find the entered datas!?
Mit freundlichen Grüssen
Urs Gerber
-----Ursprüngliche Nachricht-----
Von: Grubb, Jared [mailto:address@hidden
Gesendet: Dienstag, 17. Juli 2007 17:29
An: address@hidden; Mailing list for lwIP users
Betreff: [?? Probable Spam] RE: [lwip-users] webserver with method=POST There
is a real basic web server example in the CVS right now, under
/contrib/apps/httpserver (this is in the "contrib" module). You can browse the
online CVS if you need to.
Second, I would read the original document that Adam Dunkels wrote about lwIP.
It's a few years old now, and we're working on updated documentation, but with
the example above (which is current) and the old PDF, you should be able to get
yourself started! The PDF is here:
http://www.sics.se/~adam/lwip/doc/lwip.pdf
Jared
From: address@hidden [mailto:address@hidden On Behalf Of Urs Gerber
Sent: Tuesday, July 17, 2007 7:25 AM
To: address@hidden
Subject: [lwip-users] webserver with method=POST
I'm using lwip 1.2 and would like to make a small webserver application with a
password inquiry (method POST).
Question:
How can I read the entered data?
Does someone have an example?
Mit freundlichen Grüssen
Urs Gerber