1 |
/* |
2 |
http-connection.c - TCP connection between HTTP client and server |
3 |
Copyright (C) 2008 siliconforks.com |
4 |
|
5 |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License along |
16 |
with this program; if not, write to the Free Software Foundation, Inc., |
17 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
*/ |
19 |
|
20 |
#include <config.h> |
21 |
|
22 |
#include "http-server.h" |
23 |
|
24 |
#include <assert.h> |
25 |
#include <errno.h> |
26 |
#include <string.h> |
27 |
|
28 |
#include "util.h" |
29 |
|
30 |
#define CONNECTION_BUFFER_CAPACITY 8192 |
31 |
|
32 |
struct HTTPConnection { |
33 |
SOCKET s; |
34 |
uint8_t input_buffer[CONNECTION_BUFFER_CAPACITY]; |
35 |
size_t input_buffer_offset; |
36 |
size_t input_buffer_length; |
37 |
uint8_t output_buffer[CONNECTION_BUFFER_CAPACITY]; |
38 |
size_t output_buffer_offset; |
39 |
size_t output_buffer_length; |
40 |
}; |
41 |
|
42 |
static HTTPConnection * HTTPConnection_new(SOCKET s) { |
43 |
HTTPConnection * connection = xmalloc(sizeof(HTTPConnection)); |
44 |
connection->s = s; |
45 |
connection->input_buffer_offset = 0; |
46 |
connection->input_buffer_length = 0; |
47 |
connection->output_buffer_offset = 0; |
48 |
connection->output_buffer_length = 0; |
49 |
return connection; |
50 |
} |
51 |
|
52 |
HTTPConnection * HTTPConnection_new_client(const char * host, uint16_t port) { |
53 |
struct in_addr ip_address; |
54 |
if (! inet_aton(host, &ip_address)) { |
55 |
/* it's a host name */ |
56 |
if (xgethostbyname(host, &ip_address) != 0) { |
57 |
return NULL; |
58 |
} |
59 |
} |
60 |
|
61 |
SOCKET s = socket(PF_INET, SOCK_STREAM, 0); |
62 |
if (s == INVALID_SOCKET) { |
63 |
return NULL; |
64 |
} |
65 |
|
66 |
struct sockaddr_in a; |
67 |
a.sin_family = AF_INET; |
68 |
a.sin_port = htons(port); |
69 |
a.sin_addr = ip_address; |
70 |
|
71 |
if (connect(s, (struct sockaddr *) &a, sizeof(a)) < 0) { |
72 |
closesocket(s); |
73 |
return NULL; |
74 |
} |
75 |
|
76 |
return HTTPConnection_new(s); |
77 |
} |
78 |
|
79 |
HTTPConnection * HTTPConnection_new_server(SOCKET s) { |
80 |
return HTTPConnection_new(s); |
81 |
} |
82 |
|
83 |
int HTTPConnection_delete(HTTPConnection * connection) { |
84 |
int result = 0; |
85 |
if (closesocket(connection->s) == -1) { |
86 |
result = errno; |
87 |
} |
88 |
free(connection); |
89 |
return result; |
90 |
} |
91 |
|
92 |
int HTTPConnection_get_peer(HTTPConnection * connection, struct sockaddr_in * peer) { |
93 |
int result = 0; |
94 |
socklen_t length = sizeof(struct sockaddr_in); |
95 |
if (getpeername(connection->s, (struct sockaddr *) peer, &length) == -1) { |
96 |
result = errno; |
97 |
} |
98 |
return result; |
99 |
} |
100 |
|
101 |
int HTTPConnection_read_octet(HTTPConnection * connection, int * octet) { |
102 |
if (connection->input_buffer_offset >= connection->input_buffer_length) { |
103 |
ssize_t bytes_received = recv(connection->s, connection->input_buffer, CONNECTION_BUFFER_CAPACITY, 0); |
104 |
if (bytes_received == -1) { |
105 |
return errno; |
106 |
} |
107 |
else if (bytes_received == 0) { |
108 |
/* orderly shutdown */ |
109 |
*octet = -1; |
110 |
return 0; |
111 |
} |
112 |
else { |
113 |
connection->input_buffer_offset = 0; |
114 |
connection->input_buffer_length = bytes_received; |
115 |
} |
116 |
} |
117 |
*octet = connection->input_buffer[connection->input_buffer_offset]; |
118 |
connection->input_buffer_offset++; |
119 |
return 0; |
120 |
} |
121 |
|
122 |
int HTTPConnection_peek_octet(HTTPConnection * connection, int * octet) { |
123 |
int result = HTTPConnection_read_octet(connection, octet); |
124 |
|
125 |
/* check for error */ |
126 |
if (result != 0) { |
127 |
return result; |
128 |
} |
129 |
|
130 |
/* check for end */ |
131 |
if (*octet == -1) { |
132 |
return 0; |
133 |
} |
134 |
|
135 |
/* reset input buffer */ |
136 |
connection->input_buffer_offset--; |
137 |
return 0; |
138 |
} |
139 |
|
140 |
int HTTPConnection_write(HTTPConnection * connection, const void * p, size_t size) { |
141 |
while (size > 0) { |
142 |
if (connection->output_buffer_length == CONNECTION_BUFFER_CAPACITY) { |
143 |
/* buffer full */ |
144 |
ssize_t bytes_sent = send(connection->s, connection->output_buffer, CONNECTION_BUFFER_CAPACITY, 0); |
145 |
if (bytes_sent == -1) { |
146 |
return errno; |
147 |
} |
148 |
connection->output_buffer_length = 0; |
149 |
} |
150 |
|
151 |
size_t buffer_remaining = CONNECTION_BUFFER_CAPACITY - connection->output_buffer_length; |
152 |
size_t bytes_to_copy; |
153 |
if (size <= buffer_remaining) { |
154 |
bytes_to_copy = size; |
155 |
} |
156 |
else { |
157 |
bytes_to_copy = buffer_remaining; |
158 |
} |
159 |
|
160 |
memcpy(connection->output_buffer + connection->output_buffer_length, p, bytes_to_copy); |
161 |
connection->output_buffer_length += bytes_to_copy; |
162 |
p += bytes_to_copy; |
163 |
size -= bytes_to_copy; |
164 |
} |
165 |
|
166 |
return 0; |
167 |
} |
168 |
|
169 |
int HTTPConnection_flush(HTTPConnection * connection) { |
170 |
if (connection->output_buffer_length > 0) { |
171 |
ssize_t bytes_sent = send(connection->s, connection->output_buffer, connection->output_buffer_length, 0); |
172 |
if (bytes_sent == -1) { |
173 |
return errno; |
174 |
} |
175 |
connection->output_buffer_length = 0; |
176 |
} |
177 |
return 0; |
178 |
} |