Mercurial > mplayer.hg
annotate libmpdemux/http.c @ 8141:be9a2636b071
use the more stable NEWTYPE critsect code
author | arpi |
---|---|
date | Sun, 10 Nov 2002 21:04:10 +0000 |
parents | 7da2c2a68547 |
children | 63a5e03f4346 |
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 ) { | |
1027 | 48 if( http_hdr==NULL || response==NULL || length<0 ) return -1; |
7304
7da2c2a68547
Check if realloc failed on http_hdr->buffer instead of ptr in http_response_append,
bertrand
parents:
7293
diff
changeset
|
49 |
7293 | 50 http_hdr->buffer = (char*)realloc( http_hdr->buffer, http_hdr->buffer_size+length+1 ); |
7304
7da2c2a68547
Check if realloc failed on http_hdr->buffer instead of ptr in http_response_append,
bertrand
parents:
7293
diff
changeset
|
51 if( http_hdr->buffer==NULL ) { |
7293 | 52 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory (re)allocation failed\n"); |
902 | 53 return -1; |
54 } | |
7293 | 55 memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length ); |
56 http_hdr->buffer_size += length; | |
57 http_hdr->buffer[http_hdr->buffer_size]=0; // close the string! | |
902 | 58 return http_hdr->buffer_size; |
59 } | |
60 | |
61 int | |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
62 http_is_header_entire( HTTP_header_t *http_hdr ) { |
902 | 63 if( http_hdr==NULL ) return -1; |
7293 | 64 if( http_hdr->buffer==NULL ) return 0; // empty |
65 | |
3784 | 66 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL && |
67 strstr(http_hdr->buffer, "\n\n")==NULL ) return 0; | |
68 return 1; | |
902 | 69 } |
70 | |
71 int | |
72 http_response_parse( HTTP_header_t *http_hdr ) { | |
870 | 73 char *hdr_ptr, *ptr; |
74 char *field=NULL; | |
75 int pos_hdr_sep, len; | |
902 | 76 if( http_hdr==NULL ) return -1; |
77 if( http_hdr->is_parsed ) return 0; | |
870 | 78 |
79 // Get the protocol | |
902 | 80 hdr_ptr = strstr( http_hdr->buffer, " " ); |
870 | 81 if( hdr_ptr==NULL ) { |
5915 | 82 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. No space separator found.\n"); |
902 | 83 return -1; |
870 | 84 } |
902 | 85 len = hdr_ptr-http_hdr->buffer; |
870 | 86 http_hdr->protocol = (char*)malloc(len+1); |
87 if( http_hdr->protocol==NULL ) { | |
5915 | 88 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
902 | 89 return -1; |
870 | 90 } |
902 | 91 strncpy( http_hdr->protocol, http_hdr->buffer, len ); |
92 http_hdr->protocol[len]='\0'; | |
870 | 93 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) { |
94 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) { | |
5915 | 95 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get HTTP minor version.\n"); |
902 | 96 return -1; |
870 | 97 } |
98 } | |
99 | |
100 // Get the status code | |
101 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) { | |
5915 | 102 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get status code.\n"); |
902 | 103 return -1; |
870 | 104 } |
105 hdr_ptr += 4; | |
106 | |
107 // Get the reason phrase | |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
108 ptr = strstr( hdr_ptr, "\n" ); |
870 | 109 if( hdr_ptr==NULL ) { |
5915 | 110 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get the reason phrase.\n"); |
902 | 111 return -1; |
870 | 112 } |
113 len = ptr-hdr_ptr; | |
114 http_hdr->reason_phrase = (char*)malloc(len+1); | |
115 if( http_hdr->reason_phrase==NULL ) { | |
5915 | 116 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
902 | 117 return -1; |
870 | 118 } |
119 strncpy( http_hdr->reason_phrase, hdr_ptr, len ); | |
4311 | 120 if( http_hdr->reason_phrase[len-1]=='\r' ) { |
121 len--; | |
122 } | |
870 | 123 http_hdr->reason_phrase[len]='\0'; |
124 | |
125 // Set the position of the header separator: \r\n\r\n | |
902 | 126 ptr = strstr( http_hdr->buffer, "\r\n\r\n" ); |
870 | 127 if( ptr==NULL ) { |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
128 ptr = strstr( http_hdr->buffer, "\n\n" ); |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
129 if( ptr==NULL ) { |
5915 | 130 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
|
131 return -1; |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
132 } |
870 | 133 } |
902 | 134 pos_hdr_sep = ptr-http_hdr->buffer; |
870 | 135 |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
136 // 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
|
137 hdr_ptr = strstr( http_hdr->buffer, "\n" )+1; |
870 | 138 do { |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
139 ptr = hdr_ptr; |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
140 while( *ptr!='\r' && *ptr!='\n' ) ptr++; |
870 | 141 len = ptr-hdr_ptr; |
4816
f1dea39a50bb
Fixed the http response parser when the http header only has the HTTP
bertrand
parents:
4311
diff
changeset
|
142 if( len==0 ) break; |
870 | 143 field = (char*)realloc(field, len+1); |
144 if( field==NULL ) { | |
5915 | 145 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
902 | 146 return -1; |
870 | 147 } |
148 strncpy( field, hdr_ptr, len ); | |
149 field[len]='\0'; | |
150 http_set_field( http_hdr, field ); | |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
151 hdr_ptr = ptr+((*ptr=='\r')?2:1); |
902 | 152 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) ); |
870 | 153 |
154 if( field!=NULL ) free( field ); | |
155 | |
902 | 156 if( pos_hdr_sep+4<http_hdr->buffer_size ) { |
870 | 157 // Response has data! |
6465 | 158 http_hdr->body = http_hdr->buffer+pos_hdr_sep+4; |
159 http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+4); | |
870 | 160 } |
161 | |
902 | 162 http_hdr->is_parsed = 1; |
163 return 0; | |
870 | 164 } |
165 | |
166 char * | |
902 | 167 http_build_request( HTTP_header_t *http_hdr ) { |
3497 | 168 char *ptr, *uri=NULL; |
902 | 169 int len; |
3039 | 170 HTTP_field_t *field; |
870 | 171 if( http_hdr==NULL ) return NULL; |
172 | |
173 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET"); | |
174 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/"); | |
3497 | 175 else { |
176 uri = (char*)malloc(strlen(http_hdr->uri)*2); | |
177 if( uri==NULL ) { | |
5915 | 178 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
3497 | 179 return NULL; |
180 } | |
181 url_escape_string( uri, http_hdr->uri ); | |
182 } | |
870 | 183 |
3497 | 184 //**** Compute the request length |
185 // Add the Method line | |
186 len = strlen(http_hdr->method)+strlen(uri)+12; | |
187 // Add the fields | |
188 field = http_hdr->first_field; | |
3039 | 189 while( field!=NULL ) { |
190 len += strlen(field->field_name)+2; | |
191 field = field->next; | |
192 } | |
3497 | 193 // Add the CRLF |
194 len += 2; | |
195 // Add the body | |
902 | 196 if( http_hdr->body!=NULL ) { |
197 len += http_hdr->body_size; | |
198 } | |
3497 | 199 // Free the buffer if it was previously used |
902 | 200 if( http_hdr->buffer!=NULL ) { |
201 free( http_hdr->buffer ); | |
202 http_hdr->buffer = NULL; | |
203 } | |
3497 | 204 http_hdr->buffer = (char*)malloc(len+1); |
902 | 205 if( http_hdr->buffer==NULL ) { |
5915 | 206 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
902 | 207 return NULL; |
208 } | |
209 http_hdr->buffer_size = len; | |
210 | |
3497 | 211 //*** Building the request |
902 | 212 ptr = http_hdr->buffer; |
3497 | 213 // Add the method line |
214 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version ); | |
3039 | 215 field = http_hdr->first_field; |
3497 | 216 // Add the field |
3039 | 217 while( field!=NULL ) { |
218 ptr += sprintf( ptr, "%s\r\n", field->field_name ); | |
219 field = field->next; | |
220 } | |
870 | 221 ptr += sprintf( ptr, "\r\n" ); |
3497 | 222 // Add the body |
870 | 223 if( http_hdr->body!=NULL ) { |
224 memcpy( ptr, http_hdr->body, http_hdr->body_size ); | |
225 } | |
3497 | 226 |
227 if( uri ) free( uri ); | |
902 | 228 return http_hdr->buffer; |
870 | 229 } |
230 | |
231 char * | |
232 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) { | |
233 if( http_hdr==NULL || field_name==NULL ) return NULL; | |
3039 | 234 http_hdr->field_search_pos = http_hdr->first_field; |
235 http_hdr->field_search = (char*)realloc( http_hdr->field_search, strlen(field_name)+1 ); | |
870 | 236 if( http_hdr->field_search==NULL ) { |
5915 | 237 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 238 return NULL; |
239 } | |
240 strcpy( http_hdr->field_search, field_name ); | |
241 return http_get_next_field( http_hdr ); | |
242 } | |
243 | |
244 char * | |
245 http_get_next_field( HTTP_header_t *http_hdr ) { | |
246 char *ptr; | |
3039 | 247 HTTP_field_t *field; |
870 | 248 if( http_hdr==NULL ) return NULL; |
249 | |
3039 | 250 field = http_hdr->field_search_pos; |
251 while( field!=NULL ) { | |
252 ptr = strstr( field->field_name, ":" ); | |
870 | 253 if( ptr==NULL ) return NULL; |
3039 | 254 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) { |
870 | 255 ptr++; // Skip the column |
256 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some | |
3039 | 257 http_hdr->field_search_pos = field->next; |
870 | 258 return ptr; // return the value without the field name |
259 } | |
3039 | 260 field = field->next; |
870 | 261 } |
262 return NULL; | |
263 } | |
264 | |
265 void | |
3039 | 266 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) { |
267 HTTP_field_t *new_field; | |
268 if( http_hdr==NULL || field_name==NULL ) return; | |
870 | 269 |
3039 | 270 new_field = (HTTP_field_t*)malloc(sizeof(HTTP_field_t)); |
271 if( new_field==NULL ) { | |
5915 | 272 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 273 return; |
274 } | |
3039 | 275 new_field->next = NULL; |
276 new_field->field_name = (char*)malloc(strlen(field_name)+1); | |
277 if( new_field->field_name==NULL ) { | |
5915 | 278 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
3039 | 279 return; |
280 } | |
281 strcpy( new_field->field_name, field_name ); | |
282 | |
283 if( http_hdr->last_field==NULL ) { | |
284 http_hdr->first_field = new_field; | |
285 } else { | |
286 http_hdr->last_field->next = new_field; | |
287 } | |
288 http_hdr->last_field = new_field; | |
870 | 289 http_hdr->field_nb++; |
290 } | |
291 | |
292 void | |
293 http_set_method( HTTP_header_t *http_hdr, const char *method ) { | |
294 if( http_hdr==NULL || method==NULL ) return; | |
295 | |
296 http_hdr->method = (char*)malloc(strlen(method)+1); | |
297 if( http_hdr->method==NULL ) { | |
5915 | 298 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 299 return; |
300 } | |
301 strcpy( http_hdr->method, method ); | |
302 } | |
303 | |
304 void | |
305 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) { | |
306 if( http_hdr==NULL || uri==NULL ) return; | |
307 | |
308 http_hdr->uri = (char*)malloc(strlen(uri)+1); | |
309 if( http_hdr->uri==NULL ) { | |
5915 | 310 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 311 return; |
312 } | |
313 strcpy( http_hdr->uri, uri ); | |
314 } | |
315 | |
6514 | 316 int |
317 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) { | |
318 char *auth, *usr_pass, *b64_usr_pass; | |
319 int encoded_len, pass_len=0, out_len; | |
320 if( http_hdr==NULL || username==NULL ) return -1; | |
321 | |
322 if( password!=NULL ) { | |
323 pass_len = strlen(password); | |
324 } | |
325 | |
326 usr_pass = (char*)malloc(strlen(username)+pass_len+2); | |
327 if( usr_pass==NULL ) { | |
328 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
329 return -1; | |
330 } | |
331 | |
332 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password ); | |
333 | |
334 // Base 64 encode with at least 33% more data than the original size | |
335 encoded_len = strlen(usr_pass)*2; | |
336 b64_usr_pass = (char*)malloc(encoded_len); | |
337 if( b64_usr_pass==NULL ) { | |
338 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
339 return -1; | |
340 } | |
341 | |
342 out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len); | |
343 if( out_len<0 ) { | |
344 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n"); | |
345 return -1; | |
346 } | |
347 | |
348 b64_usr_pass[out_len]='\0'; | |
349 | |
350 auth = (char*)malloc(encoded_len+22); | |
351 if( auth==NULL ) { | |
352 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
353 return -1; | |
354 } | |
355 | |
356 sprintf( auth, "Authorization: Basic %s", b64_usr_pass); | |
357 http_set_field( http_hdr, auth ); | |
358 | |
359 free( usr_pass ); | |
360 free( b64_usr_pass ); | |
361 free( auth ); | |
362 | |
363 return 0; | |
364 } | |
365 | |
870 | 366 void |
367 http_debug_hdr( HTTP_header_t *http_hdr ) { | |
3039 | 368 HTTP_field_t *field; |
369 int i = 0; | |
902 | 370 if( http_hdr==NULL ) return; |
870 | 371 |
5915 | 372 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n"); |
373 mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol ); | |
374 mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version ); | |
375 mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri ); | |
376 mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method ); | |
377 mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code ); | |
378 mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase ); | |
379 mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%d]\n", http_hdr->body_size ); | |
870 | 380 |
5915 | 381 mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n"); |
3039 | 382 field = http_hdr->first_field; |
383 while( field!=NULL ) { | |
5915 | 384 mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name ); |
3039 | 385 field = field->next; |
386 } | |
5915 | 387 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n"); |
870 | 388 } |
6514 | 389 |
390 int | |
391 base64_encode(const void *enc, int encLen, char *out, int outMax) { | |
392 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; | |
393 | |
394 unsigned char *encBuf; | |
395 int outLen; | |
396 unsigned int bits; | |
397 unsigned int shift; | |
398 | |
399 encBuf = (unsigned char*)enc; | |
400 outLen = 0; | |
401 bits = 0; | |
402 shift = 0; | |
403 | |
404 while( outLen<outMax ) { | |
405 if( encLen>0 ) { | |
406 // Shift in byte | |
407 bits <<= 8; | |
408 bits |= *encBuf; | |
409 shift += 8; | |
410 // Next byte | |
411 encBuf++; | |
412 encLen--; | |
413 } else if( shift>0 ) { | |
414 // Pad last bits to 6 bits - will end next loop | |
415 bits <<= 6 - shift; | |
416 shift = 6; | |
417 } else { | |
418 // Terminate with Mime style '=' | |
419 *out = '='; | |
420 outLen++; | |
421 | |
422 return outLen; | |
423 } | |
424 | |
425 // Encode 6 bit segments | |
426 while( shift>=6 ) { | |
427 shift -= 6; | |
428 *out = b64[ (bits >> shift) & 0x3F ]; | |
429 out++; | |
430 outLen++; | |
431 } | |
432 } | |
433 | |
434 // Output overflow | |
435 return -1; | |
436 } | |
437 | |
438 |