Mercurial > mplayer.hg
annotate libmpdemux/http.c @ 4185:85c90bb8729a
initial stuff. comments welcomed...
author | arpi |
---|---|
date | Wed, 16 Jan 2002 00:14:59 +0000 |
parents | 44c74b600573 |
children | c9f861653fe2 |
rev | line source |
---|---|
902 | 1 /* |
2 * HTTP Helper | |
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com> | |
4 * (C) 2001, MPlayer team. | |
5 */ | |
6 | |
870 | 7 #include <stdio.h> |
8 #include <stdlib.h> | |
9 #include <string.h> | |
10 | |
11 #include "http.h" | |
12 | |
13 HTTP_header_t * | |
14 http_new_header() { | |
15 HTTP_header_t *http_hdr; | |
16 | |
17 http_hdr = (HTTP_header_t*)malloc(sizeof(HTTP_header_t)); | |
18 if( http_hdr==NULL ) return NULL; | |
19 memset( http_hdr, 0, sizeof(HTTP_header_t) ); | |
20 | |
21 return http_hdr; | |
22 } | |
23 | |
24 void | |
25 http_free( HTTP_header_t *http_hdr ) { | |
3039 | 26 HTTP_field_t *field, *field2free; |
870 | 27 if( http_hdr==NULL ) return; |
28 if( http_hdr->protocol!=NULL ) free( http_hdr->protocol ); | |
29 if( http_hdr->uri!=NULL ) free( http_hdr->uri ); | |
30 if( http_hdr->reason_phrase!=NULL ) free( http_hdr->reason_phrase ); | |
31 if( http_hdr->body!=NULL ) free( http_hdr->body ); | |
32 if( http_hdr->field_search!=NULL ) free( http_hdr->field_search ); | |
902 | 33 if( http_hdr->method!=NULL ) free( http_hdr->method ); |
34 if( http_hdr->buffer!=NULL ) free( http_hdr->buffer ); | |
3039 | 35 field = http_hdr->first_field; |
36 while( field!=NULL ) { | |
37 field2free = field; | |
38 field = field->next; | |
39 free( field2free ); | |
40 } | |
870 | 41 free( http_hdr ); |
3039 | 42 http_hdr = NULL; |
870 | 43 } |
44 | |
902 | 45 int |
46 http_response_append( HTTP_header_t *http_hdr, char *response, int length ) { | |
47 char *ptr = NULL; | |
1027 | 48 if( http_hdr==NULL || response==NULL || length<0 ) return -1; |
902 | 49 ptr = (char*)malloc( http_hdr->buffer_size+length ); |
50 if( ptr==NULL ) { | |
51 printf("Memory allocation failed\n"); | |
52 return -1; | |
53 } | |
54 if( http_hdr->buffer_size==0 ) { | |
55 // Buffer empty, copy response into it. | |
56 memcpy( ptr, response, length ); | |
57 http_hdr->buffer_size = length; | |
58 } else { | |
59 // Buffer not empty, grow buffer, copy and append the response. | |
60 memcpy( ptr, http_hdr->buffer, http_hdr->buffer_size ); | |
61 free( http_hdr->buffer ); | |
62 memcpy( ptr+http_hdr->buffer_size, response, length ); | |
63 http_hdr->buffer_size += length; | |
64 } | |
65 http_hdr->buffer = ptr; | |
66 return http_hdr->buffer_size; | |
67 } | |
68 | |
69 int | |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
70 http_is_header_entire( HTTP_header_t *http_hdr ) { |
902 | 71 if( http_hdr==NULL ) return -1; |
72 | |
3784 | 73 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL && |
74 strstr(http_hdr->buffer, "\n\n")==NULL ) return 0; | |
75 return 1; | |
902 | 76 } |
77 | |
78 int | |
79 http_response_parse( HTTP_header_t *http_hdr ) { | |
870 | 80 char *hdr_ptr, *ptr; |
81 char *field=NULL; | |
82 int pos_hdr_sep, len; | |
902 | 83 if( http_hdr==NULL ) return -1; |
84 if( http_hdr->is_parsed ) return 0; | |
870 | 85 |
86 // Get the protocol | |
902 | 87 hdr_ptr = strstr( http_hdr->buffer, " " ); |
870 | 88 if( hdr_ptr==NULL ) { |
902 | 89 printf("Malformed answer. No space separator found.\n"); |
90 return -1; | |
870 | 91 } |
902 | 92 len = hdr_ptr-http_hdr->buffer; |
870 | 93 http_hdr->protocol = (char*)malloc(len+1); |
94 if( http_hdr->protocol==NULL ) { | |
95 printf("Memory allocation failed\n"); | |
902 | 96 return -1; |
870 | 97 } |
902 | 98 strncpy( http_hdr->protocol, http_hdr->buffer, len ); |
99 http_hdr->protocol[len]='\0'; | |
870 | 100 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) { |
101 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) { | |
902 | 102 printf("Malformed answer. Unable to get HTTP minor version.\n"); |
103 return -1; | |
870 | 104 } |
105 } | |
106 | |
107 // Get the status code | |
108 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) { | |
902 | 109 printf("Malformed answer. Unable to get status code.\n"); |
110 return -1; | |
870 | 111 } |
112 hdr_ptr += 4; | |
113 | |
114 // Get the reason phrase | |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
115 ptr = strstr( hdr_ptr, "\n" ); |
870 | 116 if( hdr_ptr==NULL ) { |
902 | 117 printf("Malformed answer. Unable to get the reason phrase.\n"); |
118 return -1; | |
870 | 119 } |
120 len = ptr-hdr_ptr; | |
121 http_hdr->reason_phrase = (char*)malloc(len+1); | |
122 if( http_hdr->reason_phrase==NULL ) { | |
123 printf("Memory allocation failed\n"); | |
902 | 124 return -1; |
870 | 125 } |
126 strncpy( http_hdr->reason_phrase, hdr_ptr, len ); | |
127 http_hdr->reason_phrase[len]='\0'; | |
128 | |
129 // Set the position of the header separator: \r\n\r\n | |
902 | 130 ptr = strstr( http_hdr->buffer, "\r\n\r\n" ); |
870 | 131 if( ptr==NULL ) { |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
132 ptr = strstr( http_hdr->buffer, "\n\n" ); |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
133 if( ptr==NULL ) { |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
134 printf("Header may be incomplete. No CRLF CRLF found.\n"); |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
135 return -1; |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
136 } |
870 | 137 } |
902 | 138 pos_hdr_sep = ptr-http_hdr->buffer; |
870 | 139 |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
140 // Point to the first line after the method line. |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
141 hdr_ptr = strstr( http_hdr->buffer, "\n" )+1; |
870 | 142 do { |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
143 ptr = hdr_ptr; |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
144 while( *ptr!='\r' && *ptr!='\n' ) ptr++; |
870 | 145 len = ptr-hdr_ptr; |
146 field = (char*)realloc(field, len+1); | |
147 if( field==NULL ) { | |
148 printf("Memory allocation failed\n"); | |
902 | 149 return -1; |
870 | 150 } |
151 strncpy( field, hdr_ptr, len ); | |
152 field[len]='\0'; | |
153 http_set_field( http_hdr, field ); | |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
154 hdr_ptr = ptr+((*ptr=='\r')?2:1); |
902 | 155 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) ); |
870 | 156 |
157 if( field!=NULL ) free( field ); | |
158 | |
902 | 159 if( pos_hdr_sep+4<http_hdr->buffer_size ) { |
870 | 160 // Response has data! |
902 | 161 int data_length = http_hdr->buffer_size-(pos_hdr_sep+4); |
870 | 162 http_hdr->body = (char*)malloc( data_length ); |
163 if( http_hdr->body==NULL ) { | |
902 | 164 printf("Memory allocation failed\n"); |
165 return -1; | |
870 | 166 } |
902 | 167 memcpy( http_hdr->body, http_hdr->buffer+pos_hdr_sep+4, data_length ); |
870 | 168 http_hdr->body_size = data_length; |
169 } | |
170 | |
902 | 171 http_hdr->is_parsed = 1; |
172 return 0; | |
870 | 173 } |
174 | |
175 char * | |
902 | 176 http_build_request( HTTP_header_t *http_hdr ) { |
3497 | 177 char *ptr, *uri=NULL; |
902 | 178 int len; |
3039 | 179 HTTP_field_t *field; |
870 | 180 if( http_hdr==NULL ) return NULL; |
181 | |
182 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET"); | |
183 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/"); | |
3497 | 184 else { |
185 uri = (char*)malloc(strlen(http_hdr->uri)*2); | |
186 if( uri==NULL ) { | |
187 printf("Memory allocation failed\n"); | |
188 return NULL; | |
189 } | |
190 url_escape_string( uri, http_hdr->uri ); | |
191 } | |
870 | 192 |
3497 | 193 //**** Compute the request length |
194 // Add the Method line | |
195 len = strlen(http_hdr->method)+strlen(uri)+12; | |
196 // Add the fields | |
197 field = http_hdr->first_field; | |
3039 | 198 while( field!=NULL ) { |
199 len += strlen(field->field_name)+2; | |
200 field = field->next; | |
201 } | |
3497 | 202 // Add the CRLF |
203 len += 2; | |
204 // Add the body | |
902 | 205 if( http_hdr->body!=NULL ) { |
206 len += http_hdr->body_size; | |
207 } | |
3497 | 208 // Free the buffer if it was previously used |
902 | 209 if( http_hdr->buffer!=NULL ) { |
210 free( http_hdr->buffer ); | |
211 http_hdr->buffer = NULL; | |
212 } | |
3497 | 213 http_hdr->buffer = (char*)malloc(len+1); |
902 | 214 if( http_hdr->buffer==NULL ) { |
215 printf("Memory allocation failed\n"); | |
216 return NULL; | |
217 } | |
218 http_hdr->buffer_size = len; | |
219 | |
3497 | 220 //*** Building the request |
902 | 221 ptr = http_hdr->buffer; |
3497 | 222 // Add the method line |
223 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version ); | |
3039 | 224 field = http_hdr->first_field; |
3497 | 225 // Add the field |
3039 | 226 while( field!=NULL ) { |
227 ptr += sprintf( ptr, "%s\r\n", field->field_name ); | |
228 field = field->next; | |
229 } | |
870 | 230 ptr += sprintf( ptr, "\r\n" ); |
3497 | 231 // Add the body |
870 | 232 if( http_hdr->body!=NULL ) { |
233 memcpy( ptr, http_hdr->body, http_hdr->body_size ); | |
234 } | |
3497 | 235 |
236 if( uri ) free( uri ); | |
902 | 237 return http_hdr->buffer; |
870 | 238 } |
239 | |
240 char * | |
241 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) { | |
242 if( http_hdr==NULL || field_name==NULL ) return NULL; | |
3039 | 243 http_hdr->field_search_pos = http_hdr->first_field; |
244 http_hdr->field_search = (char*)realloc( http_hdr->field_search, strlen(field_name)+1 ); | |
870 | 245 if( http_hdr->field_search==NULL ) { |
246 printf("Memory allocation failed\n"); | |
247 return NULL; | |
248 } | |
249 strcpy( http_hdr->field_search, field_name ); | |
250 return http_get_next_field( http_hdr ); | |
251 } | |
252 | |
253 char * | |
254 http_get_next_field( HTTP_header_t *http_hdr ) { | |
255 char *ptr; | |
256 int i; | |
3039 | 257 HTTP_field_t *field; |
870 | 258 if( http_hdr==NULL ) return NULL; |
259 | |
3039 | 260 field = http_hdr->field_search_pos; |
261 while( field!=NULL ) { | |
262 ptr = strstr( field->field_name, ":" ); | |
870 | 263 if( ptr==NULL ) return NULL; |
3039 | 264 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) { |
870 | 265 ptr++; // Skip the column |
266 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some | |
3039 | 267 http_hdr->field_search_pos = field->next; |
870 | 268 return ptr; // return the value without the field name |
269 } | |
3039 | 270 field = field->next; |
870 | 271 } |
272 return NULL; | |
273 } | |
274 | |
275 void | |
3039 | 276 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) { |
277 HTTP_field_t *new_field; | |
278 if( http_hdr==NULL || field_name==NULL ) return; | |
870 | 279 |
3039 | 280 new_field = (HTTP_field_t*)malloc(sizeof(HTTP_field_t)); |
281 if( new_field==NULL ) { | |
870 | 282 printf("Memory allocation failed\n"); |
283 return; | |
284 } | |
3039 | 285 new_field->next = NULL; |
286 new_field->field_name = (char*)malloc(strlen(field_name)+1); | |
287 if( new_field->field_name==NULL ) { | |
288 printf("Memory allocation failed\n"); | |
289 return; | |
290 } | |
291 strcpy( new_field->field_name, field_name ); | |
292 | |
293 if( http_hdr->last_field==NULL ) { | |
294 http_hdr->first_field = new_field; | |
295 } else { | |
296 http_hdr->last_field->next = new_field; | |
297 } | |
298 http_hdr->last_field = new_field; | |
870 | 299 http_hdr->field_nb++; |
300 } | |
301 | |
302 void | |
303 http_set_method( HTTP_header_t *http_hdr, const char *method ) { | |
304 if( http_hdr==NULL || method==NULL ) return; | |
305 | |
306 http_hdr->method = (char*)malloc(strlen(method)+1); | |
307 if( http_hdr->method==NULL ) { | |
308 printf("Memory allocation failed\n"); | |
309 return; | |
310 } | |
311 strcpy( http_hdr->method, method ); | |
312 } | |
313 | |
314 void | |
315 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) { | |
316 if( http_hdr==NULL || uri==NULL ) return; | |
317 | |
318 http_hdr->uri = (char*)malloc(strlen(uri)+1); | |
319 if( http_hdr->uri==NULL ) { | |
320 printf("Memory allocation failed\n"); | |
321 return; | |
322 } | |
323 strcpy( http_hdr->uri, uri ); | |
324 } | |
325 | |
326 void | |
327 http_debug_hdr( HTTP_header_t *http_hdr ) { | |
3039 | 328 HTTP_field_t *field; |
329 int i = 0; | |
902 | 330 if( http_hdr==NULL ) return; |
870 | 331 |
332 printf("protocol: %s\n", http_hdr->protocol ); | |
333 printf("http minor version: %d\n", http_hdr->http_minor_version ); | |
334 printf("uri: %s\n", http_hdr->uri ); | |
335 printf("method: %s\n", http_hdr->method ); | |
336 printf("status code: %d\n", http_hdr->status_code ); | |
337 printf("reason phrase: %s\n", http_hdr->reason_phrase ); | |
338 | |
339 printf("Fields:\n"); | |
3039 | 340 field = http_hdr->first_field; |
341 while( field!=NULL ) { | |
342 printf(" %d - %s\n", i++, field->field_name ); | |
343 field = field->next; | |
344 } | |
870 | 345 } |