Skip to content

Commit

Permalink
Replace twitter app with generic http-post-auth
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
karlp committed May 4, 2013
1 parent 96e85cc commit 7345579
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
2 changes: 2 additions & 0 deletions apps/http-post-auth/Makefile.http-post-auth
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
http-post-auth_src = http-post-auth.c
http-post-auth_dsc =
43 changes: 23 additions & 20 deletions apps/twitter/twitter.c → apps/http-post-auth/http-post-auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@

/**
* \file
* Contiki interface to posting Twitter messages
* Contiki interface to post to http basic auth services
* \author
* Adam Dunkels <[email protected]>
*/

#include "contiki.h"
#include "contiki-net.h"
#include "shell.h"

#include "twitter.h"
#include "http-post-auth.h"

#include <stdio.h>
#include <string.h>
Expand All @@ -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];
Expand All @@ -60,17 +61,19 @@ 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 <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#else
#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)
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand All @@ -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);

Expand All @@ -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");
Expand All @@ -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()) {
Expand Down
12 changes: 8 additions & 4 deletions apps/twitter/twitter.h → apps/http-post-auth/http-post-auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */
2 changes: 0 additions & 2 deletions apps/twitter/Makefile.twitter

This file was deleted.

0 comments on commit 7345579

Please sign in to comment.