-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhttp_trans.c
executable file
·291 lines (272 loc) · 7.58 KB
/
http_trans.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* http_trans.c -- Functions for doing transport related stuff including
* automatically extending buffers and whatnot.
* Created: Christopher Blizzard <blizzard@appliedtheory.com>, 5-Aug-1998
*
* Copyright (C) 1998 Free Software Foundation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "http_trans.h"
#include "http_global.h"
static int
http_trans_buf_free(http_trans_conn *a_conn);
int
http_trans_connect(http_trans_conn *a_conn)
{
if ((a_conn == NULL) || (a_conn->host == NULL))
goto ec;
if (a_conn->hostinfo == NULL)
{
/* look up the name of the proxy if it's there. */
if (a_conn->proxy_host)
{
if ((a_conn->hostinfo = gethostbyname(a_conn->proxy_host)) == NULL)
{
a_conn->error_type = http_trans_err_type_host;
a_conn->error = h_errno;
goto ec;
}
}
else
{
/* look up the name */
if ((a_conn->hostinfo = gethostbyname(a_conn->host)) == NULL)
{
a_conn->error_type = http_trans_err_type_host;
a_conn->error = h_errno;
goto ec;
}
}
/* set up the saddr */
a_conn->saddr.sin_family = AF_INET;
/* set the proxy port */
if (a_conn->proxy_host)
a_conn->saddr.sin_port = htons(a_conn->proxy_port);
else
a_conn->saddr.sin_port = htons(a_conn->port);
/* copy the name info */
memcpy(&a_conn->saddr.sin_addr.s_addr,
a_conn->hostinfo->h_addr_list[0],
sizeof(unsigned long));
}
/* set up the socket */
if ((a_conn->sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
a_conn->error_type = http_trans_err_type_errno;
a_conn->error = errno;
goto ec;
}
/* set up the socket */
if (connect(a_conn->sock,
(struct sockaddr *)&a_conn->saddr,
sizeof(struct sockaddr)) < 0)
{
a_conn->error_type = http_trans_err_type_errno;
a_conn->error = errno;
goto ec;
}
return 0;
ec:
return -1;
}
http_trans_conn *
http_trans_conn_new(void)
{
http_trans_conn *l_return = NULL;
/* allocate a new connection struct */
l_return = (http_trans_conn *)malloc(sizeof(http_trans_conn));
memset(l_return, 0, sizeof(http_trans_conn));
/* default to 80 */
l_return->port = 80;
/* default to 1000 bytes at a time */
l_return->io_buf_chunksize = 1024;
/* allocate a new trans buffer */
l_return->io_buf = malloc(l_return->io_buf_chunksize);
memset(l_return->io_buf, 0, l_return->io_buf_chunksize);
l_return->io_buf_len = l_return->io_buf_chunksize;
/* make sure the socket looks like it's closed */
l_return->sock = -1;
return l_return;
}
void
http_trans_conn_destroy(http_trans_conn *a_conn)
{
/* destroy the connection structure. */
if (a_conn == NULL)
return;
if (a_conn->io_buf)
free(a_conn->io_buf);
if (a_conn->sock != -1)
close(a_conn->sock);
free(a_conn);
return;
}
const char *
http_trans_get_host_error(int a_herror)
{
switch (a_herror)
{
case HOST_NOT_FOUND:
return "Host not found";
case NO_ADDRESS:
return "An address is not associated with that host";
case NO_RECOVERY:
return "An unrecoverable name server error occured";
case TRY_AGAIN:
return "A temporary error occurred on an authoritative name server. Please try again later.";
default:
return "No error or error not known.";
}
}
int
http_trans_append_data_to_buf(http_trans_conn *a_conn,
char *a_data,
int a_data_len)
{
if (http_trans_buf_free(a_conn) < a_data_len)
{
a_conn->io_buf = realloc(a_conn->io_buf, a_conn->io_buf_len + a_data_len);
a_conn->io_buf_len += a_data_len;
}
memcpy(&a_conn->io_buf[a_conn->io_buf_alloc], a_data, a_data_len);
a_conn->io_buf_alloc += a_data_len;
return 1;
}
int
http_trans_read_into_buf(http_trans_conn *a_conn)
{
int l_read = 0;
int l_bytes_to_read = 0;
/* set the length if this is the first time */
if (a_conn->io_buf_io_left == 0)
{
a_conn->io_buf_io_left = a_conn->io_buf_chunksize;
a_conn->io_buf_io_done = 0;
}
/* make sure there's enough space */
if (http_trans_buf_free(a_conn) < a_conn->io_buf_io_left)
{
a_conn->io_buf = realloc(a_conn->io_buf,
a_conn->io_buf_len + a_conn->io_buf_io_left);
a_conn->io_buf_len += a_conn->io_buf_io_left;
}
/* check to see how much we should try to read */
if (a_conn->io_buf_io_left > a_conn->io_buf_chunksize)
l_bytes_to_read = a_conn->io_buf_chunksize;
else
l_bytes_to_read = a_conn->io_buf_io_left;
/* read in some data */
if ((a_conn->last_read = l_read = read(a_conn->sock,
&a_conn->io_buf[a_conn->io_buf_alloc],
l_bytes_to_read)) < 0)
{
if (errno == EINTR)
l_read = 0;
else
return HTTP_TRANS_ERR;
}
else if (l_read == 0)
return HTTP_TRANS_DONE;
/* mark the buffer */
a_conn->io_buf_io_left -= l_read;
a_conn->io_buf_io_done += l_read;
a_conn->io_buf_alloc += l_read;
/* generate the result */
if (a_conn->io_buf_io_left == 0)
return HTTP_TRANS_DONE;
return HTTP_TRANS_NOT_DONE;
}
int
http_trans_write_buf(http_trans_conn *a_conn)
{
int l_written = 0;
if (a_conn->io_buf_io_left == 0)
{
a_conn->io_buf_io_left = a_conn->io_buf_alloc;
a_conn->io_buf_io_done = 0;
}
/* write out some data */
if ((a_conn->last_read = l_written = write (a_conn->sock,
&a_conn->io_buf[a_conn->io_buf_io_done],
a_conn->io_buf_io_left)) <= 0)
{
if (errno == EINTR)
l_written = 0;
else
return HTTP_TRANS_ERR;
}
if (l_written == 0)
return HTTP_TRANS_DONE;
/* advance the counters */
a_conn->io_buf_io_left -= l_written;
a_conn->io_buf_io_done += l_written;
if (a_conn->io_buf_io_left == 0)
return HTTP_TRANS_DONE;
return HTTP_TRANS_NOT_DONE;
}
void
http_trans_buf_reset(http_trans_conn *a_conn)
{
if (a_conn->io_buf)
free(a_conn->io_buf);
a_conn->io_buf = malloc(a_conn->io_buf_chunksize);
memset(a_conn->io_buf, 0, a_conn->io_buf_chunksize);
a_conn->io_buf_len = a_conn->io_buf_chunksize;
a_conn->io_buf_alloc = 0;
a_conn->io_buf_io_done = 0;
a_conn->io_buf_io_left = 0;
}
void
http_trans_buf_clip(http_trans_conn *a_conn, char *a_clip_to)
{
int l_bytes = 0;
/* get the number of bytes to clip off of the front */
l_bytes = a_clip_to - a_conn->io_buf;
if (l_bytes > 0)
{
memmove(a_conn->io_buf, a_clip_to, a_conn->io_buf_alloc - l_bytes);
a_conn->io_buf_alloc -= l_bytes;
}
a_conn->io_buf_io_done = 0;
a_conn->io_buf_io_left = 0;
}
char *
http_trans_buf_has_patt(char *a_buf, int a_len,
char *a_pat, int a_patlen)
{
int i = 0;
for ( ; i <= ( a_len - a_patlen ); i++ )
{
if (a_buf[i] == a_pat[0])
{
if (memcmp(&a_buf[i], a_pat, a_patlen) == 0)
return &a_buf[i];
}
}
return NULL;
}
/* static functions */
static int
http_trans_buf_free(http_trans_conn *a_conn)
{
return (a_conn->io_buf_len - a_conn->io_buf_alloc);
}