Mercurial > mplayer.hg
annotate libmpdemux/http.c @ 6539:79b536a37e40
better planar support, chroma subsampling support and Y8/Y800 support
author | alex |
---|---|
date | Sun, 23 Jun 2002 21:08:31 +0000 |
parents | 37b0b3302395 |
children | 0d7942100437 |
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" |
5915 | 13 #include "mp_msg.h" |
870 | 14 |
15 HTTP_header_t * | |
16 http_new_header() { | |
17 HTTP_header_t *http_hdr; | |
18 | |
19 http_hdr = (HTTP_header_t*)malloc(sizeof(HTTP_header_t)); | |
20 if( http_hdr==NULL ) return NULL; | |
21 memset( http_hdr, 0, sizeof(HTTP_header_t) ); | |
22 | |
23 return http_hdr; | |
24 } | |
25 | |
26 void | |
27 http_free( HTTP_header_t *http_hdr ) { | |
3039 | 28 HTTP_field_t *field, *field2free; |
870 | 29 if( http_hdr==NULL ) return; |
30 if( http_hdr->protocol!=NULL ) free( http_hdr->protocol ); | |
31 if( http_hdr->uri!=NULL ) free( http_hdr->uri ); | |
32 if( http_hdr->reason_phrase!=NULL ) free( http_hdr->reason_phrase ); | |
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 ) { | |
5915 | 52 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
902 | 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 ) { |
5915 | 90 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. No space separator found.\n"); |
902 | 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 ) { | |
5915 | 96 mp_msg(MSGT_NETWORK,MSGL_FATAL,"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 ) { | |
5915 | 103 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get HTTP minor version.\n"); |
902 | 104 return -1; |
870 | 105 } |
106 } | |
107 | |
108 // Get the status code | |
109 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) { | |
5915 | 110 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get status code.\n"); |
902 | 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 ) { |
5915 | 118 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get the reason phrase.\n"); |
902 | 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 ) { | |
5915 | 124 mp_msg(MSGT_NETWORK,MSGL_FATAL,"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 ) { |
5915 | 138 mp_msg(MSGT_NETWORK,MSGL_ERR,"Header may be incomplete. No CRLF CRLF found.\n"); |
3514
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 ) { | |
5915 | 153 mp_msg(MSGT_NETWORK,MSGL_ERR,"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! |
6465 | 166 http_hdr->body = http_hdr->buffer+pos_hdr_sep+4; |
167 http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+4); | |
870 | 168 } |
169 | |
902 | 170 http_hdr->is_parsed = 1; |
171 return 0; | |
870 | 172 } |
173 | |
174 char * | |
902 | 175 http_build_request( HTTP_header_t *http_hdr ) { |
3497 | 176 char *ptr, *uri=NULL; |
902 | 177 int len; |
3039 | 178 HTTP_field_t *field; |
870 | 179 if( http_hdr==NULL ) return NULL; |
180 | |
181 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET"); | |
182 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/"); | |
3497 | 183 else { |
184 uri = (char*)malloc(strlen(http_hdr->uri)*2); | |
185 if( uri==NULL ) { | |
5915 | 186 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
3497 | 187 return NULL; |
188 } | |
189 url_escape_string( uri, http_hdr->uri ); | |
190 } | |
870 | 191 |
3497 | 192 //**** Compute the request length |
193 // Add the Method line | |
194 len = strlen(http_hdr->method)+strlen(uri)+12; | |
195 // Add the fields | |
196 field = http_hdr->first_field; | |
3039 | 197 while( field!=NULL ) { |
198 len += strlen(field->field_name)+2; | |
199 field = field->next; | |
200 } | |
3497 | 201 // Add the CRLF |
202 len += 2; | |
203 // Add the body | |
902 | 204 if( http_hdr->body!=NULL ) { |
205 len += http_hdr->body_size; | |
206 } | |
3497 | 207 // Free the buffer if it was previously used |
902 | 208 if( http_hdr->buffer!=NULL ) { |
209 free( http_hdr->buffer ); | |
210 http_hdr->buffer = NULL; | |
211 } | |
3497 | 212 http_hdr->buffer = (char*)malloc(len+1); |
902 | 213 if( http_hdr->buffer==NULL ) { |
5915 | 214 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
902 | 215 return NULL; |
216 } | |
217 http_hdr->buffer_size = len; | |
218 | |
3497 | 219 //*** Building the request |
902 | 220 ptr = http_hdr->buffer; |
3497 | 221 // Add the method line |
222 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version ); | |
3039 | 223 field = http_hdr->first_field; |
3497 | 224 // Add the field |
3039 | 225 while( field!=NULL ) { |
226 ptr += sprintf( ptr, "%s\r\n", field->field_name ); | |
227 field = field->next; | |
228 } | |
870 | 229 ptr += sprintf( ptr, "\r\n" ); |
3497 | 230 // Add the body |
870 | 231 if( http_hdr->body!=NULL ) { |
232 memcpy( ptr, http_hdr->body, http_hdr->body_size ); | |
233 } | |
3497 | 234 |
235 if( uri ) free( uri ); | |
902 | 236 return http_hdr->buffer; |
870 | 237 } |
238 | |
239 char * | |
240 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) { | |
241 if( http_hdr==NULL || field_name==NULL ) return NULL; | |
3039 | 242 http_hdr->field_search_pos = http_hdr->first_field; |
243 http_hdr->field_search = (char*)realloc( http_hdr->field_search, strlen(field_name)+1 ); | |
870 | 244 if( http_hdr->field_search==NULL ) { |
5915 | 245 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 246 return NULL; |
247 } | |
248 strcpy( http_hdr->field_search, field_name ); | |
249 return http_get_next_field( http_hdr ); | |
250 } | |
251 | |
252 char * | |
253 http_get_next_field( HTTP_header_t *http_hdr ) { | |
254 char *ptr; | |
3039 | 255 HTTP_field_t *field; |
870 | 256 if( http_hdr==NULL ) return NULL; |
257 | |
3039 | 258 field = http_hdr->field_search_pos; |
259 while( field!=NULL ) { | |
260 ptr = strstr( field->field_name, ":" ); | |
870 | 261 if( ptr==NULL ) return NULL; |
3039 | 262 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) { |
870 | 263 ptr++; // Skip the column |
264 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some | |
3039 | 265 http_hdr->field_search_pos = field->next; |
870 | 266 return ptr; // return the value without the field name |
267 } | |
3039 | 268 field = field->next; |
870 | 269 } |
270 return NULL; | |
271 } | |
272 | |
273 void | |
3039 | 274 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) { |
275 HTTP_field_t *new_field; | |
276 if( http_hdr==NULL || field_name==NULL ) return; | |
870 | 277 |
3039 | 278 new_field = (HTTP_field_t*)malloc(sizeof(HTTP_field_t)); |
279 if( new_field==NULL ) { | |
5915 | 280 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 281 return; |
282 } | |
3039 | 283 new_field->next = NULL; |
284 new_field->field_name = (char*)malloc(strlen(field_name)+1); | |
285 if( new_field->field_name==NULL ) { | |
5915 | 286 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
3039 | 287 return; |
288 } | |
289 strcpy( new_field->field_name, field_name ); | |
290 | |
291 if( http_hdr->last_field==NULL ) { | |
292 http_hdr->first_field = new_field; | |
293 } else { | |
294 http_hdr->last_field->next = new_field; | |
295 } | |
296 http_hdr->last_field = new_field; | |
870 | 297 http_hdr->field_nb++; |
298 } | |
299 | |
300 void | |
301 http_set_method( HTTP_header_t *http_hdr, const char *method ) { | |
302 if( http_hdr==NULL || method==NULL ) return; | |
303 | |
304 http_hdr->method = (char*)malloc(strlen(method)+1); | |
305 if( http_hdr->method==NULL ) { | |
5915 | 306 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 307 return; |
308 } | |
309 strcpy( http_hdr->method, method ); | |
310 } | |
311 | |
312 void | |
313 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) { | |
314 if( http_hdr==NULL || uri==NULL ) return; | |
315 | |
316 http_hdr->uri = (char*)malloc(strlen(uri)+1); | |
317 if( http_hdr->uri==NULL ) { | |
5915 | 318 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 319 return; |
320 } | |
321 strcpy( http_hdr->uri, uri ); | |
322 } | |
323 | |
6514 | 324 int |
325 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) { | |
326 char *auth, *usr_pass, *b64_usr_pass; | |
327 int encoded_len, pass_len=0, out_len; | |
328 if( http_hdr==NULL || username==NULL ) return -1; | |
329 | |
330 if( password!=NULL ) { | |
331 pass_len = strlen(password); | |
332 } | |
333 | |
334 usr_pass = (char*)malloc(strlen(username)+pass_len+2); | |
335 if( usr_pass==NULL ) { | |
336 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
337 return -1; | |
338 } | |
339 | |
340 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password ); | |
341 | |
342 // Base 64 encode with at least 33% more data than the original size | |
343 encoded_len = strlen(usr_pass)*2; | |
344 b64_usr_pass = (char*)malloc(encoded_len); | |
345 if( b64_usr_pass==NULL ) { | |
346 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
347 return -1; | |
348 } | |
349 | |
350 out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len); | |
351 if( out_len<0 ) { | |
352 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n"); | |
353 return -1; | |
354 } | |
355 | |
356 b64_usr_pass[out_len]='\0'; | |
357 | |
358 auth = (char*)malloc(encoded_len+22); | |
359 if( auth==NULL ) { | |
360 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
361 return -1; | |
362 } | |
363 | |
364 sprintf( auth, "Authorization: Basic %s", b64_usr_pass); | |
365 http_set_field( http_hdr, auth ); | |
366 | |
367 free( usr_pass ); | |
368 free( b64_usr_pass ); | |
369 free( auth ); | |
370 | |
371 return 0; | |
372 } | |
373 | |
870 | 374 void |
375 http_debug_hdr( HTTP_header_t *http_hdr ) { | |
3039 | 376 HTTP_field_t *field; |
377 int i = 0; | |
902 | 378 if( http_hdr==NULL ) return; |
870 | 379 |
5915 | 380 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n"); |
381 mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol ); | |
382 mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version ); | |
383 mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri ); | |
384 mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method ); | |
385 mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code ); | |
386 mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase ); | |
387 mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%d]\n", http_hdr->body_size ); | |
870 | 388 |
5915 | 389 mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n"); |
3039 | 390 field = http_hdr->first_field; |
391 while( field!=NULL ) { | |
5915 | 392 mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name ); |
3039 | 393 field = field->next; |
394 } | |
5915 | 395 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n"); |
870 | 396 } |
6514 | 397 |
398 int | |
399 base64_encode(const void *enc, int encLen, char *out, int outMax) { | |
400 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; | |
401 | |
402 unsigned char *encBuf; | |
403 int outLen; | |
404 unsigned int bits; | |
405 unsigned int shift; | |
406 | |
407 encBuf = (unsigned char*)enc; | |
408 outLen = 0; | |
409 bits = 0; | |
410 shift = 0; | |
411 | |
412 while( outLen<outMax ) { | |
413 if( encLen>0 ) { | |
414 // Shift in byte | |
415 bits <<= 8; | |
416 bits |= *encBuf; | |
417 shift += 8; | |
418 // Next byte | |
419 encBuf++; | |
420 encLen--; | |
421 } else if( shift>0 ) { | |
422 // Pad last bits to 6 bits - will end next loop | |
423 bits <<= 6 - shift; | |
424 shift = 6; | |
425 } else { | |
426 // Terminate with Mime style '=' | |
427 *out = '='; | |
428 outLen++; | |
429 | |
430 return outLen; | |
431 } | |
432 | |
433 // Encode 6 bit segments | |
434 while( shift>=6 ) { | |
435 shift -= 6; | |
436 *out = b64[ (bits >> shift) & 0x3F ]; | |
437 out++; | |
438 outLen++; | |
439 } | |
440 } | |
441 | |
442 // Output overflow | |
443 return -1; | |
444 } | |
445 | |
446 |