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