1 | /*
|
---|
2 | * Copyright (c) 2013 Martin Sucha
|
---|
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 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup http
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <str.h>
|
---|
40 | #include <macros.h>
|
---|
41 |
|
---|
42 | #include <inet/dnsr.h>
|
---|
43 | #include <inet/tcp.h>
|
---|
44 |
|
---|
45 | #include <http/http.h>
|
---|
46 | #include <http/receive-buffer.h>
|
---|
47 |
|
---|
48 | static ssize_t http_receive(void *client_data, void *buf, size_t buf_size)
|
---|
49 | {
|
---|
50 | http_t *http = client_data;
|
---|
51 | size_t nrecv;
|
---|
52 | int rc;
|
---|
53 |
|
---|
54 | rc = tcp_conn_recv(http->conn, buf, buf_size, &nrecv);
|
---|
55 | if (rc != EOK)
|
---|
56 | return rc;
|
---|
57 |
|
---|
58 | return nrecv;
|
---|
59 | }
|
---|
60 |
|
---|
61 | http_t *http_create(const char *host, uint16_t port)
|
---|
62 | {
|
---|
63 | http_t *http = malloc(sizeof(http_t));
|
---|
64 | if (http == NULL)
|
---|
65 | return NULL;
|
---|
66 |
|
---|
67 | http->host = str_dup(host);
|
---|
68 | if (http->host == NULL) {
|
---|
69 | free(http);
|
---|
70 | return NULL;
|
---|
71 | }
|
---|
72 | http->port = port;
|
---|
73 |
|
---|
74 | http->buffer_size = 4096;
|
---|
75 | int rc = recv_buffer_init(&http->recv_buffer, http->buffer_size,
|
---|
76 | http_receive, http);
|
---|
77 | if (rc != EOK) {
|
---|
78 | free(http);
|
---|
79 | return NULL;
|
---|
80 | }
|
---|
81 |
|
---|
82 | return http;
|
---|
83 | }
|
---|
84 |
|
---|
85 | int http_connect(http_t *http)
|
---|
86 | {
|
---|
87 | if (http->conn != NULL)
|
---|
88 | return EBUSY;
|
---|
89 |
|
---|
90 | /* Interpret as address */
|
---|
91 | int rc = inet_addr_parse(http->host, &http->addr);
|
---|
92 |
|
---|
93 | if (rc != EOK) {
|
---|
94 | /* Interpret as a host name */
|
---|
95 | dnsr_hostinfo_t *hinfo = NULL;
|
---|
96 | rc = dnsr_name2host(http->host, &hinfo, ip_any);
|
---|
97 |
|
---|
98 | if (rc != EOK)
|
---|
99 | return rc;
|
---|
100 |
|
---|
101 | http->addr = hinfo->addr;
|
---|
102 | dnsr_hostinfo_destroy(hinfo);
|
---|
103 | }
|
---|
104 |
|
---|
105 | inet_ep2_t epp;
|
---|
106 |
|
---|
107 | inet_ep2_init(&epp);
|
---|
108 | epp.remote.addr = http->addr;
|
---|
109 | epp.remote.port = http->port;
|
---|
110 |
|
---|
111 | rc = tcp_create(&http->tcp);
|
---|
112 | if (rc != EOK)
|
---|
113 | return rc;
|
---|
114 |
|
---|
115 | rc = tcp_conn_create(http->tcp, &epp, NULL, NULL, &http->conn);
|
---|
116 | if (rc != EOK)
|
---|
117 | return rc;
|
---|
118 |
|
---|
119 | rc = tcp_conn_wait_connected(http->conn);
|
---|
120 | if (rc != EOK)
|
---|
121 | return rc;
|
---|
122 |
|
---|
123 | return rc;
|
---|
124 | }
|
---|
125 |
|
---|
126 | int http_close(http_t *http)
|
---|
127 | {
|
---|
128 | if (http->conn == NULL)
|
---|
129 | return EINVAL;
|
---|
130 |
|
---|
131 | tcp_conn_destroy(http->conn);
|
---|
132 | tcp_destroy(http->tcp);
|
---|
133 | return EOK;
|
---|
134 | }
|
---|
135 |
|
---|
136 | void http_destroy(http_t *http)
|
---|
137 | {
|
---|
138 | recv_buffer_fini(&http->recv_buffer);
|
---|
139 | free(http);
|
---|
140 | }
|
---|
141 |
|
---|
142 | /** @}
|
---|
143 | */
|
---|