Skip to content

Commit

Permalink
Add HTTP POST capability to apps/webbrowser
Browse files Browse the repository at this point in the history
  • Loading branch information
maniacbug committed Aug 17, 2011
1 parent 613783c commit 34957ec
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
9 changes: 9 additions & 0 deletions apps/webbrowser/http-strings.c
Expand Up @@ -13,6 +13,9 @@ const char http_302[5] =
const char http_get[5] = const char http_get[5] =
/* "GET " */ /* "GET " */
{0x47, 0x45, 0x54, 0x20, }; {0x47, 0x45, 0x54, 0x20, };
const char http_post[6] =
/* "POST " */
{0x50, 0x4f, 0x53, 0x54, 0x20, };
const char http_10[9] = const char http_10[9] =
/* "HTTP/1.0" */ /* "HTTP/1.0" */
{0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, }; {0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, };
Expand Down Expand Up @@ -73,6 +76,12 @@ const char http_content_type_jpg [29] =
const char http_content_type_binary[43] = const char http_content_type_binary[43] =
/* "Content-type: application/octet-stream\r\n\r\n" */ /* "Content-type: application/octet-stream\r\n\r\n" */
{0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x6f, 0x63, 0x74, 0x65, 0x74, 0x2d, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0xd, 0xa, 0xd, 0xa, }; {0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x6f, 0x63, 0x74, 0x65, 0x74, 0x2d, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0xd, 0xa, 0xd, 0xa, };
const char http_content_type_form[50] =
/* "Content-Type: application/x-www-form-urlencoded\r\n" */
{0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x78, 0x2d, 0x77, 0x77, 0x77, 0x2d, 0x66, 0x6f, 0x72, 0x6d, 0x2d, 0x75, 0x72, 0x6c, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x65, 0x64, 0xd, 0xa,};
const char http_content_length[17] =
/* "Content-Length: " */
{0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x20, };
const char http_html[6] = const char http_html[6] =
/* ".html" */ /* ".html" */
{0x2e, 0x68, 0x74, 0x6d, 0x6c, }; {0x2e, 0x68, 0x74, 0x6d, 0x6c, };
Expand Down
3 changes: 3 additions & 0 deletions apps/webbrowser/http-strings.h
Expand Up @@ -3,6 +3,7 @@ extern const char http_200[5];
extern const char http_301[5]; extern const char http_301[5];
extern const char http_302[5]; extern const char http_302[5];
extern const char http_get[5]; extern const char http_get[5];
extern const char http_post[6];
extern const char http_10[9]; extern const char http_10[9];
extern const char http_11[9]; extern const char http_11[9];
extern const char http_content_type[15]; extern const char http_content_type[15];
Expand All @@ -23,6 +24,8 @@ extern const char http_content_type_png [28];
extern const char http_content_type_gif [28]; extern const char http_content_type_gif [28];
extern const char http_content_type_jpg [29]; extern const char http_content_type_jpg [29];
extern const char http_content_type_binary[43]; extern const char http_content_type_binary[43];
extern const char http_content_type_form[50];
extern const char http_content_length[17];
extern const char http_html[6]; extern const char http_html[6];
extern const char http_htm[5]; extern const char http_htm[5];
extern const char http_css[5]; extern const char http_css[5];
Expand Down
54 changes: 50 additions & 4 deletions apps/webbrowser/webclient.c
Expand Up @@ -34,6 +34,7 @@
*/ */


#include <string.h> #include <string.h>
#include <stdio.h>


