Mercurial > mplayer.hg
comparison stream/http.c @ 19271:64d82a45a05d
introduce new 'stream' directory for all stream layer related components and split them from libmpdemux
author | ben |
---|---|
date | Mon, 31 Jul 2006 17:39:17 +0000 |
parents | libmpdemux/http.c@a8e681ad7c90 |
children | ab8d6b6deb63 |
comparison
equal
deleted
inserted
replaced
19270:7d39b911f0bd | 19271:64d82a45a05d |
---|---|
1 /* | |
2 * HTTP Helper | |
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com> | |
4 * (C) 2001, MPlayer team. | |
5 */ | |
6 | |
7 #include "config.h" | |
8 | |
9 #include <stdio.h> | |
10 #include <stdlib.h> | |
11 #include <string.h> | |
12 #include <unistd.h> | |
13 | |
14 #ifndef HAVE_WINSOCK2 | |
15 #define closesocket close | |
16 #else | |
17 #include <winsock2.h> | |
18 #include <ws2tcpip.h> | |
19 #endif | |
20 | |
21 #include "http.h" | |
22 #include "url.h" | |
23 #include "mp_msg.h" | |
24 | |
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 typedef struct { | |
38 unsigned metaint; | |
39 unsigned metapos; | |
40 int is_ultravox; | |
41 } scast_data_t; | |
42 | |
43 /** | |
44 * \brief first read any data from sc->buffer then from fd | |
45 * \param fd file descriptor to read data from | |
46 * \param buffer buffer to read into | |
47 * \param len how many bytes to read | |
48 * \param sc streaming control containing buffer to read from first | |
49 * \return len unless there is a read error or eof | |
50 */ | |
51 static unsigned my_read(int fd, char *buffer, int len, streaming_ctrl_t *sc) { | |
52 unsigned pos = 0; | |
53 unsigned cp_len = sc->buffer_size - sc->buffer_pos; | |
54 if (cp_len > len) | |
55 cp_len = len; | |
56 memcpy(buffer, &sc->buffer[sc->buffer_pos], cp_len); | |
57 sc->buffer_pos += cp_len; | |
58 pos += cp_len; | |
59 while (pos < len) { | |
60 int ret = recv(fd, &buffer[pos], len - pos, 0); | |
61 if (ret <= 0) | |
62 break; | |
63 pos += ret; | |
64 } | |
65 return pos; | |
66 } | |
67 | |
68 /** | |
69 * \brief read and process (i.e. discard *g*) a block of ultravox metadata | |
70 * \param fd file descriptor to read from | |
71 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd | |
72 * \return number of real data before next metadata block starts or 0 on error | |
73 */ | |
74 static unsigned uvox_meta_read(int fd, streaming_ctrl_t *sc) { | |
75 unsigned metaint; | |
76 unsigned char info[6] = {0, 0, 0, 0, 0, 0}; | |
77 int info_read; | |
78 do { | |
79 info_read = my_read(fd, info, 1, sc); | |
80 if (info[0] == 0x00) | |
81 info_read = my_read(fd, info, 6, sc); | |
82 else | |
83 info_read += my_read(fd, &info[1], 5, sc); | |
84 if (info_read != 6) // read error or eof | |
85 return 0; | |
86 // sync byte and reserved flags | |
87 if (info[0] != 0x5a || (info[1] & 0xfc) != 0x00) { | |
88 mp_msg(MSGT_DEMUXER, MSGL_ERR, "Invalid or unknown uvox metadata\n"); | |
89 return 0; | |
90 } | |
91 if (info[1] & 0x01) | |
92 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Encrypted ultravox data\n"); | |
93 metaint = info[4] << 8 | info[5]; | |
94 if ((info[3] & 0xf) < 0x07) { // discard any metadata nonsense | |
95 char *metabuf = malloc(metaint); | |
96 my_read(fd, metabuf, metaint, sc); | |
97 free(metabuf); | |
98 } | |
99 } while ((info[3] & 0xf) < 0x07); | |
100 return metaint; | |
101 } | |
102 | |
103 /** | |
104 * \brief read one scast meta data entry and print it | |
105 * \param fd file descriptor to read from | |
106 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd | |
107 */ | |
108 static void scast_meta_read(int fd, streaming_ctrl_t *sc) { | |
109 unsigned char tmp = 0; | |
110 unsigned metalen; | |
111 my_read(fd, &tmp, 1, sc); | |
112 metalen = tmp * 16; | |
113 if (metalen > 0) { | |
114 char *info = malloc(metalen + 1); | |
115 unsigned nlen = my_read(fd, info, metalen, sc); | |
116 info[nlen] = 0; | |
117 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\nICY Info: %s\n", info); | |
118 free(info); | |
119 } | |
120 } | |
121 | |
122 /** | |
123 * \brief read data from scast/ultravox stream without any metadata | |
124 * \param fd file descriptor to read from | |
125 * \param buffer buffer to read data into | |
126 * \param size number of bytes to read | |
127 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd | |
128 */ | |
129 static int scast_streaming_read(int fd, char *buffer, int size, | |
130 streaming_ctrl_t *sc) { | |
131 scast_data_t *sd = (scast_data_t *)sc->data; | |
132 unsigned block, ret; | |
133 unsigned done = 0; | |
134 | |
135 // first read remaining data up to next metadata | |
136 block = sd->metaint - sd->metapos; | |
137 if (block > size) | |
138 block = size; | |
139 ret = my_read(fd, buffer, block, sc); | |
140 sd->metapos += ret; | |
141 done += ret; | |
142 if (ret != block) // read problems or eof | |
143 size = done; | |
144 | |
145 while (done < size) { // now comes the metadata | |
146 if (sd->is_ultravox) | |
147 { | |
148 sd->metaint = uvox_meta_read(fd, sc); | |
149 if (!sd->metaint) | |
150 size = done; | |
151 } | |
152 else | |
153 scast_meta_read(fd, sc); // read and display metadata | |
154 sd->metapos = 0; | |
155 block = size - done; | |
156 if (block > sd->metaint) | |
157 block = sd->metaint; | |
158 ret = my_read(fd, &buffer[done], block, sc); | |
159 sd->metapos += ret; | |
160 done += ret; | |
161 if (ret != block) // read problems or eof | |
162 size = done; | |
163 } | |
164 return done; | |
165 } | |
166 | |
167 static int scast_streaming_start(stream_t *stream) { | |
168 int metaint; | |
169 scast_data_t *scast_data; | |
170 HTTP_header_t *http_hdr = stream->streaming_ctrl->data; | |
171 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0; | |
172 if (!stream || stream->fd < 0 || !http_hdr) | |
173 return -1; | |
174 if (is_ultravox) | |
175 metaint = 0; | |
176 else { | |
177 metaint = atoi(http_get_field(http_hdr, "Icy-MetaInt")); | |
178 if (metaint <= 0) | |
179 return -1; | |
180 } | |
181 stream->streaming_ctrl->buffer = malloc(http_hdr->body_size); | |
182 stream->streaming_ctrl->buffer_size = http_hdr->body_size; | |
183 stream->streaming_ctrl->buffer_pos = 0; | |
184 memcpy(stream->streaming_ctrl->buffer, http_hdr->body, http_hdr->body_size); | |
185 scast_data = malloc(sizeof(scast_data_t)); | |
186 scast_data->metaint = metaint; | |
187 scast_data->metapos = 0; | |
188 scast_data->is_ultravox = is_ultravox; | |
189 http_free(http_hdr); | |
190 stream->streaming_ctrl->data = scast_data; | |
191 stream->streaming_ctrl->streaming_read = scast_streaming_read; | |
192 stream->streaming_ctrl->streaming_seek = NULL; | |
193 stream->streaming_ctrl->prebuffer_size = 64 * 1024; // 64 KBytes | |
194 stream->streaming_ctrl->buffering = 1; | |
195 stream->streaming_ctrl->status = streaming_playing_e; | |
196 return 0; | |
197 } | |
198 | |
199 static int nop_streaming_start( stream_t *stream ) { | |
200 HTTP_header_t *http_hdr = NULL; | |
201 char *next_url=NULL; | |
202 URL_t *rd_url=NULL; | |
203 int fd,ret; | |
204 if( stream==NULL ) return -1; | |
205 | |
206 fd = stream->fd; | |
207 if( fd<0 ) { | |
208 fd = http_send_request( stream->streaming_ctrl->url, 0 ); | |
209 if( fd<0 ) return -1; | |
210 http_hdr = http_read_response( fd ); | |
211 if( http_hdr==NULL ) return -1; | |
212 | |
213 switch( http_hdr->status_code ) { | |
214 case 200: // OK | |
215 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") ); | |
216 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); | |
217 if( http_hdr->body_size>0 ) { | |
218 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { | |
219 http_free( http_hdr ); | |
220 return -1; | |
221 } | |
222 } | |
223 break; | |
224 // Redirect | |
225 case 301: // Permanently | |
226 case 302: // Temporarily | |
227 ret=-1; | |
228 next_url = http_get_field( http_hdr, "Location" ); | |
229 | |
230 if (next_url != NULL) | |
231 rd_url=url_new(next_url); | |
232 | |
233 if (next_url != NULL && rd_url != NULL) { | |
234 mp_msg(MSGT_NETWORK,MSGL_STATUS,"Redirected: Using this url instead %s\n",next_url); | |
235 stream->streaming_ctrl->url=check4proxies(rd_url); | |
236 ret=nop_streaming_start(stream); //recursively get streaming started | |
237 } else { | |
238 mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n"); | |
239 closesocket( fd ); | |
240 fd = -1; | |
241 } | |
242 return ret; | |
243 break; | |
244 case 401: //Authorization required | |
245 case 403: //Forbidden | |
246 case 404: //Not found | |
247 case 500: //Server Error | |
248 default: | |
249 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); | |
250 closesocket( fd ); | |
251 fd = -1; | |
252 return -1; | |
253 break; | |
254 } | |
255 stream->fd = fd; | |
256 } else { | |
257 http_hdr = (HTTP_header_t*)stream->streaming_ctrl->data; | |
258 if( http_hdr->body_size>0 ) { | |
259 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { | |
260 http_free( http_hdr ); | |
261 stream->streaming_ctrl->data = NULL; | |
262 return -1; | |
263 } | |
264 } | |
265 } | |
266 | |
267 if( http_hdr ) { | |
268 http_free( http_hdr ); | |
269 stream->streaming_ctrl->data = NULL; | |
270 } | |
271 | |
272 stream->streaming_ctrl->streaming_read = nop_streaming_read; | |
273 stream->streaming_ctrl->streaming_seek = nop_streaming_seek; | |
274 stream->streaming_ctrl->prebuffer_size = 64*1024; // 64 KBytes | |
275 stream->streaming_ctrl->buffering = 1; | |
276 stream->streaming_ctrl->status = streaming_playing_e; | |
277 return 0; | |
278 } | |
279 | |
280 HTTP_header_t * | |
281 http_new_header(void) { | |
282 HTTP_header_t *http_hdr; | |
283 | |
284 http_hdr = malloc(sizeof(HTTP_header_t)); | |
285 if( http_hdr==NULL ) return NULL; | |
286 memset( http_hdr, 0, sizeof(HTTP_header_t) ); | |
287 | |
288 return http_hdr; | |
289 } | |
290 | |
291 void | |
292 http_free( HTTP_header_t *http_hdr ) { | |
293 HTTP_field_t *field, *field2free; | |
294 if( http_hdr==NULL ) return; | |
295 if( http_hdr->protocol!=NULL ) free( http_hdr->protocol ); | |
296 if( http_hdr->uri!=NULL ) free( http_hdr->uri ); | |
297 if( http_hdr->reason_phrase!=NULL ) free( http_hdr->reason_phrase ); | |
298 if( http_hdr->field_search!=NULL ) free( http_hdr->field_search ); | |
299 if( http_hdr->method!=NULL ) free( http_hdr->method ); | |
300 if( http_hdr->buffer!=NULL ) free( http_hdr->buffer ); | |
301 field = http_hdr->first_field; | |
302 while( field!=NULL ) { | |
303 field2free = field; | |
304 if (field->field_name) | |
305 free(field->field_name); | |
306 field = field->next; | |
307 free( field2free ); | |
308 } | |
309 free( http_hdr ); | |
310 http_hdr = NULL; | |
311 } | |
312 | |
313 int | |
314 http_response_append( HTTP_header_t *http_hdr, char *response, int length ) { | |
315 if( http_hdr==NULL || response==NULL || length<0 ) return -1; | |
316 | |
317 if( (unsigned)length > SIZE_MAX - http_hdr->buffer_size - 1) { | |
318 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Bad size in memory (re)allocation\n"); | |
319 return -1; | |
320 } | |
321 http_hdr->buffer = (char*)realloc( http_hdr->buffer, http_hdr->buffer_size+length+1 ); | |
322 if( http_hdr->buffer==NULL ) { | |
323 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory (re)allocation failed\n"); | |
324 return -1; | |
325 } | |
326 memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length ); | |
327 http_hdr->buffer_size += length; | |
328 http_hdr->buffer[http_hdr->buffer_size]=0; // close the string! | |
329 return http_hdr->buffer_size; | |
330 } | |
331 | |
332 int | |
333 http_is_header_entire( HTTP_header_t *http_hdr ) { | |
334 if( http_hdr==NULL ) return -1; | |
335 if( http_hdr->buffer==NULL ) return 0; // empty | |
336 | |
337 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL && | |
338 strstr(http_hdr->buffer, "\n\n")==NULL ) return 0; | |
339 return 1; | |
340 } | |
341 | |
342 int | |
343 http_response_parse( HTTP_header_t *http_hdr ) { | |
344 char *hdr_ptr, *ptr; | |
345 char *field=NULL; | |
346 int pos_hdr_sep, hdr_sep_len; | |
347 size_t len; | |
348 if( http_hdr==NULL ) return -1; | |
349 if( http_hdr->is_parsed ) return 0; | |
350 | |
351 // Get the protocol | |
352 hdr_ptr = strstr( http_hdr->buffer, " " ); | |
353 if( hdr_ptr==NULL ) { | |
354 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. No space separator found.\n"); | |
355 return -1; | |
356 } | |
357 len = hdr_ptr-http_hdr->buffer; | |
358 http_hdr->protocol = malloc(len+1); | |
359 if( http_hdr->protocol==NULL ) { | |
360 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
361 return -1; | |
362 } | |
363 strncpy( http_hdr->protocol, http_hdr->buffer, len ); | |
364 http_hdr->protocol[len]='\0'; | |
365 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) { | |
366 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) { | |
367 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get HTTP minor version.\n"); | |
368 return -1; | |
369 } | |
370 } | |
371 | |
372 // Get the status code | |
373 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) { | |
374 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get status code.\n"); | |
375 return -1; | |
376 } | |
377 hdr_ptr += 4; | |
378 | |
379 // Get the reason phrase | |
380 ptr = strstr( hdr_ptr, "\n" ); | |
381 if( hdr_ptr==NULL ) { | |
382 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get the reason phrase.\n"); | |
383 return -1; | |
384 } | |
385 len = ptr-hdr_ptr; | |
386 http_hdr->reason_phrase = malloc(len+1); | |
387 if( http_hdr->reason_phrase==NULL ) { | |
388 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
389 return -1; | |
390 } | |
391 strncpy( http_hdr->reason_phrase, hdr_ptr, len ); | |
392 if( http_hdr->reason_phrase[len-1]=='\r' ) { | |
393 len--; | |
394 } | |
395 http_hdr->reason_phrase[len]='\0'; | |
396 | |
397 // Set the position of the header separator: \r\n\r\n | |
398 hdr_sep_len = 4; | |
399 ptr = strstr( http_hdr->buffer, "\r\n\r\n" ); | |
400 if( ptr==NULL ) { | |
401 ptr = strstr( http_hdr->buffer, "\n\n" ); | |
402 if( ptr==NULL ) { | |
403 mp_msg(MSGT_NETWORK,MSGL_ERR,"Header may be incomplete. No CRLF CRLF found.\n"); | |
404 return -1; | |
405 } | |
406 hdr_sep_len = 2; | |
407 } | |
408 pos_hdr_sep = ptr-http_hdr->buffer; | |
409 | |
410 // Point to the first line after the method line. | |
411 hdr_ptr = strstr( http_hdr->buffer, "\n" )+1; | |
412 do { | |
413 ptr = hdr_ptr; | |
414 while( *ptr!='\r' && *ptr!='\n' ) ptr++; | |
415 len = ptr-hdr_ptr; | |
416 if( len==0 ) break; | |
417 field = (char*)realloc(field, len+1); | |
418 if( field==NULL ) { | |
419 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); | |
420 return -1; | |
421 } | |
422 strncpy( field, hdr_ptr, len ); | |
423 field[len]='\0'; | |
424 http_set_field( http_hdr, field ); | |
425 hdr_ptr = ptr+((*ptr=='\r')?2:1); | |
426 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) ); | |
427 | |
428 if( field!=NULL ) free( field ); | |
429 | |
430 if( pos_hdr_sep+hdr_sep_len<http_hdr->buffer_size ) { | |
431 // Response has data! | |
432 http_hdr->body = http_hdr->buffer+pos_hdr_sep+hdr_sep_len; | |
433 http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+hdr_sep_len); | |
434 } | |
435 | |
436 http_hdr->is_parsed = 1; | |
437 return 0; | |
438 } | |
439 | |
440 char * | |
441 http_build_request( HTTP_header_t *http_hdr ) { | |
442 char *ptr, *uri=NULL; | |
443 int len; | |
444 HTTP_field_t *field; | |
445 if( http_hdr==NULL ) return NULL; | |
446 | |
447 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET"); | |
448 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/"); | |
449 else { | |
450 uri = malloc(strlen(http_hdr->uri) + 1); | |
451 if( uri==NULL ) { | |
452 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); | |
453 return NULL; | |
454 } | |
455 strcpy(uri,http_hdr->uri); | |
456 } | |
457 | |
458 //**** Compute the request length | |
459 // Add the Method line | |
460 len = strlen(http_hdr->method)+strlen(uri)+12; | |
461 // Add the fields | |
462 field = http_hdr->first_field; | |
463 while( field!=NULL ) { | |
464 len += strlen(field->field_name)+2; | |
465 field = field->next; | |
466 } | |
467 // Add the CRLF | |
468 len += 2; | |
469 // Add the body | |
470 if( http_hdr->body!=NULL ) { | |
471 len += http_hdr->body_size; | |
472 } | |
473 // Free the buffer if it was previously used | |
474 if( http_hdr->buffer!=NULL ) { | |
475 free( http_hdr->buffer ); | |
476 http_hdr->buffer = NULL; | |
477 } | |
478 http_hdr->buffer = malloc(len+1); | |
479 if( http_hdr->buffer==NULL ) { | |
480 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); | |
481 return NULL; | |
482 } | |
483 http_hdr->buffer_size = len; | |
484 | |
485 //*** Building the request | |
486 ptr = http_hdr->buffer; | |
487 // Add the method line | |
488 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version ); | |
489 field = http_hdr->first_field; | |
490 // Add the field | |
491 while( field!=NULL ) { | |
492 ptr += sprintf( ptr, "%s\r\n", field->field_name ); | |
493 field = field->next; | |
494 } | |
495 ptr += sprintf( ptr, "\r\n" ); | |
496 // Add the body | |
497 if( http_hdr->body!=NULL ) { | |
498 memcpy( ptr, http_hdr->body, http_hdr->body_size ); | |
499 } | |
500 | |
501 if( uri ) free( uri ); | |
502 return http_hdr->buffer; | |
503 } | |
504 | |
505 char * | |
506 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) { | |
507 if( http_hdr==NULL || field_name==NULL ) return NULL; | |
508 http_hdr->field_search_pos = http_hdr->first_field; | |
509 http_hdr->field_search = (char*)realloc( http_hdr->field_search, strlen(field_name)+1 ); | |
510 if( http_hdr->field_search==NULL ) { | |
511 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
512 return NULL; | |
513 } | |
514 strcpy( http_hdr->field_search, field_name ); | |
515 return http_get_next_field( http_hdr ); | |
516 } | |
517 | |
518 char * | |
519 http_get_next_field( HTTP_header_t *http_hdr ) { | |
520 char *ptr; | |
521 HTTP_field_t *field; | |
522 if( http_hdr==NULL ) return NULL; | |
523 | |
524 field = http_hdr->field_search_pos; | |
525 while( field!=NULL ) { | |
526 ptr = strstr( field->field_name, ":" ); | |
527 if( ptr==NULL ) return NULL; | |
528 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) { | |
529 ptr++; // Skip the column | |
530 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some | |
531 http_hdr->field_search_pos = field->next; | |
532 return ptr; // return the value without the field name | |
533 } | |
534 field = field->next; | |
535 } | |
536 return NULL; | |
537 } | |
538 | |
539 void | |
540 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) { | |
541 HTTP_field_t *new_field; | |
542 if( http_hdr==NULL || field_name==NULL ) return; | |
543 | |
544 new_field = malloc(sizeof(HTTP_field_t)); | |
545 if( new_field==NULL ) { | |
546 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
547 return; | |
548 } | |
549 new_field->next = NULL; | |
550 new_field->field_name = malloc(strlen(field_name)+1); | |
551 if( new_field->field_name==NULL ) { | |
552 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
553 return; | |
554 } | |
555 strcpy( new_field->field_name, field_name ); | |
556 | |
557 if( http_hdr->last_field==NULL ) { | |
558 http_hdr->first_field = new_field; | |
559 } else { | |
560 http_hdr->last_field->next = new_field; | |
561 } | |
562 http_hdr->last_field = new_field; | |
563 http_hdr->field_nb++; | |
564 } | |
565 | |
566 void | |
567 http_set_method( HTTP_header_t *http_hdr, const char *method ) { | |
568 if( http_hdr==NULL || method==NULL ) return; | |
569 | |
570 http_hdr->method = malloc(strlen(method)+1); | |
571 if( http_hdr->method==NULL ) { | |
572 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
573 return; | |
574 } | |
575 strcpy( http_hdr->method, method ); | |
576 } | |
577 | |
578 void | |
579 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) { | |
580 if( http_hdr==NULL || uri==NULL ) return; | |
581 | |
582 http_hdr->uri = malloc(strlen(uri)+1); | |
583 if( http_hdr->uri==NULL ) { | |
584 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
585 return; | |
586 } | |
587 strcpy( http_hdr->uri, uri ); | |
588 } | |
589 | |
590 int | |
591 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) { | |
592 char *auth = NULL, *usr_pass = NULL, *b64_usr_pass = NULL; | |
593 int encoded_len, pass_len=0, out_len; | |
594 int res = -1; | |
595 if( http_hdr==NULL || username==NULL ) return -1; | |
596 | |
597 if( password!=NULL ) { | |
598 pass_len = strlen(password); | |
599 } | |
600 | |
601 usr_pass = malloc(strlen(username)+pass_len+2); | |
602 if( usr_pass==NULL ) { | |
603 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
604 goto out; | |
605 } | |
606 | |
607 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password ); | |
608 | |
609 // Base 64 encode with at least 33% more data than the original size | |
610 encoded_len = strlen(usr_pass)*2; | |
611 b64_usr_pass = malloc(encoded_len); | |
612 if( b64_usr_pass==NULL ) { | |
613 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
614 goto out; | |
615 } | |
616 | |
617 out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len); | |
618 if( out_len<0 ) { | |
619 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n"); | |
620 goto out; | |
621 } | |
622 | |
623 b64_usr_pass[out_len]='\0'; | |
624 | |
625 auth = malloc(encoded_len+22); | |
626 if( auth==NULL ) { | |
627 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
628 goto out; | |
629 } | |
630 | |
631 sprintf( auth, "Authorization: Basic %s", b64_usr_pass); | |
632 http_set_field( http_hdr, auth ); | |
633 res = 0; | |
634 | |
635 out: | |
636 free( usr_pass ); | |
637 free( b64_usr_pass ); | |
638 free( auth ); | |
639 | |
640 return res; | |
641 } | |
642 | |
643 void | |
644 http_debug_hdr( HTTP_header_t *http_hdr ) { | |
645 HTTP_field_t *field; | |
646 int i = 0; | |
647 if( http_hdr==NULL ) return; | |
648 | |
649 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n"); | |
650 mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol ); | |
651 mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version ); | |
652 mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri ); | |
653 mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method ); | |
654 mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code ); | |
655 mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase ); | |
656 mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%d]\n", http_hdr->body_size ); | |
657 | |
658 mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n"); | |
659 field = http_hdr->first_field; | |
660 while( field!=NULL ) { | |
661 mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name ); | |
662 field = field->next; | |
663 } | |
664 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n"); | |
665 } | |
666 | |
667 int | |
668 base64_encode(const void *enc, int encLen, char *out, int outMax) { | |
669 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
670 | |
671 unsigned char *encBuf; | |
672 int outLen; | |
673 unsigned int bits; | |
674 unsigned int shift; | |
675 | |
676 encBuf = (unsigned char*)enc; | |
677 outLen = 0; | |
678 bits = 0; | |
679 shift = 0; | |
680 outMax &= ~3; | |
681 | |
682 while( outLen<outMax ) { | |
683 if( encLen>0 ) { | |
684 // Shift in byte | |
685 bits <<= 8; | |
686 bits |= *encBuf; | |
687 shift += 8; | |
688 // Next byte | |
689 encBuf++; | |
690 encLen--; | |
691 } else if( shift>0 ) { | |
692 // Pad last bits to 6 bits - will end next loop | |
693 bits <<= 6 - shift; | |
694 shift = 6; | |
695 } else { | |
696 // As per RFC 2045, section 6.8, | |
697 // pad output as necessary: 0 to 2 '=' chars. | |
698 while( outLen & 3 ){ | |
699 *out++ = '='; | |
700 outLen++; | |
701 } | |
702 | |
703 return outLen; | |
704 } | |
705 | |
706 // Encode 6 bit segments | |
707 while( shift>=6 ) { | |
708 shift -= 6; | |
709 *out = b64[ (bits >> shift) & 0x3F ]; | |
710 out++; | |
711 outLen++; | |
712 } | |
713 } | |
714 | |
715 // Output overflow | |
716 return -1; | |
717 } | |
718 | |
719 static int http_streaming_start(stream_t *stream, int* file_format) { | |
720 HTTP_header_t *http_hdr; | |
721 unsigned int i; | |
722 int fd=-1; | |
723 int redirect = 0; | |
724 int auth_retry=0; | |
725 int seekable=0; | |
726 char *content_type; | |
727 char *next_url; | |
728 URL_t *url = stream->streaming_ctrl->url; | |
729 | |
730 do | |
731 { | |
732 fd = http_send_request( url, 0 ); | |
733 if( fd<0 ) { | |
734 return -1; | |
735 } | |
736 | |
737 http_hdr = http_read_response( fd ); | |
738 if( http_hdr==NULL ) { | |
739 closesocket( fd ); | |
740 http_free( http_hdr ); | |
741 return -1; | |
742 } | |
743 | |
744 stream->fd=fd; | |
745 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) { | |
746 http_debug_hdr( http_hdr ); | |
747 } | |
748 | |
749 stream->streaming_ctrl->data = (void*)http_hdr; | |
750 | |
751 // Check if we can make partial content requests and thus seek in http-streams | |
752 if( http_hdr!=NULL && http_hdr->status_code==200 ) { | |
753 char *accept_ranges; | |
754 if( (accept_ranges = http_get_field(http_hdr,"Accept-Ranges")) != NULL ) | |
755 seekable = strncmp(accept_ranges,"bytes",5)==0; | |
756 } | |
757 | |
758 // Check if the response is an ICY status_code reason_phrase | |
759 if( !strcasecmp(http_hdr->protocol, "ICY") ) { | |
760 switch( http_hdr->status_code ) { | |
761 case 200: { // OK | |
762 char *field_data = NULL; | |
763 // note: I skip icy-notice1 and 2, as they contain html <BR> | |
764 // and are IMHO useless info ::atmos | |
765 if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL ) | |
766 mp_msg(MSGT_NETWORK,MSGL_INFO,"Name : %s\n", field_data); field_data = NULL; | |
767 if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL ) | |
768 mp_msg(MSGT_NETWORK,MSGL_INFO,"Genre : %s\n", field_data); field_data = NULL; | |
769 if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL ) | |
770 mp_msg(MSGT_NETWORK,MSGL_INFO,"Website: %s\n", field_data); field_data = NULL; | |
771 // XXX: does this really mean public server? ::atmos | |
772 if( (field_data = http_get_field(http_hdr, "icy-pub")) != NULL ) | |
773 mp_msg(MSGT_NETWORK,MSGL_INFO,"Public : %s\n", atoi(field_data)?"yes":"no"); field_data = NULL; | |
774 if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL ) | |
775 mp_msg(MSGT_NETWORK,MSGL_INFO,"Bitrate: %skbit/s\n", field_data); field_data = NULL; | |
776 | |
777 // If content-type == video/nsv we most likely have a winamp video stream | |
778 // otherwise it should be mp3. if there are more types consider adding mime type | |
779 // handling like later | |
780 if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "video/nsv") || !strcmp(field_data, "misc/ultravox"))) | |
781 *file_format = DEMUXER_TYPE_NSV; | |
782 else if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "audio/aacp") || !strcmp(field_data, "audio/aac"))) | |
783 *file_format = DEMUXER_TYPE_AAC; | |
784 else | |
785 *file_format = DEMUXER_TYPE_AUDIO; | |
786 return 0; | |
787 } | |
788 case 400: // Server Full | |
789 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n"); | |
790 return -1; | |
791 case 401: // Service Unavailable | |
792 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n"); | |
793 return -1; | |
794 case 403: // Service Forbidden | |
795 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n"); | |
796 return -1; | |
797 case 404: // Resource Not Found | |
798 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n"); | |
799 return -1; | |
800 default: | |
801 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n"); | |
802 return -1; | |
803 } | |
804 } | |
805 | |
806 // Assume standard http if not ICY | |
807 switch( http_hdr->status_code ) { | |
808 case 200: // OK | |
809 // Look if we can use the Content-Type | |
810 content_type = http_get_field( http_hdr, "Content-Type" ); | |
811 if( content_type!=NULL ) { | |
812 char *content_length = NULL; | |
813 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type ); | |
814 if( (content_length = http_get_field(http_hdr, "Content-Length")) != NULL) | |
815 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length")); | |
816 // Check in the mime type table for a demuxer type | |
817 i = 0; | |
818 while(mime_type_table[i].mime_type != NULL) { | |
819 if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) { | |
820 *file_format = mime_type_table[i].demuxer_type; | |
821 return seekable; | |
822 } | |
823 i++; | |
824 } | |
825 } | |
826 // Not found in the mime type table, don't fail, | |
827 // we should try raw HTTP | |
828 return seekable; | |
829 // Redirect | |
830 case 301: // Permanently | |
831 case 302: // Temporarily | |
832 // TODO: RFC 2616, recommand to detect infinite redirection loops | |
833 next_url = http_get_field( http_hdr, "Location" ); | |
834 if( next_url!=NULL ) { | |
835 closesocket( fd ); | |
836 url_free( url ); | |
837 stream->streaming_ctrl->url = url = url_new( next_url ); | |
838 http_free( http_hdr ); | |
839 redirect = 1; | |
840 } | |
841 break; | |
842 case 401: // Authentication required | |
843 if( http_authenticate(http_hdr, url, &auth_retry)<0 ) return STREAM_UNSUPORTED; | |
844 redirect = 1; | |
845 break; | |
846 default: | |
847 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); | |
848 return -1; | |
849 } | |
850 } while( redirect ); | |
851 | |
852 return -1; | |
853 } | |
854 | |
855 static int fixup_open(stream_t *stream,int seekable) { | |
856 HTTP_header_t *http_hdr = stream->streaming_ctrl->data; | |
857 int is_icy = http_hdr && http_get_field(http_hdr, "Icy-MetaInt"); | |
858 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0; | |
859 | |
860 stream->type = STREAMTYPE_STREAM; | |
861 if(!is_icy && !is_ultravox && seekable) | |
862 { | |
863 stream->flags |= STREAM_SEEK; | |
864 stream->seek = http_seek; | |
865 } | |
866 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
867 if ((!is_icy && !is_ultravox) || scast_streaming_start(stream)) | |
868 if(nop_streaming_start( stream )) { | |
869 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_start failed\n"); | |
870 streaming_ctrl_free(stream->streaming_ctrl); | |
871 stream->streaming_ctrl = NULL; | |
872 return STREAM_UNSUPORTED; | |
873 } | |
874 | |
875 fixup_network_stream_cache(stream); | |
876 return STREAM_OK; | |
877 } | |
878 | |
879 static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) { | |
880 int seekable=0; | |
881 URL_t *url; | |
882 | |
883 stream->streaming_ctrl = streaming_ctrl_new(); | |
884 if( stream->streaming_ctrl==NULL ) { | |
885 return STREAM_ERROR; | |
886 } | |
887 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
888 url = url_new(stream->url); | |
889 stream->streaming_ctrl->url = check4proxies(url); | |
890 url_free(url); | |
891 | |
892 mp_msg(MSGT_OPEN, MSGL_INFO, "STREAM_HTTP(1), URL: %s\n", stream->url); | |
893 seekable = http_streaming_start(stream, file_format); | |
894 if((seekable < 0) || (*file_format == DEMUXER_TYPE_ASF)) { | |
895 streaming_ctrl_free(stream->streaming_ctrl); | |
896 stream->streaming_ctrl = NULL; | |
897 return STREAM_UNSUPORTED; | |
898 } | |
899 | |
900 return fixup_open(stream, seekable); | |
901 } | |
902 | |
903 static int open_s2(stream_t *stream,int mode, void* opts, int* file_format) { | |
904 int seekable=0; | |
905 URL_t *url; | |
906 | |
907 stream->streaming_ctrl = streaming_ctrl_new(); | |
908 if( stream->streaming_ctrl==NULL ) { | |
909 return STREAM_ERROR; | |
910 } | |
911 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
912 url = url_new(stream->url); | |
913 stream->streaming_ctrl->url = check4proxies(url); | |
914 url_free(url); | |
915 | |
916 mp_msg(MSGT_OPEN, MSGL_INFO, "STREAM_HTTP(2), URL: %s\n", stream->url); | |
917 seekable = http_streaming_start(stream, file_format); | |
918 if(seekable < 0) { | |
919 streaming_ctrl_free(stream->streaming_ctrl); | |
920 stream->streaming_ctrl = NULL; | |
921 return STREAM_UNSUPORTED; | |
922 } | |
923 | |
924 return fixup_open(stream, seekable); | |
925 } | |
926 | |
927 | |
928 stream_info_t stream_info_http1 = { | |
929 "http streaming", | |
930 "null", | |
931 "Bertrand, Albeau, Reimar Doeffinger, Arpi?", | |
932 "plain http", | |
933 open_s1, | |
934 {"http", "http_proxy", "unsv", NULL}, | |
935 NULL, | |
936 0 // Urls are an option string | |
937 }; | |
938 | |
939 stream_info_t stream_info_http2 = { | |
940 "http streaming", | |
941 "null", | |
942 "Bertrand, Albeu, Arpi? who?", | |
943 "plain http, also used as fallback for many other protocols", | |
944 open_s2, | |
945 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback | |
946 NULL, | |
947 0 // Urls are an option string | |
948 }; |