Contiki-NG
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 #include "sys/cc.h"
36 
37 struct http_socket;
38 
39 typedef enum {
40  HTTP_SOCKET_ERR,
41  HTTP_SOCKET_OK,
42  HTTP_SOCKET_HEADER,
43  HTTP_SOCKET_DATA,
44  HTTP_SOCKET_CLOSED,
45  HTTP_SOCKET_TIMEDOUT,
46  HTTP_SOCKET_ABORTED,
47  HTTP_SOCKET_HOSTNAME_NOT_FOUND,
48 } http_socket_event_t;
49 
50 struct http_socket_header {
51  uint16_t status_code;
52  int64_t content_length;
53  struct {
54  int64_t first_byte_pos;
55  int64_t last_byte_pos;
56  int64_t instance_length;
57  } content_range;
58 };
59 
60 typedef void (* http_socket_callback_t)(struct http_socket *s,
61  void *ptr,
62  http_socket_event_t ev,
63  const uint8_t *data,
64  uint16_t datalen);
65 
66 #define HTTP_SOCKET_INPUTBUFSIZE UIP_TCP_MSS
67 #define HTTP_SOCKET_OUTPUTBUFSIZE MAX(UIP_TCP_MSS, 128)
68 
69 #define HTTP_SOCKET_URLLEN 128
70 
71 #define HTTP_SOCKET_TIMEOUT ((2 * 60 + 30) * CLOCK_SECOND)
72 
73 struct http_socket {
74  struct http_socket *next;
75  struct tcp_socket s;
76  uip_ipaddr_t proxy_addr;
77  uint16_t proxy_port;
78  int64_t pos;
79  uint64_t length;
80  const uint8_t *postdata;
81  uint16_t postdatalen;
82  http_socket_callback_t callback;
83  void *callbackptr;
84  int did_tcp_connect;
85  char url[HTTP_SOCKET_URLLEN];
86  uint8_t inputbuf[HTTP_SOCKET_INPUTBUFSIZE];
87  uint8_t outputbuf[HTTP_SOCKET_OUTPUTBUFSIZE];
88 
89  struct etimer timeout_timer;
90  uint8_t timeout_timer_started;
91  struct pt pt, headerpt;
92  int header_chars;
93  char header_field[15];
94  struct http_socket_header header;
95  uint8_t header_received;
96  uint64_t bodylen;
97  const char *content_type;
98 };
99 
100 void http_socket_init(struct http_socket *s);
101 
102 int http_socket_get(struct http_socket *s, const char *url,
103  int64_t pos, uint64_t length,
104  http_socket_callback_t callback,
105  void *callbackptr);
106 
107 int http_socket_post(struct http_socket *s, const char *url,
108  const void *postdata,
109  uint16_t postdatalen,
110  const char *content_type,
111  http_socket_callback_t callback,
112  void *callbackptr);
113 
114 int http_socket_close(struct http_socket *socket);
115 
116 void http_socket_set_proxy(struct http_socket *s,
117  const uip_ipaddr_t *addr, uint16_t port);
118 
119 
120 #endif /* HTTP_SOCKET_H */
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:107
A timer.
Definition: etimer.h:76
Default definitions of C compiler quirk work-arounds.