Contiki 3.x
http-common.h
1 #ifndef HTTPCOMMON_H_
2 #define HTTPCOMMON_H_
3 
4 /*includes*/
5 #include "contiki.h"
6 #include "contiki-net.h"
7 
8 /*current state of the request, waiting: handling request, output: sending response*/
9 #define STATE_WAITING 0
10 #define STATE_OUTPUT 1
11 
12 /*definitions of the line ending characters*/
13 #define LINE_FEED_CHAR '\n'
14 #define CARRIAGE_RETURN_CHAR '\r'
15 
16 /*needed for web services giving all path (http://172.16.79.0/services/light1)
17  * instead relative (/services/light1) in HTTP request. Ex: Restlet lib. does it*/
18 extern const char* http_string;
19 
20 /*HTTP method strings*/
21 extern const char* http_get_string;
22 extern const char* http_head_string;
23 extern const char* http_post_string;
24 extern const char* http_put_string;
25 extern const char* http_delete_string;
26 
27 extern const char* httpv1_1;
28 extern const char* line_end;
29 extern const char* contiki;
30 extern const char* close;
31 
32 /*header names*/
33 extern const char* HTTP_HEADER_NAME_CONTENT_TYPE;
34 extern const char* HTTP_HEADER_NAME_CONTENT_LENGTH;
35 extern const char* HTTP_HEADER_NAME_LOCATION;
36 extern const char* HTTP_HEADER_NAME_CONNECTION;
37 extern const char* HTTP_HEADER_NAME_SERVER;
38 extern const char* HTTP_HEADER_NAME_HOST;
39 extern const char* HTTP_HEADER_NAME_IF_NONE_MATCH;
40 extern const char* HTTP_HEADER_NAME_ETAG;
41 
42 extern const char* header_delimiter;
43 
44 
45 /*Configuration parameters*/
46 #define HTTP_PORT 8080
47 #define HTTP_DATA_BUFF_SIZE 600
48 #define INCOMING_DATA_BUFF_SIZE 102 /*100+2, 100 = max url len, 2 = space char+'\0'*/
49 
50 /*HTTP method types*/
51 typedef enum {
52  HTTP_METHOD_GET = (1 << 0),
53  HTTP_METHOD_POST = (1 << 1),
54  HTTP_METHOD_PUT = (1 << 2),
55  HTTP_METHOD_DELETE = (1 << 3)
56 } http_method_t;
57 
58 //DY : FIXME right now same enum names with COAP with different values. Will this work fine?
59 typedef enum {
60  OK_200 = 200,
61  CREATED_201 = 201,
62  NOT_MODIFIED_304 = 304,
63  BAD_REQUEST_400 = 400,
64  NOT_FOUND_404 = 404,
65  METHOD_NOT_ALLOWED_405 = 405,
66  REQUEST_URI_TOO_LONG_414 = 414,
67  UNSUPPORTED_MADIA_TYPE_415 = 415,
68  INTERNAL_SERVER_ERROR_500 = 500,
69  BAD_GATEWAY_502 = 502,
70  SERVICE_UNAVAILABLE_503 = 503,
71  GATEWAY_TIMEOUT_504 = 504
72 } status_code_t;
73 
74 typedef enum {
75  TEXT_PLAIN,
76  TEXT_XML,
77  TEXT_CSV,
78  TEXT_HTML,
79  APPLICATION_XML,
80  APPLICATION_EXI,
81  APPLICATION_JSON,
82  APPLICATION_LINK_FORMAT,
83  APPLICATION_WWW_FORM,
84  UNKNOWN_CONTENT_TYPE
85 } content_type_t;
86 
87 /*Header type*/
88 struct http_header_t {
89  struct http_header_t* next;
90  char* name;
91  char* value;
92 };
93 typedef struct http_header_t http_header_t;
94 
95 /*This structure contains information about the HTTP request.*/
96 struct http_request_t {
97  char* url;
98  uint16_t url_len;
99  http_method_t request_type; /* GET, POST, etc */
100  char* query;
101  uint16_t query_len;
102  http_header_t* headers;
103  uint16_t payload_len;
104  uint8_t* payload;
105 };
106 typedef struct http_request_t http_request_t;
107 
108 /*This structure contains information about the HTTP response.*/
109 struct http_response_t {
110  status_code_t status_code;
111  char* status_string;
112  http_header_t* headers;
113  uint16_t payload_len;
114  uint8_t* payload;
115 };
116 typedef struct http_response_t http_response_t;
117 
118 /*This structure contains information about the TCP Connection.*/
119 typedef struct {
120  struct psock sin, sout; /*Protosockets for incoming and outgoing communication*/
121  struct pt outputpt;
122  char inputbuf[INCOMING_DATA_BUFF_SIZE]; /*to put incoming data in*/
123  uint8_t state;
124  http_request_t request;
125  http_response_t response;
126 } connection_state_t;
127 
128 /*error definitions*/
129 typedef enum {
130  HTTP_NO_ERROR,
131 
132  /*Memory errors*/
133  HTTP_MEMORY_ALLOC_ERR,
134  HTTP_MEMORY_BOUNDARY_EXCEEDED,
135 
136  /*specific errors*/
137  HTTP_XML_NOT_VALID,
138  HTTP_SOAP_MESSAGE_NOT_VALID,
139  HTTP_URL_TOO_LONG,
140  HTTP_URL_INVALID
141 } http_error_t;
142 
143 #endif /*HTTPCOMMON_H_*/
The representation of a protosocket.
Definition: psock.h:112