#include "contiki-net.h" #include "contiki-net.h"
#include "www.h" #include "www.h"
Expand Down Expand Up @@ -65,6 +66,8 @@ struct webclient_state {
u16_t port; u16_t port;
char host[40]; char host[40];
char file[WWW_CONF_MAX_URLLEN]; char file[WWW_CONF_MAX_URLLEN];
char formvars[100];
char content_length[5];
u16_t getrequestptr; u16_t getrequestptr;
u16_t getrequestleft; u16_t getrequestleft;


Expand Down Expand Up @@ -110,9 +113,13 @@ webclient_init(void)
static void static void
init_connection(void) init_connection(void)
{ {
/* If formvars is set, this is a post */
int method = s.formvars[0] ? 1 : 0;
size_t http_method_size = method ? sizeof(http_post) : sizeof(http_get);

s.state = WEBCLIENT_STATE_STATUSLINE; s.state = WEBCLIENT_STATE_STATUSLINE;


s.getrequestleft = sizeof(http_get) - 1 + 1 + s.getrequestleft = http_method_size - 1 + 1 +
sizeof(http_10) - 1 + sizeof(http_10) - 1 +
sizeof(http_crnl) - 1 + sizeof(http_crnl) - 1 +
sizeof(http_host) - 1 + sizeof(http_host) - 1 +
Expand All @@ -121,7 +128,16 @@ init_connection(void)
(u16_t)strlen(s.file) + (u16_t)strlen(s.host); (u16_t)strlen(s.file) + (u16_t)strlen(s.host);
s.getrequestptr = 0; s.getrequestptr = 0;


if (method) {
snprintf(s.content_length,sizeof(s.content_length),"%i",strlen(s.formvars));
s.getrequestleft +=
sizeof(http_content_length) + (u16_t)strlen(s.content_length) -1 +
sizeof(http_crnl) - 1 +
sizeof(http_content_type_form) - 1 +
(u16_t)strlen(s.formvars) ;
}
s.httpheaderlineptr = 0; s.httpheaderlineptr = 0;

} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void void
Expand All @@ -130,8 +146,8 @@ webclient_close(void)
s.state = WEBCLIENT_STATE_CLOSE; s.state = WEBCLIENT_STATE_CLOSE;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
unsigned char static unsigned char
webclient_get(const char *host, u16_t port, const char *file) webclient_common(const char *host, u16_t port, const char *file)
{ {
uip_ipaddr_t addr; uip_ipaddr_t addr;
struct uip_conn *conn; struct uip_conn *conn;
Expand Down Expand Up @@ -165,6 +181,20 @@ webclient_get(const char *host, u16_t port, const char *file)
return 1; return 1;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
unsigned char
webclient_get(const char *host, u16_t port, const char *file)
{
s.formvars[0] = 0;
return webclient_common(host,port,file);
}
/*-----------------------------------------------------------------------------------*/
unsigned char
webclient_post(const char *host, u16_t port, const char *file, const char *formvars)
{
strncpy(s.formvars, formvars, sizeof(s.formvars));
return webclient_common(host,port,file);
}
/*-----------------------------------------------------------------------------------*/
/* Copy data into a "window", specified by the windowstart and /* Copy data into a "window", specified by the windowstart and
windowend variables. Only data that fits within the window is windowend variables. Only data that fits within the window is
copied. This function is used to copy data into the uIP buffer, which copied. This function is used to copy data into the uIP buffer, which
Expand Down Expand Up @@ -215,14 +245,19 @@ senddata(void)
u16_t len; u16_t len;
int curptr; int curptr;


/* If formvars is set, this is a post */
int method = s.formvars[0] ? 1 : 0;
size_t http_method_size = method ? sizeof(http_post) : sizeof(http_get);
const char* http_method = method ? http_post : http_get;

if(s.getrequestleft > 0) { if(s.getrequestleft > 0) {


windowstart = s.getrequestptr; windowstart = s.getrequestptr;
curptr = 0; curptr = 0;
windowend = windowstart + uip_mss(); windowend = windowstart + uip_mss();
windowptr = (char *)uip_appdata - windowstart; windowptr = (char *)uip_appdata - windowstart;


curptr = window_copy(curptr, http_get, sizeof(http_get) - 1); curptr = window_copy(curptr, http_method, http_method_size - 1);
curptr = window_copy(curptr, s.file, (unsigned char)strlen(s.file)); curptr = window_copy(curptr, s.file, (unsigned char)strlen(s.file));
curptr = window_copy(curptr, " ", 1); curptr = window_copy(curptr, " ", 1);
curptr = window_copy(curptr, http_10, sizeof(http_10) - 1); curptr = window_copy(curptr, http_10, sizeof(http_10) - 1);
Expand All @@ -232,9 +267,20 @@ senddata(void)
curptr = window_copy(curptr, http_host, sizeof(http_host) - 1); curptr = window_copy(curptr, http_host, sizeof(http_host) - 1);
curptr = window_copy(curptr, s.host, (unsigned char)strlen(s.host)); curptr = window_copy(curptr, s.host, (unsigned char)strlen(s.host));
curptr = window_copy(curptr, http_crnl, sizeof(http_crnl) - 1); curptr = window_copy(curptr, http_crnl, sizeof(http_crnl) - 1);

if (method) {
curptr = window_copy(curptr, http_content_length, sizeof(http_content_length) - 1);
curptr = window_copy(curptr, s.content_length, (unsigned char)strlen(s.content_length));
curptr = window_copy(curptr, http_crnl, sizeof(http_crnl) - 1);
curptr = window_copy(curptr, http_content_type_form, sizeof(http_content_type_form) - 1);
}


curptr = window_copy(curptr, http_user_agent_fields, curptr = window_copy(curptr, http_user_agent_fields,
(unsigned char)strlen(http_user_agent_fields)); (unsigned char)strlen(http_user_agent_fields));

if (method) {
curptr = window_copy(curptr, s.formvars, (unsigned char)strlen(s.formvars));
}


len = s.getrequestleft > uip_mss()? len = s.getrequestleft > uip_mss()?
uip_mss(): uip_mss():
Expand Down
1 change: 1 addition & 0 deletions apps/webbrowser/webclient.h
Expand Up @@ -53,6 +53,7 @@ void webclient_closed(void);
/* Functions. */ /* Functions. */
void webclient_init(void); void webclient_init(void);
unsigned char webclient_get(const char *host, u16_t port, const char *file); unsigned char webclient_get(const char *host, u16_t port, const char *file);
unsigned char webclient_post(const char *host, u16_t port, const char *file, const char *formvars);
void webclient_close(void); void webclient_close(void);


void webclient_appcall(void *state); void webclient_appcall(void *state);
Expand Down

0 comments on commit 34957ec

Please sign in to comment.