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 <stdio.h>
|
---|
37 | #include <stdlib.h>
|
---|
38 | #include <str.h>
|
---|
39 | #include <macros.h>
|
---|
40 |
|
---|
41 | #include <net/socket.h>
|
---|
42 | #include <inet/dnsr.h>
|
---|
43 |
|
---|
44 | #include <http/http.h>
|
---|
45 | #include <http/receive-buffer.h>
|
---|
46 |
|
---|
47 | static ssize_t http_receive(void *client_data, void *buf, size_t buf_size)
|
---|
48 | {
|
---|
49 | http_t *http = client_data;
|
---|
50 | return recv(http->conn_sd, buf, buf_size, 0);
|
---|
51 | }
|
---|
52 |
|
---|
53 | http_t *http_create(const char *host, uint16_t port)
|
---|
54 | {
|
---|
55 | http_t *http = malloc(sizeof(http_t));
|
---|
56 | if (http == NULL)
|
---|
57 | return NULL;
|
---|
58 |
|
---|
59 | http->host = str_dup(host);
|
---|
60 | if (http->host == NULL) {
|
---|
61 | free(http);
|
---|
62 | return NULL;
|
---|
63 | }
|
---|
64 | http->port = port;
|
---|
65 |
|
---|
66 | http->buffer_size = 4096;
|
---|
67 | int rc = recv_buffer_init(&http->recv_buffer, http->buffer_size,
|
---|
68 | http_receive, http);
|
---|
69 | if (rc != EOK) {
|
---|
70 | free(http);
|
---|
71 | return NULL;
|
---|
72 | }
|
---|
73 |
|
---|
74 | return http;
|
---|
75 | }
|
---|
76 |
|
---|
77 | int http_connect(http_t *http)
|
---|
78 | {
|
---|
79 | if (http->connected)
|
---|
80 | return EBUSY;
|
---|
81 |
|
---|
82 | /* Interpret as address */
|
---|
83 | int rc = inet_addr_parse(http->host, &http->addr);
|
---|
84 |
|
---|
85 | if (rc != EOK) {
|
---|
86 | /* Interpret as a host name */
|
---|
87 | dnsr_hostinfo_t *hinfo = NULL;
|
---|
88 | rc = dnsr_name2host(http->host, &hinfo, AF_NONE);
|
---|
89 |
|
---|
90 | if (rc != EOK)
|
---|
91 | return rc;
|
---|
92 |
|
---|
93 | http->addr = hinfo->addr;
|
---|
94 | dnsr_hostinfo_destroy(hinfo);
|
---|
95 | }
|
---|
96 |
|
---|
97 | struct sockaddr_in addr;
|
---|
98 | struct sockaddr_in6 addr6;
|
---|
99 | uint16_t af = inet_addr_sockaddr_in(&http->addr, &addr, &addr6);
|
---|
100 |
|
---|
101 | http->conn_sd = socket(PF_INET, SOCK_STREAM, 0);
|
---|
102 | if (http->conn_sd < 0)
|
---|
103 | return http->conn_sd;
|
---|
104 |
|
---|
105 | switch (af) {
|
---|
106 | case AF_INET:
|
---|
107 | addr.sin_port = htons(http->port);
|
---|
108 | rc = connect(http->conn_sd, (struct sockaddr *) &addr, sizeof(addr));
|
---|
109 | break;
|
---|
110 | case AF_INET6:
|
---|
111 | addr6.sin6_port = htons(http->port);
|
---|
112 | rc = connect(http->conn_sd, (struct sockaddr *) &addr6, sizeof(addr6));
|
---|
113 | break;
|
---|
114 | default:
|
---|
115 | return ENOTSUP;
|
---|
116 | }
|
---|
117 |
|
---|
118 | return rc;
|
---|
119 | }
|
---|
120 |
|
---|
121 | int http_close(http_t *http)
|
---|
122 | {
|
---|
123 | if (!http->connected)
|
---|
124 | return EINVAL;
|
---|
125 |
|
---|
126 | return closesocket(http->conn_sd);
|
---|
127 | }
|
---|
128 |
|
---|
129 | void http_destroy(http_t *http)
|
---|
130 | {
|
---|
131 | recv_buffer_fini(&http->recv_buffer);
|
---|
132 | free(http);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /** @}
|
---|
136 | */
|
---|