Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add HTTP POST capability to apps/webbrowser

  • Loading branch information...
commit 34957ec7ae8e915c4318e9d55d2c50f03306603a 1 parent 613783c
@maniacbug authored
View
9 apps/webbrowser/http-strings.c
@@ -13,6 +13,9 @@ const char http_302[5] =
const char http_get[5] =
/* "GET " */
{0x47, 0x45, 0x54, 0x20, };
+const char http_post[6] =
+/* "POST " */
+{0x50, 0x4f, 0x53, 0x54, 0x20, };
const char http_10[9] =
/* "HTTP/1.0" */
{0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, };
@@ -73,6 +76,12 @@ const char http_content_type_jpg [29] =
const char http_content_type_binary[43] =
/* "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, };
+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] =
/* ".html" */
{0x2e, 0x68, 0x74, 0x6d, 0x6c, };
View
3  apps/webbrowser/http-strings.h
@@ -3,6 +3,7 @@ extern const char http_200[5];
extern const char http_301[5];
extern const char http_302[5];
extern const char http_get[5];
+extern const char http_post[6];
extern const char http_10[9];
extern const char http_11[9];
extern const char http_content_type[15];
@@ -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_jpg [29];
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_htm[5];
extern const char http_css[5];
View
54 apps/webbrowser/webclient.c
@@ -34,6 +34,7 @@
*/
#include <string.h>
+#include <stdio.h>
#include "contiki-net.h"
#include "www.h"
@@ -65,6 +66,8 @@ struct webclient_state {
u16_t port;
char host[40];
char file[WWW_CONF_MAX_URLLEN];
+ char formvars[100];
+ char content_length[5];
u16_t getrequestptr;
u16_t getrequestleft;
@@ -110,9 +113,13 @@ webclient_init(void)
static 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.getrequestleft = sizeof(http_get) - 1 + 1 +
+ s.getrequestleft = http_method_size - 1 + 1 +
sizeof(http_10) - 1 +
sizeof(http_crnl) - 1 +
sizeof(http_host) - 1 +
@@ -121,7 +128,16 @@ init_connection(void)
(u16_t)strlen(s.file) + (u16_t)strlen(s.host);
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;
+
}
/*-----------------------------------------------------------------------------------*/
void
@@ -130,8 +146,8 @@ webclient_close(void)
s.state = WEBCLIENT_STATE_CLOSE;
}
/*-----------------------------------------------------------------------------------*/
-unsigned char
-webclient_get(const char *host, u16_t port, const char *file)
+static unsigned char
+webclient_common(const char *host, u16_t port, const char *file)
{
uip_ipaddr_t addr;
struct uip_conn *conn;
@@ -165,6 +181,20 @@ webclient_get(const char *host, u16_t port, const char *file)
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
windowend variables. Only data that fits within the window is
copied. This function is used to copy data into the uIP buffer, which
@@ -215,6 +245,11 @@ senddata(void)
u16_t len;
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) {
windowstart = s.getrequestptr;
@@ -222,7 +257,7 @@ senddata(void)
windowend = windowstart + uip_mss();
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, " ", 1);
curptr = window_copy(curptr, http_10, sizeof(http_10) - 1);
@@ -232,9 +267,20 @@ senddata(void)
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, 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,
(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()?
uip_mss():
View
1  apps/webbrowser/webclient.h
@@ -53,6 +53,7 @@ void webclient_closed(void);
/* Functions. */
void webclient_init(void);
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_appcall(void *state);
Please sign in to comment.
Something went wrong with that request. Please try again.