From 7345579ad6a271c51ee55f1d5c121e5db1588d3f Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Sat, 4 May 2013 11:32:24 +0000 Subject: [PATCH] Replace twitter app with generic http-post-auth Twitter removed http basic auth from the api in august 2010, but this underlying code is a good example of doing http basic auth in contiki. The app has been renamed, and some fixes applied to make it build cleanly. --- apps/http-post-auth/Makefile.http-post-auth | 2 + .../http-post-auth.c} | 43 ++++++++++--------- .../http-post-auth.h} | 12 ++++-- apps/twitter/Makefile.twitter | 2 - 4 files changed, 33 insertions(+), 26 deletions(-) create mode 100644 apps/http-post-auth/Makefile.http-post-auth rename apps/{twitter/twitter.c => http-post-auth/http-post-auth.c} (88%) rename apps/{twitter/twitter.h => http-post-auth/http-post-auth.h} (87%) delete mode 100644 apps/twitter/Makefile.twitter diff --git a/apps/http-post-auth/Makefile.http-post-auth b/apps/http-post-auth/Makefile.http-post-auth new file mode 100644 index 000000000..d97f9d48e --- /dev/null +++ b/apps/http-post-auth/Makefile.http-post-auth @@ -0,0 +1,2 @@ +http-post-auth_src = http-post-auth.c +http-post-auth_dsc = diff --git a/apps/twitter/twitter.c b/apps/http-post-auth/http-post-auth.c similarity index 88% rename from apps/twitter/twitter.c rename to apps/http-post-auth/http-post-auth.c index 30d396aba..71c493836 100644 --- a/apps/twitter/twitter.c +++ b/apps/http-post-auth/http-post-auth.c @@ -32,16 +32,15 @@ /** * \file - * Contiki interface to posting Twitter messages + * Contiki interface to post to http basic auth services * \author * Adam Dunkels */ #include "contiki.h" #include "contiki-net.h" -#include "shell.h" -#include "twitter.h" +#include "http-post-auth.h" #include #include @@ -50,7 +49,9 @@ #define MAX_MESSAGE 160 #define MAX_LENGTH 10 -struct twitter_state { +#define HOST_NAME "api.example.org" + +struct http_post_auth_state { unsigned char timer; struct psock sin, sout; char lengthstr[MAX_LENGTH]; @@ -60,9 +61,11 @@ struct twitter_state { uip_ipaddr_t addr; }; -struct twitter_state conn; +struct http_post_auth_state conn; +#ifndef DEBUG #define DEBUG 0 +#endif #if DEBUG #include #define PRINTF(...) printf(__VA_ARGS__) @@ -70,7 +73,7 @@ struct twitter_state conn; #define PRINTF(...) #endif -PROCESS(twitter_process, "Twitter client"); +PROCESS(http_post_auth_process, "HTTP POST auth client"); /*---------------------------------------------------------------------------*/ static uint8_t base64_encode_6bits(uint8_t c) @@ -123,15 +126,15 @@ base64_encode_24bits(const uint8_t inputdata[], char outputdata[], int len) } /*---------------------------------------------------------------------------*/ int -twitter_post(const uint8_t *username_password, const char *msg) +http_post_auth(const uint8_t *username_password, const char *msg) { int len; int i, j; - struct twitter_state *s; + struct http_post_auth_state *s; - process_exit(&twitter_process); + process_exit(&http_post_auth_process); - /* s = (struct twitter_state *)memb_alloc(&conns);*/ + /* s = (struct http_post_auth_state *)memb_alloc(&conns);*/ s = &conn; if(s == NULL) { PRINTF("Could not allocate memory for the tweet\n"); @@ -159,12 +162,12 @@ twitter_post(const uint8_t *username_password, const char *msg) PRINTF("message '%s'\n", s->message);*/ /* Spawn process to deal with TCP connection */ - process_start(&twitter_process, (char *)s); + process_start(&http_post_auth_process, (char *)s); return 1; } /*---------------------------------------------------------------------------*/ static int -handle_output(struct twitter_state *s) +handle_output(struct http_post_auth_state *s) { PSOCK_BEGIN(&s->sout); /* Send POST header */ @@ -178,7 +181,7 @@ handle_output(struct twitter_state *s) /* Send Agent header */ PSOCK_SEND_STR(&s->sout, "User-Agent: Contiki 2.x\r\n"); - PSOCK_SEND_STR(&s->sout, "Host: twitter.com\r\n"); + PSOCK_SEND_STR(&s->sout, "Host: " HOST_NAME "\r\n"); PSOCK_SEND_STR(&s->sout, "Accept: */*\r\n"); /* Send Content length header */ @@ -202,7 +205,7 @@ handle_output(struct twitter_state *s) } /*---------------------------------------------------------------------------*/ static int -handle_input(struct twitter_state *s) +handle_input(struct http_post_auth_state *s) { PSOCK_BEGIN(&s->sin); @@ -212,26 +215,26 @@ handle_input(struct twitter_state *s) } /*---------------------------------------------------------------------------*/ static void -handle_connection(struct twitter_state *s) +handle_connection(struct http_post_auth_state *s) { handle_input(s); handle_output(s); } /*---------------------------------------------------------------------------*/ -PROCESS_THREAD(twitter_process, ev, data) +PROCESS_THREAD(http_post_auth_process, ev, data) { - struct twitter_state *s = data; + struct http_post_auth_state *s = data; struct uip_conn *conn; PROCESS_BEGIN(); - /* Lookup host "twitter.com" */ + /* Lookup host */ /* XXX for now, just use 128.121.146.228 */ uip_ipaddr(&s->addr, 128,121,146,228); - /* Open a TCP connection to port 80 on twitter.com */ + /* Open a TCP connection to port 80 */ conn = tcp_connect(&s->addr, uip_htons(80), s); if(conn == NULL) { PRINTF("Could not open TCP connection\n"); @@ -243,7 +246,7 @@ PROCESS_THREAD(twitter_process, ev, data) PROCESS_WAIT_EVENT(); if(ev == tcpip_event) { - struct twitter_state *s = (struct twitter_state *)data; + struct http_post_auth_state *s = (struct http_post_auth_state *)data; if(uip_closed() || uip_aborted() || uip_timedout()) { if(uip_closed()) { diff --git a/apps/twitter/twitter.h b/apps/http-post-auth/http-post-auth.h similarity index 87% rename from apps/twitter/twitter.h rename to apps/http-post-auth/http-post-auth.h index 0088821b8..5204b6430 100644 --- a/apps/twitter/twitter.h +++ b/apps/http-post-auth/http-post-auth.h @@ -30,9 +30,13 @@ * */ -#ifndef __TWITTER_H__ -#define __TWITTER_H__ +#ifndef __HTTP_POST_AUTH_H__ +#define __HTTP_POST_AUTH_H__ -int twitter_post(const uint8_t *username_password, const char *message); +#include "contiki-net.h" -#endif /* __TWITTER_H__ */ +PROCESS_NAME(http_post_auth_process); + +int http_post_auth(const uint8_t *username_password, const char *message); + +#endif /* __HTTP_POST_AUTH_H__ */ diff --git a/apps/twitter/Makefile.twitter b/apps/twitter/Makefile.twitter deleted file mode 100644 index d2e7d3f85..000000000 --- a/apps/twitter/Makefile.twitter +++ /dev/null @@ -1,2 +0,0 @@ -twitter_src = twitter.c -twitter_dsc =