Mercurial > mplayer.hg
annotate libmpdemux/http.c @ 15918:8f6c98310f38
initial translation, synced with 1.12
author | gabrov |
---|---|
date | Mon, 04 Jul 2005 18:29:48 +0000 |
parents | 52edb32f0c2a |
children | 4ee24ec6ac16 |
rev | line source |
---|---|
902 | 1 /* |
2 * HTTP Helper | |
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com> | |
4 * (C) 2001, MPlayer team. | |
5 */ | |
6 | |
15614
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
7 #include "config.h" |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
8 |
870 | 9 #include <stdio.h> |
10 #include <stdlib.h> | |
11 #include <string.h> | |
15585 | 12 #include <unistd.h> |
870 | 13 |
15614
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
14 #ifndef HAVE_WINSOCK2 |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
15 #define closesocket close |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
16 #else |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
17 #include <winsock2.h> |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
18 #include <ws2tcpip.h> |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
19 #endif |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
20 |
870 | 21 #include "http.h" |
4816
f1dea39a50bb
Fixed the http response parser when the http header only has the HTTP
bertrand
parents:
4311
diff
changeset
|
22 #include "url.h" |
5915 | 23 #include "mp_msg.h" |
870 | 24 |
15585 | 25 #include "stream.h" |
26 #include "demuxer.h" | |
27 #include "network.h" | |
28 #include "help_mp.h" | |
29 | |
30 | |
31 extern mime_struct_t mime_type_table[]; | |
32 extern int stream_cache_size; | |
33 extern int network_bandwidth; | |
34 | |
35 extern int http_seek(stream_t *stream, off_t pos); | |
36 | |
37 static int nop_streaming_start( stream_t *stream ) { | |
38 HTTP_header_t *http_hdr = NULL; | |
39 char *next_url=NULL; | |
40 URL_t *rd_url=NULL; | |
41 int fd,ret; | |
42 if( stream==NULL ) return -1; | |
43 | |
44 fd = stream->fd; | |
45 if( fd<0 ) { | |
46 fd = http_send_request( stream->streaming_ctrl->url, 0 ); | |
47 if( fd<0 ) return -1; | |
48 http_hdr = http_read_response( fd ); | |
49 if( http_hdr==NULL ) return -1; | |
50 | |
51 switch( http_hdr->status_code ) { | |
52 case 200: // OK | |
53 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") ); | |
54 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); | |
55 if( http_hdr->body_size>0 ) { | |
56 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { | |
57 http_free( http_hdr ); | |
58 return -1; | |
59 } | |
60 } | |
61 break; | |
62 // Redirect | |
63 case 301: // Permanently | |
64 case 302: // Temporarily | |
65 ret=-1; | |
66 next_url = http_get_field( http_hdr, "Location" ); | |
67 | |
68 if (next_url != NULL) | |
69 rd_url=url_new(next_url); | |
70 | |
71 if (next_url != NULL && rd_url != NULL) { | |
72 mp_msg(MSGT_NETWORK,MSGL_STATUS,"Redirected: Using this url instead %s\n",next_url); | |
73 stream->streaming_ctrl->url=check4proxies(rd_url); | |
74 ret=nop_streaming_start(stream); //recursively get streaming started | |
75 } else { | |
76 mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n"); | |
77 closesocket( fd ); | |
78 fd = -1; | |
79 } | |
80 return ret; | |
81 break; | |
82 case 401: //Authorization required | |
83 case 403: //Forbidden | |
84 case 404: //Not found | |
85 case 500: //Server Error | |
86 default: | |
87 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); | |
88 closesocket( fd ); | |
89 fd = -1; | |
90 return -1; | |
91 break; | |
92 } | |
93 stream->fd = fd; | |
94 } else { | |
95 http_hdr = (HTTP_header_t*)stream->streaming_ctrl->data; | |
96 if( http_hdr->body_size>0 ) { | |
97 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { | |
98 http_free( http_hdr ); | |
99 stream->streaming_ctrl->data = NULL; | |
100 return -1; | |
101 } | |
102 } | |
103 } | |
104 | |
105 if( http_hdr ) { | |
106 http_free( http_hdr ); | |
107 stream->streaming_ctrl->data = NULL; | |
108 } | |
109 | |
110 stream->streaming_ctrl->streaming_read = nop_streaming_read; | |
111 stream->streaming_ctrl->streaming_seek = nop_streaming_seek; | |
112 stream->streaming_ctrl->prebuffer_size = 64*1024; // 64 KBytes | |
113 stream->streaming_ctrl->buffering = 1; | |
114 stream->streaming_ctrl->status = streaming_playing_e; | |
115 return 0; | |
116 } | |
117 | |
870 | 118 HTTP_header_t * |
119 http_new_header() { | |
120 HTTP_header_t *http_hdr; | |
121 | |
122 http_hdr = (HTTP_header_t*)malloc(sizeof(HTTP_header_t)); | |
123 if( http_hdr==NULL ) return NULL; | |
124 memset( http_hdr, 0, sizeof(HTTP_header_t) ); | |
125 | |
126 return http_hdr; | |
127 } | |
128 | |
129 void | |
130 http_free( HTTP_header_t *http_hdr ) { | |
3039 | 131 HTTP_field_t *field, *field2free; |
870 | 132 if( http_hdr==NULL ) return; |
133 if( http_hdr->protocol!=NULL ) free( http_hdr->protocol ); | |
134 if( http_hdr->uri!=NULL ) free( http_hdr->uri ); | |
135 if( http_hdr->reason_phrase!=NULL ) free( http_hdr->reason_phrase ); | |
136 if( http_hdr->field_search!=NULL ) free( http_hdr->field_search ); | |
902 | 137 if( http_hdr->method!=NULL ) free( http_hdr->method ); |
138 if( http_hdr->buffer!=NULL ) free( http_hdr->buffer ); | |
3039 | 139 field = http_hdr->first_field; |
140 while( field!=NULL ) { | |
141 field2free = field; | |
14460 | 142 if (field->field_name) |
143 free(field->field_name); | |
3039 | 144 field = field->next; |
145 free( field2free ); | |
146 } | |
870 | 147 free( http_hdr ); |
3039 | 148 http_hdr = NULL; |
870 | 149 } |
150 | |
902 | 151 int |
152 http_response_append( HTTP_header_t *http_hdr, char *response, int length ) { | |
1027 | 153 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
|
154 |
7293 | 155 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
|
156 if( http_hdr->buffer==NULL ) { |
7293 | 157 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory (re)allocation failed\n"); |
902 | 158 return -1; |
159 } | |
7293 | 160 memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length ); |
161 http_hdr->buffer_size += length; | |
162 http_hdr->buffer[http_hdr->buffer_size]=0; // close the string! | |
902 | 163 return http_hdr->buffer_size; |
164 } | |
165 | |
166 int | |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
167 http_is_header_entire( HTTP_header_t *http_hdr ) { |
902 | 168 if( http_hdr==NULL ) return -1; |
7293 | 169 if( http_hdr->buffer==NULL ) return 0; // empty |
170 | |
3784 | 171 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL && |
172 strstr(http_hdr->buffer, "\n\n")==NULL ) return 0; | |
173 return 1; | |
902 | 174 } |
175 | |
176 int | |
177 http_response_parse( HTTP_header_t *http_hdr ) { | |
870 | 178 char *hdr_ptr, *ptr; |
179 char *field=NULL; | |
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
180 int pos_hdr_sep, hdr_sep_len, len; |
902 | 181 if( http_hdr==NULL ) return -1; |
182 if( http_hdr->is_parsed ) return 0; | |
870 | 183 |
184 // Get the protocol | |
902 | 185 hdr_ptr = strstr( http_hdr->buffer, " " ); |
870 | 186 if( hdr_ptr==NULL ) { |
5915 | 187 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. No space separator found.\n"); |
902 | 188 return -1; |
870 | 189 } |
902 | 190 len = hdr_ptr-http_hdr->buffer; |
870 | 191 http_hdr->protocol = (char*)malloc(len+1); |
192 if( http_hdr->protocol==NULL ) { | |
5915 | 193 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
902 | 194 return -1; |
870 | 195 } |
902 | 196 strncpy( http_hdr->protocol, http_hdr->buffer, len ); |
197 http_hdr->protocol[len]='\0'; | |
870 | 198 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) { |
199 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) { | |
5915 | 200 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get HTTP minor version.\n"); |
902 | 201 return -1; |
870 | 202 } |
203 } | |
204 | |
205 // Get the status code | |
206 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) { | |
5915 | 207 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get status code.\n"); |
902 | 208 return -1; |
870 | 209 } |
210 hdr_ptr += 4; | |
211 | |
212 // Get the reason phrase | |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
213 ptr = strstr( hdr_ptr, "\n" ); |
870 | 214 if( hdr_ptr==NULL ) { |
5915 | 215 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get the reason phrase.\n"); |
902 | 216 return -1; |
870 | 217 } |
218 len = ptr-hdr_ptr; | |
219 http_hdr->reason_phrase = (char*)malloc(len+1); | |
220 if( http_hdr->reason_phrase==NULL ) { | |
5915 | 221 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
902 | 222 return -1; |
870 | 223 } |
224 strncpy( http_hdr->reason_phrase, hdr_ptr, len ); | |
4311 | 225 if( http_hdr->reason_phrase[len-1]=='\r' ) { |
226 len--; | |
227 } | |
870 | 228 http_hdr->reason_phrase[len]='\0'; |
229 | |
230 // Set the position of the header separator: \r\n\r\n | |
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
231 hdr_sep_len = 4; |
902 | 232 ptr = strstr( http_hdr->buffer, "\r\n\r\n" ); |
870 | 233 if( ptr==NULL ) { |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
234 ptr = strstr( http_hdr->buffer, "\n\n" ); |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
235 if( ptr==NULL ) { |
5915 | 236 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
|
237 return -1; |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
238 } |
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
239 hdr_sep_len = 2; |
870 | 240 } |
902 | 241 pos_hdr_sep = ptr-http_hdr->buffer; |
870 | 242 |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
243 // 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
|
244 hdr_ptr = strstr( http_hdr->buffer, "\n" )+1; |
870 | 245 do { |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
246 ptr = hdr_ptr; |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
247 while( *ptr!='\r' && *ptr!='\n' ) ptr++; |
870 | 248 len = ptr-hdr_ptr; |
4816
f1dea39a50bb
Fixed the http response parser when the http header only has the HTTP
bertrand
parents:
4311
diff
changeset
|
249 if( len==0 ) break; |
870 | 250 field = (char*)realloc(field, len+1); |
251 if( field==NULL ) { | |
5915 | 252 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
902 | 253 return -1; |
870 | 254 } |
255 strncpy( field, hdr_ptr, len ); | |
256 field[len]='\0'; | |
257 http_set_field( http_hdr, field ); | |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
258 hdr_ptr = ptr+((*ptr=='\r')?2:1); |
902 | 259 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) ); |
870 | 260 |
261 if( field!=NULL ) free( field ); | |
262 | |
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
263 if( pos_hdr_sep+hdr_sep_len<http_hdr->buffer_size ) { |
870 | 264 // Response has data! |
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
265 http_hdr->body = http_hdr->buffer+pos_hdr_sep+hdr_sep_len; |
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
266 http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+hdr_sep_len); |
870 | 267 } |
268 | |
902 | 269 http_hdr->is_parsed = 1; |
270 return 0; | |
870 | 271 } |
272 | |
273 char * | |
902 | 274 http_build_request( HTTP_header_t *http_hdr ) { |
3497 | 275 char *ptr, *uri=NULL; |
902 | 276 int len; |
3039 | 277 HTTP_field_t *field; |
870 | 278 if( http_hdr==NULL ) return NULL; |
279 | |
280 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET"); | |
281 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/"); | |
3497 | 282 else { |
12391 | 283 uri = (char*)malloc(strlen(http_hdr->uri) + 1); |
3497 | 284 if( uri==NULL ) { |
5915 | 285 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
3497 | 286 return NULL; |
287 } | |
12391 | 288 strcpy(uri,http_hdr->uri); |
3497 | 289 } |
870 | 290 |
3497 | 291 //**** Compute the request length |
292 // Add the Method line | |
293 len = strlen(http_hdr->method)+strlen(uri)+12; | |
294 // Add the fields | |
295 field = http_hdr->first_field; | |
3039 | 296 while( field!=NULL ) { |
297 len += strlen(field->field_name)+2; | |
298 field = field->next; | |
299 } | |
3497 | 300 // Add the CRLF |
301 len += 2; | |
302 // Add the body | |
902 | 303 if( http_hdr->body!=NULL ) { |
304 len += http_hdr->body_size; | |
305 } | |
3497 | 306 // Free the buffer if it was previously used |
902 | 307 if( http_hdr->buffer!=NULL ) { |
308 free( http_hdr->buffer ); | |
309 http_hdr->buffer = NULL; | |
310 } | |
3497 | 311 http_hdr->buffer = (char*)malloc(len+1); |
902 | 312 if( http_hdr->buffer==NULL ) { |
5915 | 313 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
902 | 314 return NULL; |
315 } | |
316 http_hdr->buffer_size = len; | |
317 | |
3497 | 318 //*** Building the request |
902 | 319 ptr = http_hdr->buffer; |
3497 | 320 // Add the method line |
321 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version ); | |
3039 | 322 field = http_hdr->first_field; |
3497 | 323 // Add the field |
3039 | 324 while( field!=NULL ) { |
325 ptr += sprintf( ptr, "%s\r\n", field->field_name ); | |
326 field = field->next; | |
327 } | |
870 | 328 ptr += sprintf( ptr, "\r\n" ); |
3497 | 329 // Add the body |
870 | 330 if( http_hdr->body!=NULL ) { |
331 memcpy( ptr, http_hdr->body, http_hdr->body_size ); | |
332 } | |
3497 | 333 |
334 if( uri ) free( uri ); | |
902 | 335 return http_hdr->buffer; |
870 | 336 } |
337 | |
338 char * | |
339 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) { | |
340 if( http_hdr==NULL || field_name==NULL ) return NULL; | |
3039 | 341 http_hdr->field_search_pos = http_hdr->first_field; |
342 http_hdr->field_search = (char*)realloc( http_hdr->field_search, strlen(field_name)+1 ); | |
870 | 343 if( http_hdr->field_search==NULL ) { |
5915 | 344 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 345 return NULL; |
346 } | |
347 strcpy( http_hdr->field_search, field_name ); | |
348 return http_get_next_field( http_hdr ); | |
349 } | |
350 | |
351 char * | |
352 http_get_next_field( HTTP_header_t *http_hdr ) { | |
353 char *ptr; | |
3039 | 354 HTTP_field_t *field; |
870 | 355 if( http_hdr==NULL ) return NULL; |
356 | |
3039 | 357 field = http_hdr->field_search_pos; |
358 while( field!=NULL ) { | |
359 ptr = strstr( field->field_name, ":" ); | |
870 | 360 if( ptr==NULL ) return NULL; |
3039 | 361 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) { |
870 | 362 ptr++; // Skip the column |
363 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some | |
3039 | 364 http_hdr->field_search_pos = field->next; |
870 | 365 return ptr; // return the value without the field name |
366 } | |
3039 | 367 field = field->next; |
870 | 368 } |
369 return NULL; | |
370 } | |
371 | |
372 void | |
3039 | 373 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) { |
374 HTTP_field_t *new_field; | |
375 if( http_hdr==NULL || field_name==NULL ) return; | |
870 | 376 |
3039 | 377 new_field = (HTTP_field_t*)malloc(sizeof(HTTP_field_t)); |
378 if( new_field==NULL ) { | |
5915 | 379 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 380 return; |
381 } | |
3039 | 382 new_field->next = NULL; |
383 new_field->field_name = (char*)malloc(strlen(field_name)+1); | |
384 if( new_field->field_name==NULL ) { | |
5915 | 385 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
3039 | 386 return; |
387 } | |
388 strcpy( new_field->field_name, field_name ); | |
389 | |
390 if( http_hdr->last_field==NULL ) { | |
391 http_hdr->first_field = new_field; | |
392 } else { | |
393 http_hdr->last_field->next = new_field; | |
394 } | |
395 http_hdr->last_field = new_field; | |
870 | 396 http_hdr->field_nb++; |
397 } | |
398 | |
399 void | |
400 http_set_method( HTTP_header_t *http_hdr, const char *method ) { | |
401 if( http_hdr==NULL || method==NULL ) return; | |
402 | |
403 http_hdr->method = (char*)malloc(strlen(method)+1); | |
404 if( http_hdr->method==NULL ) { | |
5915 | 405 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 406 return; |
407 } | |
408 strcpy( http_hdr->method, method ); | |
409 } | |
410 | |
411 void | |
412 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) { | |
413 if( http_hdr==NULL || uri==NULL ) return; | |
414 | |
415 http_hdr->uri = (char*)malloc(strlen(uri)+1); | |
416 if( http_hdr->uri==NULL ) { | |
5915 | 417 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 418 return; |
419 } | |
420 strcpy( http_hdr->uri, uri ); | |
421 } | |
422 | |
6514 | 423 int |
424 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) { | |
425 char *auth, *usr_pass, *b64_usr_pass; | |
426 int encoded_len, pass_len=0, out_len; | |
427 if( http_hdr==NULL || username==NULL ) return -1; | |
428 | |
429 if( password!=NULL ) { | |
430 pass_len = strlen(password); | |
431 } | |
432 | |
433 usr_pass = (char*)malloc(strlen(username)+pass_len+2); | |
434 if( usr_pass==NULL ) { | |
435 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
436 return -1; | |
437 } | |
438 | |
439 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password ); | |
440 | |
441 // Base 64 encode with at least 33% more data than the original size | |
442 encoded_len = strlen(usr_pass)*2; | |
443 b64_usr_pass = (char*)malloc(encoded_len); | |
444 if( b64_usr_pass==NULL ) { | |
445 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
446 return -1; | |
447 } | |
448 | |
449 out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len); | |
450 if( out_len<0 ) { | |
451 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n"); | |
452 return -1; | |
453 } | |
454 | |
455 b64_usr_pass[out_len]='\0'; | |
456 | |
457 auth = (char*)malloc(encoded_len+22); | |
458 if( auth==NULL ) { | |
459 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
460 return -1; | |
461 } | |
462 | |
463 sprintf( auth, "Authorization: Basic %s", b64_usr_pass); | |
464 http_set_field( http_hdr, auth ); | |
465 | |
466 free( usr_pass ); | |
467 free( b64_usr_pass ); | |
468 free( auth ); | |
469 | |
470 return 0; | |
471 } | |
472 | |
870 | 473 void |
474 http_debug_hdr( HTTP_header_t *http_hdr ) { | |
3039 | 475 HTTP_field_t *field; |
476 int i = 0; | |
902 | 477 if( http_hdr==NULL ) return; |
870 | 478 |
5915 | 479 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n"); |
480 mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol ); | |
481 mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version ); | |
482 mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri ); | |
483 mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method ); | |
484 mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code ); | |
485 mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase ); | |
486 mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%d]\n", http_hdr->body_size ); | |
870 | 487 |
5915 | 488 mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n"); |
3039 | 489 field = http_hdr->first_field; |
490 while( field!=NULL ) { | |
5915 | 491 mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name ); |
3039 | 492 field = field->next; |
493 } | |
5915 | 494 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n"); |
870 | 495 } |
6514 | 496 |
497 int | |
498 base64_encode(const void *enc, int encLen, char *out, int outMax) { | |
499 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; | |
500 | |
501 unsigned char *encBuf; | |
502 int outLen; | |
503 unsigned int bits; | |
504 unsigned int shift; | |
505 | |
506 encBuf = (unsigned char*)enc; | |
507 outLen = 0; | |
508 bits = 0; | |
509 shift = 0; | |
510 | |
511 while( outLen<outMax ) { | |
512 if( encLen>0 ) { | |
513 // Shift in byte | |
514 bits <<= 8; | |
515 bits |= *encBuf; | |
516 shift += 8; | |
517 // Next byte | |
518 encBuf++; | |
519 encLen--; | |
520 } else if( shift>0 ) { | |
521 // Pad last bits to 6 bits - will end next loop | |
522 bits <<= 6 - shift; | |
523 shift = 6; | |
524 } else { | |
525 // Terminate with Mime style '=' | |
526 *out = '='; | |
527 outLen++; | |
528 | |
529 return outLen; | |
530 } | |
531 | |
532 // Encode 6 bit segments | |
533 while( shift>=6 ) { | |
534 shift -= 6; | |
535 *out = b64[ (bits >> shift) & 0x3F ]; | |
536 out++; | |
537 outLen++; | |
538 } | |
539 } | |
540 | |
541 // Output overflow | |
542 return -1; | |
543 } | |
544 | |
15585 | 545 static int http_streaming_start(stream_t *stream, int* file_format) { |
546 HTTP_header_t *http_hdr; | |
547 unsigned int i; | |
548 int fd=-1; | |
549 int redirect = 0; | |
550 int auth_retry=0; | |
551 int seekable=0; | |
552 char *content_type; | |
553 char *next_url; | |
554 URL_t *url = stream->streaming_ctrl->url; | |
6514 | 555 |
15585 | 556 do |
557 { | |
558 fd = http_send_request( url, 0 ); | |
559 if( fd<0 ) { | |
560 return -1; | |
561 } | |
562 | |
563 http_hdr = http_read_response( fd ); | |
564 if( http_hdr==NULL ) { | |
565 closesocket( fd ); | |
566 http_free( http_hdr ); | |
567 return -1; | |
568 } | |
569 | |
570 stream->fd=fd; | |
571 if( verbose>0 ) { | |
572 http_debug_hdr( http_hdr ); | |
573 } | |
574 | |
575 stream->streaming_ctrl->data = (void*)http_hdr; | |
576 | |
577 // Check if we can make partial content requests and thus seek in http-streams | |
578 if( http_hdr!=NULL && http_hdr->status_code==200 ) { | |
579 char *accept_ranges; | |
580 if( (accept_ranges = http_get_field(http_hdr,"Accept-Ranges")) != NULL ) | |
581 seekable = strncmp(accept_ranges,"bytes",5)==0; | |
582 } | |
583 | |
584 // Check if the response is an ICY status_code reason_phrase | |
585 if( !strcasecmp(http_hdr->protocol, "ICY") ) { | |
586 switch( http_hdr->status_code ) { | |
587 case 200: { // OK | |
588 char *field_data = NULL; | |
589 // note: I skip icy-notice1 and 2, as they contain html <BR> | |
590 // and are IMHO useless info ::atmos | |
591 if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL ) | |
592 mp_msg(MSGT_NETWORK,MSGL_INFO,"Name : %s\n", field_data); field_data = NULL; | |
593 if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL ) | |
594 mp_msg(MSGT_NETWORK,MSGL_INFO,"Genre : %s\n", field_data); field_data = NULL; | |
595 if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL ) | |
596 mp_msg(MSGT_NETWORK,MSGL_INFO,"Website: %s\n", field_data); field_data = NULL; | |
597 // XXX: does this really mean public server? ::atmos | |
598 if( (field_data = http_get_field(http_hdr, "icy-pub")) != NULL ) | |
599 mp_msg(MSGT_NETWORK,MSGL_INFO,"Public : %s\n", atoi(field_data)?"yes":"no"); field_data = NULL; | |
600 if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL ) | |
601 mp_msg(MSGT_NETWORK,MSGL_INFO,"Bitrate: %skbit/s\n", field_data); field_data = NULL; | |
602 | |
603 // If content-type == video/nsv we most likely have a winamp video stream | |
604 // otherwise it should be mp3. if there are more types consider adding mime type | |
605 // handling like later | |
606 if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "video/nsv") || !strcmp(field_data, "misc/ultravox"))) | |
607 *file_format = DEMUXER_TYPE_NSV; | |
608 else | |
609 *file_format = DEMUXER_TYPE_AUDIO; | |
610 return 0; | |
611 } | |
612 case 400: // Server Full | |
613 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n"); | |
614 return -1; | |
615 case 401: // Service Unavailable | |
616 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n"); | |
617 return -1; | |
618 case 403: // Service Forbidden | |
619 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n"); | |
620 return -1; | |
621 case 404: // Resource Not Found | |
622 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n"); | |
623 return -1; | |
624 default: | |
625 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n"); | |
626 return -1; | |
627 } | |
628 } | |
629 | |
630 // Assume standard http if not ICY | |
631 switch( http_hdr->status_code ) { | |
632 case 200: // OK | |
633 // Look if we can use the Content-Type | |
634 content_type = http_get_field( http_hdr, "Content-Type" ); | |
635 if( content_type!=NULL ) { | |
636 char *content_length = NULL; | |
637 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type ); | |
638 if( (content_length = http_get_field(http_hdr, "Content-Length")) != NULL) | |
639 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length")); | |
640 // Check in the mime type table for a demuxer type | |
641 i = 0; | |
642 while(mime_type_table[i].mime_type != NULL) { | |
643 if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) { | |
644 *file_format = mime_type_table[i].demuxer_type; | |
645 return seekable; | |
646 } | |
647 i++; | |
648 } | |
649 } | |
650 // Not found in the mime type table, don't fail, | |
651 // we should try raw HTTP | |
652 return seekable; | |
653 // Redirect | |
654 case 301: // Permanently | |
655 case 302: // Temporarily | |
656 // TODO: RFC 2616, recommand to detect infinite redirection loops | |
657 next_url = http_get_field( http_hdr, "Location" ); | |
658 if( next_url!=NULL ) { | |
659 closesocket( fd ); | |
660 url_free( url ); | |
661 stream->streaming_ctrl->url = url = url_new( next_url ); | |
662 http_free( http_hdr ); | |
663 redirect = 1; | |
664 } | |
665 break; | |
666 case 401: // Authentication required | |
667 if( http_authenticate(http_hdr, url, &auth_retry)<0 ) return STREAM_UNSUPORTED; | |
668 redirect = 1; | |
669 break; | |
670 default: | |
671 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); | |
672 return -1; | |
673 } | |
674 } while( redirect ); | |
675 | |
676 return -1; | |
677 } | |
678 | |
679 static int fixup_open(stream_t *stream,int seekable) { | |
680 | |
681 stream->type = STREAMTYPE_STREAM; | |
682 if(seekable) | |
683 { | |
684 stream->flags |= STREAM_SEEK; | |
685 stream->seek = http_seek; | |
686 } | |
687 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
688 if(nop_streaming_start( stream )) { | |
689 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_start failed\n"); | |
690 streaming_ctrl_free(stream->streaming_ctrl); | |
691 stream->streaming_ctrl = NULL; | |
692 return STREAM_UNSUPORTED; | |
693 } | |
694 | |
695 fixup_network_stream_cache(stream); | |
696 return STREAM_OK; | |
697 } | |
698 | |
699 static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) { | |
700 int seekable=0; | |
701 URL_t *url; | |
702 | |
703 stream->streaming_ctrl = streaming_ctrl_new(); | |
704 if( stream->streaming_ctrl==NULL ) { | |
705 return STREAM_ERROR; | |
706 } | |
707 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
708 url = url_new(stream->url); | |
709 stream->streaming_ctrl->url = check4proxies(url); | |
710 //url_free(url); | |
711 | |
712 mp_msg(MSGT_OPEN, MSGL_INFO, "STREAM_HTTP(1), URL: %s\n", stream->url); | |
713 seekable = http_streaming_start(stream, file_format); | |
714 if((seekable < 0) || (*file_format == DEMUXER_TYPE_ASF)) { | |
715 streaming_ctrl_free(stream->streaming_ctrl); | |
716 stream->streaming_ctrl = NULL; | |
717 return STREAM_UNSUPORTED; | |
718 } | |
719 | |
720 return fixup_open(stream, seekable); | |
721 } | |
722 | |
723 static int open_s2(stream_t *stream,int mode, void* opts, int* file_format) { | |
724 int seekable=0; | |
725 URL_t *url; | |
726 | |
727 stream->streaming_ctrl = streaming_ctrl_new(); | |
728 if( stream->streaming_ctrl==NULL ) { | |
729 return STREAM_ERROR; | |
730 } | |
731 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
732 url = url_new(stream->url); | |
733 stream->streaming_ctrl->url = check4proxies(url); | |
734 //url_free(url); | |
735 | |
736 mp_msg(MSGT_OPEN, MSGL_INFO, "STREAM_HTTP(2), URL: %s\n", stream->url); | |
737 seekable = http_streaming_start(stream, file_format); | |
738 if(seekable < 0) { | |
739 streaming_ctrl_free(stream->streaming_ctrl); | |
740 stream->streaming_ctrl = NULL; | |
741 return STREAM_UNSUPORTED; | |
742 } | |
743 | |
744 return fixup_open(stream, seekable); | |
745 } | |
746 | |
747 | |
748 stream_info_t stream_info_http1 = { | |
749 "http streaming", | |
750 "null", | |
751 "Bertrand, Albeau, Reimar Doeffinger, Arpi?", | |
752 "plain http", | |
753 open_s1, | |
754 {"http", "http_proxy", NULL}, | |
755 NULL, | |
756 0 // Urls are an option string | |
757 }; | |
758 | |
759 stream_info_t stream_info_http2 = { | |
760 "http streaming", | |
761 "null", | |
762 "Bertrand, Albeu, Arpi? who?", | |
763 "plain http, aslo used as falback for many other protocols", | |
764 open_s2, | |
765 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback | |
766 NULL, | |
767 0 // Urls are an option string | |
768 }; |