Contiki 3.x
http-socket.h
1 /*
2  * Copyright (c) 2013, Thingsquare, http://www.thingsquare.com/.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 #ifndef HTTP_SOCKET_H
32 #define HTTP_SOCKET_H
33 
34 #include "tcp-socket.h"
35 
36 struct http_socket;
37 
38 typedef enum {
39  HTTP_SOCKET_ERR,
40  HTTP_SOCKET_OK,
41  HTTP_SOCKET_HEADER,
42  HTTP_SOCKET_DATA,
43  HTTP_SOCKET_CLOSED,
44  HTTP_SOCKET_TIMEDOUT,
45  HTTP_SOCKET_ABORTED,
46  HTTP_SOCKET_HOSTNAME_NOT_FOUND,
47 } http_socket_event_t;
48 
49 struct http_socket_header {
50  uint16_t status_code;
51  int64_t content_length;
52  struct {
53  int64_t first_byte_pos;
54  int64_t last_byte_pos;
55  int64_t instance_length;
56  } content_range;
57 };
58 
59 typedef void (* http_socket_callback_t)(struct http_socket *s,
60  void *ptr,
61  http_socket_event_t ev,
62  const uint8_t *data,
63  uint16_t datalen);
64 
65 #define MAX(n, m) (((n) < (m)) ? (m) : (n))
66 
67 #define HTTP_SOCKET_INPUTBUFSIZE UIP_TCP_MSS
68 #define HTTP_SOCKET_OUTPUTBUFSIZE MAX(UIP_TCP_MSS, 128)
69 
70 #define HTTP_SOCKET_URLLEN 128
71 
72 #define HTTP_SOCKET_TIMEOUT ((2 * 60 + 30) * CLOCK_SECOND)
73 
74 struct http_socket {
75  struct http_socket *next;
76  struct tcp_socket s;
77  uip_ipaddr_t proxy_addr;
78  uint16_t proxy_port;
79  int64_t pos;
80  uint64_t length;
81  const uint8_t *postdata;
82  uint16_t postdatalen;
83  http_socket_callback_t callback;
84  void *callbackptr;
85  int did_tcp_connect;
86  char url[HTTP_SOCKET_URLLEN];
87  uint8_t inputbuf[HTTP_SOCKET_INPUTBUFSIZE];
88  uint8_t outputbuf[HTTP_SOCKET_OUTPUTBUFSIZE];
89 
90  struct etimer timeout_timer;
91  uint8_t timeout_timer_started;
92  struct pt pt, headerpt;
93  int header_chars;
94  char header_field[15];
95  struct http_socket_header header;
96  uint8_t header_received;
97  uint64_t bodylen;
98  const char *content_type;
99 };
100 
101 void http_socket_init(struct http_socket *s);
102 
103 int http_socket_get(struct http_socket *s, const char *url,
104  int64_t pos, uint64_t length,
105  http_socket_callback_t callback,
106  void *callbackptr);
107 
108 int http_socket_post(struct http_socket *s, const char *url,
109  const void *postdata,
110  uint16_t postdatalen,
111  const char *content_type,
112  http_socket_callback_t callback,
113  void *callbackptr);
114 
115 int http_socket_close(struct http_socket *socket);
116 
117 void http_socket_set_proxy(struct http_socket *s,
118  const uip_ipaddr_t *addr, uint16_t port);
119 
120 
121 #endif /* HTTP_SOCKET_H */
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
A timer.
Definition: etimer.h:76