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
|
/** @file nanohttp-response.c HTTP response handling */
/******************************************************************
* $Id: nanohttp-response.c,v 1.19 2007/11/03 22:40:11 m0gg Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003-2004 Ferhat Ayaz
*
* 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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Email: ferhatayaz@yahoo.com
******************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifndef strtok_r
extern char* strtok_r(char*, const char*, char**);
#endif
#include "nanohttp-logging.h"
#include "nanohttp-error.h"
#include "nanohttp-common.h"
#include "nanohttp-socket.h"
#include "nanohttp-stream.h"
#include "nanohttp-mime.h"
#include "nanohttp-response.h"
static hresponse_t *
_hresponse_new(void)
{
hresponse_t *res;
if (!(res = (hresponse_t *) malloc(sizeof(hresponse_t))))
{
log_error("malloc failed (%s)", strerror(errno));
return NULL;
}
res->version = HTTP_1_1;
res->errcode = -1;
res->desc[0] = '\0';
res->header = NULL;
res->in = NULL;
res->content_type = NULL;
res->attachments = NULL;
return res;
}
static hresponse_t *
_hresponse_parse_header(const char *buffer)
{
hresponse_t *res;
char *s1, *s2, *str;
/* create response object */
res = _hresponse_new();
/* *** parse spec *** */
/* [HTTP/1.1 | 1.2] [CODE] [DESC] */
/* stage 1: HTTP spec */
str = (char *) strtok_r((char *) buffer, " ", &s2);
s1 = s2;
if (str == NULL)
{
log_error("Parse error reading HTTP spec");
return NULL;
}
if (!strcmp(str, "HTTP/1.0"))
res->version = HTTP_1_0;
else
res->version = HTTP_1_1;
/* stage 2: http code */
str = (char *) strtok_r(s1, " ", &s2);
s1 = s2;
if (str == NULL)
{
log_error("Parse error reading HTTP code");
return NULL;
}
res->errcode = atoi(str);
/* stage 3: description text */
str = (char *) strtok_r(s1, "\r\n", &s2);
s1 = s2;
if (str == NULL)
{
log_error("Parse error reading HTTP description");
return NULL;
}
/* res->desc = (char *) malloc(strlen(str) + 1);*/
strncpy(res->desc, str, RESPONSE_MAX_DESC_SIZE);
res->desc[strlen(str)] = '\0';
/* *** parse header *** */
/* [key]: [value] */
for (;;)
{
str = strtok_r(s1, "\n", &s2);
s1 = s2;
/* check if header ends without body */
if (str == NULL)
{
return res;
}
/* check also for end of header */
if (!strcmp(str, "\r"))
{
break;
}
str[strlen(str) - 1] = '\0';
res->header = hpairnode_parse(str, ":", res->header);
}
/* Check Content-type */
str = hpairnode_get(res->header, HEADER_CONTENT_TYPE);
if (str != NULL)
res->content_type = content_type_new(str);
/* return response object */
return res;
}
herror_t
hresponse_new_from_socket(struct hsocket_t *sock, hresponse_t ** out)
{
int i = 0, count;
herror_t status;
hresponse_t *res;
struct attachments_t *mimeMessage;
char buffer[MAX_HEADER_SIZE + 1];
read_header: /* for errorcode: 100 (continue) */
/* Read header */
while (i < MAX_HEADER_SIZE)
{
if ((status = hsocket_recv(sock, &(buffer[i]), 1, 1, &count)) != H_OK)
{
log_error("Socket read error");
return status;
}
buffer[i + 1] = '\0'; /* for strmp */
if (i > 3)
{
if (!strcmp(&(buffer[i - 1]), "\n\n") ||
!strcmp(&(buffer[i - 2]), "\n\r\n"))
break;
}
i++;
}
/* Create response */
res = _hresponse_parse_header(buffer);
if (res == NULL)
{
log_error("Header parse error");
return herror_new("hresponse_new_from_socket",
GENERAL_HEADER_PARSE_ERROR,
"Can not parse response header");
}
/* Chec for Errorcode: 100 (continue) */
if (res->errcode == 100)
{
hresponse_free(res);
i = 0;
goto read_header;
}
/* Create input stream */
res->in = http_input_stream_new(sock, res->header);
/* Check for MIME message */
if ((res->content_type &&
!strcmp(res->content_type->type, "multipart/related")))
{
status = mime_get_attachments(res->content_type, res->in, &mimeMessage);
if (status != H_OK)
{
/* TODO (#1#): Handle error */
hresponse_free(res);
return status;
}
else
{
res->attachments = mimeMessage;
http_input_stream_free(res->in);
res->in =
http_input_stream_new_from_file(mimeMessage->root_part->filename);
if (!res->in)
{
/* TODO (#1#): Handle error */
}
else
{
/* res->in->deleteOnExit = 1; */
}
}
}
*out = res;
return H_OK;
}
void
hresponse_free(hresponse_t * res)
{
if (res)
{
if (res->header)
hpairnode_free_deep(res->header);
if (res->in)
http_input_stream_free(res->in);
if (res->content_type)
content_type_free(res->content_type);
if (res->attachments)
attachments_free(res->attachments);
free(res);
}
return;
}
|