4 * Copyright (C) 2001 Bertrand Baudet <bertrand_baudet@yahoo.com>
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
41 #include "libmpdemux/demuxer.h"
46 extern const mime_struct_t mime_type_table
[];
47 extern int stream_cache_size
;
48 extern int network_bandwidth
;
50 int http_seek(stream_t
*stream
, off_t pos
);
59 * \brief first read any data from sc->buffer then from fd
60 * \param fd file descriptor to read data from
61 * \param buffer buffer to read into
62 * \param len how many bytes to read
63 * \param sc streaming control containing buffer to read from first
64 * \return len unless there is a read error or eof
66 static unsigned my_read(int fd
, char *buffer
, int len
, streaming_ctrl_t
*sc
) {
68 unsigned cp_len
= sc
->buffer_size
- sc
->buffer_pos
;
71 memcpy(buffer
, &sc
->buffer
[sc
->buffer_pos
], cp_len
);
72 sc
->buffer_pos
+= cp_len
;
75 int ret
= recv(fd
, &buffer
[pos
], len
- pos
, 0);
84 * \brief read and process (i.e. discard *g*) a block of ultravox metadata
85 * \param fd file descriptor to read from
86 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
87 * \return number of real data before next metadata block starts or 0 on error
89 static unsigned uvox_meta_read(int fd
, streaming_ctrl_t
*sc
) {
91 unsigned char info
[6] = {0, 0, 0, 0, 0, 0};
94 info_read
= my_read(fd
, info
, 1, sc
);
96 info_read
= my_read(fd
, info
, 6, sc
);
98 info_read
+= my_read(fd
, &info
[1], 5, sc
);
99 if (info_read
!= 6) // read error or eof
101 // sync byte and reserved flags
102 if (info
[0] != 0x5a || (info
[1] & 0xfc) != 0x00) {
103 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "Invalid or unknown uvox metadata\n");
107 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Encrypted ultravox data\n");
108 metaint
= info
[4] << 8 | info
[5];
109 if ((info
[3] & 0xf) < 0x07) { // discard any metadata nonsense
110 char *metabuf
= malloc(metaint
);
111 my_read(fd
, metabuf
, metaint
, sc
);
114 } while ((info
[3] & 0xf) < 0x07);
119 * \brief read one scast meta data entry and print it
120 * \param fd file descriptor to read from
121 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
123 static void scast_meta_read(int fd
, streaming_ctrl_t
*sc
) {
124 unsigned char tmp
= 0;
126 my_read(fd
, &tmp
, 1, sc
);
129 char *info
= malloc(metalen
+ 1);
130 unsigned nlen
= my_read(fd
, info
, metalen
, sc
);
132 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "\nICY Info: %s\n", info
);
138 * \brief read data from scast/ultravox stream without any metadata
139 * \param fd file descriptor to read from
140 * \param buffer buffer to read data into
141 * \param size number of bytes to read
142 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
144 static int scast_streaming_read(int fd
, char *buffer
, int size
,
145 streaming_ctrl_t
*sc
) {
146 scast_data_t
*sd
= (scast_data_t
*)sc
->data
;
150 // first read remaining data up to next metadata
151 block
= sd
->metaint
- sd
->metapos
;
154 ret
= my_read(fd
, buffer
, block
, sc
);
157 if (ret
!= block
) // read problems or eof
160 while (done
< size
) { // now comes the metadata
163 sd
->metaint
= uvox_meta_read(fd
, sc
);
168 scast_meta_read(fd
, sc
); // read and display metadata
171 if (block
> sd
->metaint
)
173 ret
= my_read(fd
, &buffer
[done
], block
, sc
);
176 if (ret
!= block
) // read problems or eof
182 static int scast_streaming_start(stream_t
*stream
) {
184 scast_data_t
*scast_data
;
185 HTTP_header_t
*http_hdr
= stream
->streaming_ctrl
->data
;
186 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
187 if (!stream
|| stream
->fd
< 0 || !http_hdr
)
192 metaint
= atoi(http_get_field(http_hdr
, "Icy-MetaInt"));
196 stream
->streaming_ctrl
->buffer
= malloc(http_hdr
->body_size
);
197 stream
->streaming_ctrl
->buffer_size
= http_hdr
->body_size
;
198 stream
->streaming_ctrl
->buffer_pos
= 0;
199 memcpy(stream
->streaming_ctrl
->buffer
, http_hdr
->body
, http_hdr
->body_size
);
200 scast_data
= malloc(sizeof(scast_data_t
));
201 scast_data
->metaint
= metaint
;
202 scast_data
->metapos
= 0;
203 scast_data
->is_ultravox
= is_ultravox
;
205 stream
->streaming_ctrl
->data
= scast_data
;
206 stream
->streaming_ctrl
->streaming_read
= scast_streaming_read
;
207 stream
->streaming_ctrl
->streaming_seek
= NULL
;
208 stream
->streaming_ctrl
->prebuffer_size
= 64 * 1024; // 64 KBytes
209 stream
->streaming_ctrl
->buffering
= 1;
210 stream
->streaming_ctrl
->status
= streaming_playing_e
;
214 static int nop_streaming_start( stream_t
*stream
) {
215 HTTP_header_t
*http_hdr
= NULL
;
219 if( stream
==NULL
) return -1;
223 fd
= http_send_request( stream
->streaming_ctrl
->url
, 0 );
224 if( fd
<0 ) return -1;
225 http_hdr
= http_read_response( fd
);
226 if( http_hdr
==NULL
) return -1;
228 switch( http_hdr
->status_code
) {
230 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", http_get_field(http_hdr
, "Content-Type") );
231 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", http_get_field(http_hdr
, "Content-Length") );
232 if( http_hdr
->body_size
>0 ) {
233 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
234 http_free( http_hdr
);
240 case 301: // Permanently
241 case 302: // Temporarily
242 case 303: // See Other
244 next_url
= http_get_field( http_hdr
, "Location" );
246 if (next_url
!= NULL
)
247 rd_url
=url_new(next_url
);
249 if (next_url
!= NULL
&& rd_url
!= NULL
) {
250 mp_msg(MSGT_NETWORK
,MSGL_STATUS
,"Redirected: Using this url instead %s\n",next_url
);
251 stream
->streaming_ctrl
->url
=check4proxies(rd_url
);
252 ret
=nop_streaming_start(stream
); //recursively get streaming started
254 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Redirection failed\n");
260 case 401: //Authorization required
261 case 403: //Forbidden
262 case 404: //Not found
263 case 500: //Server Error
265 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Server returned code %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
273 http_hdr
= (HTTP_header_t
*)stream
->streaming_ctrl
->data
;
274 if( http_hdr
->body_size
>0 ) {
275 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
276 http_free( http_hdr
);
277 stream
->streaming_ctrl
->data
= NULL
;
284 http_free( http_hdr
);
285 stream
->streaming_ctrl
->data
= NULL
;
288 stream
->streaming_ctrl
->streaming_read
= nop_streaming_read
;
289 stream
->streaming_ctrl
->streaming_seek
= nop_streaming_seek
;
290 stream
->streaming_ctrl
->prebuffer_size
= 64*1024; // 64 KBytes
291 stream
->streaming_ctrl
->buffering
= 1;
292 stream
->streaming_ctrl
->status
= streaming_playing_e
;
297 http_new_header(void) {
298 HTTP_header_t
*http_hdr
;
300 http_hdr
= malloc(sizeof(HTTP_header_t
));
301 if( http_hdr
==NULL
) return NULL
;
302 memset( http_hdr
, 0, sizeof(HTTP_header_t
) );
308 http_free( HTTP_header_t
*http_hdr
) {
309 HTTP_field_t
*field
, *field2free
;
310 if( http_hdr
==NULL
) return;
311 if( http_hdr
->protocol
!=NULL
) free( http_hdr
->protocol
);
312 if( http_hdr
->uri
!=NULL
) free( http_hdr
->uri
);
313 if( http_hdr
->reason_phrase
!=NULL
) free( http_hdr
->reason_phrase
);
314 if( http_hdr
->field_search
!=NULL
) free( http_hdr
->field_search
);
315 if( http_hdr
->method
!=NULL
) free( http_hdr
->method
);
316 if( http_hdr
->buffer
!=NULL
) free( http_hdr
->buffer
);
317 field
= http_hdr
->first_field
;
318 while( field
!=NULL
) {
320 if (field
->field_name
)
321 free(field
->field_name
);
330 http_response_append( HTTP_header_t
*http_hdr
, char *response
, int length
) {
331 if( http_hdr
==NULL
|| response
==NULL
|| length
<0 ) return -1;
333 if( (unsigned)length
> SIZE_MAX
- http_hdr
->buffer_size
- 1) {
334 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Bad size in memory (re)allocation\n");
337 http_hdr
->buffer
= (char*)realloc( http_hdr
->buffer
, http_hdr
->buffer_size
+length
+1 );
338 if( http_hdr
->buffer
==NULL
) {
339 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory (re)allocation failed\n");
342 memcpy( http_hdr
->buffer
+http_hdr
->buffer_size
, response
, length
);
343 http_hdr
->buffer_size
+= length
;
344 http_hdr
->buffer
[http_hdr
->buffer_size
]=0; // close the string!
345 return http_hdr
->buffer_size
;
349 http_is_header_entire( HTTP_header_t
*http_hdr
) {
350 if( http_hdr
==NULL
) return -1;
351 if( http_hdr
->buffer
==NULL
) return 0; // empty
353 if( strstr(http_hdr
->buffer
, "\r\n\r\n")==NULL
&&
354 strstr(http_hdr
->buffer
, "\n\n")==NULL
) return 0;
359 http_response_parse( HTTP_header_t
*http_hdr
) {
362 int pos_hdr_sep
, hdr_sep_len
;
364 if( http_hdr
==NULL
) return -1;
365 if( http_hdr
->is_parsed
) return 0;
368 hdr_ptr
= strstr( http_hdr
->buffer
, " " );
369 if( hdr_ptr
==NULL
) {
370 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. No space separator found.\n");
373 len
= hdr_ptr
-http_hdr
->buffer
;
374 http_hdr
->protocol
= malloc(len
+1);
375 if( http_hdr
->protocol
==NULL
) {
376 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
379 strncpy( http_hdr
->protocol
, http_hdr
->buffer
, len
);
380 http_hdr
->protocol
[len
]='\0';
381 if( !strncasecmp( http_hdr
->protocol
, "HTTP", 4) ) {
382 if( sscanf( http_hdr
->protocol
+5,"1.%d", &(http_hdr
->http_minor_version
) )!=1 ) {
383 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get HTTP minor version.\n");
388 // Get the status code
389 if( sscanf( ++hdr_ptr
, "%d", &(http_hdr
->status_code
) )!=1 ) {
390 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get status code.\n");
395 // Get the reason phrase
396 ptr
= strstr( hdr_ptr
, "\n" );
397 if( hdr_ptr
==NULL
) {
398 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get the reason phrase.\n");
402 http_hdr
->reason_phrase
= malloc(len
+1);
403 if( http_hdr
->reason_phrase
==NULL
) {
404 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
407 strncpy( http_hdr
->reason_phrase
, hdr_ptr
, len
);
408 if( http_hdr
->reason_phrase
[len
-1]=='\r' ) {
411 http_hdr
->reason_phrase
[len
]='\0';
413 // Set the position of the header separator: \r\n\r\n
415 ptr
= strstr( http_hdr
->buffer
, "\r\n\r\n" );
417 ptr
= strstr( http_hdr
->buffer
, "\n\n" );
419 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Header may be incomplete. No CRLF CRLF found.\n");
424 pos_hdr_sep
= ptr
-http_hdr
->buffer
;
426 // Point to the first line after the method line.
427 hdr_ptr
= strstr( http_hdr
->buffer
, "\n" )+1;
430 while( *ptr
!='\r' && *ptr
!='\n' ) ptr
++;
433 field
= (char*)realloc(field
, len
+1);
435 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
438 strncpy( field
, hdr_ptr
, len
);
440 http_set_field( http_hdr
, field
);
441 hdr_ptr
= ptr
+((*ptr
=='\r')?2:1);
442 } while( hdr_ptr
<(http_hdr
->buffer
+pos_hdr_sep
) );
444 if( field
!=NULL
) free( field
);
446 if( pos_hdr_sep
+hdr_sep_len
<http_hdr
->buffer_size
) {
447 // Response has data!
448 http_hdr
->body
= http_hdr
->buffer
+pos_hdr_sep
+hdr_sep_len
;
449 http_hdr
->body_size
= http_hdr
->buffer_size
-(pos_hdr_sep
+hdr_sep_len
);
452 http_hdr
->is_parsed
= 1;
457 http_build_request( HTTP_header_t
*http_hdr
) {
458 char *ptr
, *uri
=NULL
;
461 if( http_hdr
==NULL
) return NULL
;
463 if( http_hdr
->method
==NULL
) http_set_method( http_hdr
, "GET");
464 if( http_hdr
->uri
==NULL
) http_set_uri( http_hdr
, "/");
466 uri
= malloc(strlen(http_hdr
->uri
) + 1);
468 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
471 strcpy(uri
,http_hdr
->uri
);
474 //**** Compute the request length
475 // Add the Method line
476 len
= strlen(http_hdr
->method
)+strlen(uri
)+12;
478 field
= http_hdr
->first_field
;
479 while( field
!=NULL
) {
480 len
+= strlen(field
->field_name
)+2;
486 if( http_hdr
->body
!=NULL
) {
487 len
+= http_hdr
->body_size
;
489 // Free the buffer if it was previously used
490 if( http_hdr
->buffer
!=NULL
) {
491 free( http_hdr
->buffer
);
492 http_hdr
->buffer
= NULL
;
494 http_hdr
->buffer
= malloc(len
+1);
495 if( http_hdr
->buffer
==NULL
) {
496 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
499 http_hdr
->buffer_size
= len
;
501 //*** Building the request
502 ptr
= http_hdr
->buffer
;
503 // Add the method line
504 ptr
+= sprintf( ptr
, "%s %s HTTP/1.%d\r\n", http_hdr
->method
, uri
, http_hdr
->http_minor_version
);
505 field
= http_hdr
->first_field
;
507 while( field
!=NULL
) {
508 ptr
+= sprintf( ptr
, "%s\r\n", field
->field_name
);
511 ptr
+= sprintf( ptr
, "\r\n" );
513 if( http_hdr
->body
!=NULL
) {
514 memcpy( ptr
, http_hdr
->body
, http_hdr
->body_size
);
517 if( uri
) free( uri
);
518 return http_hdr
->buffer
;
522 http_get_field( HTTP_header_t
*http_hdr
, const char *field_name
) {
523 if( http_hdr
==NULL
|| field_name
==NULL
) return NULL
;
524 http_hdr
->field_search_pos
= http_hdr
->first_field
;
525 http_hdr
->field_search
= (char*)realloc( http_hdr
->field_search
, strlen(field_name
)+1 );
526 if( http_hdr
->field_search
==NULL
) {
527 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
530 strcpy( http_hdr
->field_search
, field_name
);
531 return http_get_next_field( http_hdr
);
535 http_get_next_field( HTTP_header_t
*http_hdr
) {
538 if( http_hdr
==NULL
) return NULL
;
540 field
= http_hdr
->field_search_pos
;
541 while( field
!=NULL
) {
542 ptr
= strstr( field
->field_name
, ":" );
543 if( ptr
==NULL
) return NULL
;
544 if( !strncasecmp( field
->field_name
, http_hdr
->field_search
, ptr
-(field
->field_name
) ) ) {
545 ptr
++; // Skip the column
546 while( ptr
[0]==' ' ) ptr
++; // Skip the spaces if there is some
547 http_hdr
->field_search_pos
= field
->next
;
548 return ptr
; // return the value without the field name
556 http_set_field( HTTP_header_t
*http_hdr
, const char *field_name
) {
557 HTTP_field_t
*new_field
;
558 if( http_hdr
==NULL
|| field_name
==NULL
) return;
560 new_field
= malloc(sizeof(HTTP_field_t
));
561 if( new_field
==NULL
) {
562 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
565 new_field
->next
= NULL
;
566 new_field
->field_name
= malloc(strlen(field_name
)+1);
567 if( new_field
->field_name
==NULL
) {
568 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
572 strcpy( new_field
->field_name
, field_name
);
574 if( http_hdr
->last_field
==NULL
) {
575 http_hdr
->first_field
= new_field
;
577 http_hdr
->last_field
->next
= new_field
;
579 http_hdr
->last_field
= new_field
;
580 http_hdr
->field_nb
++;
584 http_set_method( HTTP_header_t
*http_hdr
, const char *method
) {
585 if( http_hdr
==NULL
|| method
==NULL
) return;
587 http_hdr
->method
= malloc(strlen(method
)+1);
588 if( http_hdr
->method
==NULL
) {
589 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
592 strcpy( http_hdr
->method
, method
);
596 http_set_uri( HTTP_header_t
*http_hdr
, const char *uri
) {
597 if( http_hdr
==NULL
|| uri
==NULL
) return;
599 http_hdr
->uri
= malloc(strlen(uri
)+1);
600 if( http_hdr
->uri
==NULL
) {
601 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
604 strcpy( http_hdr
->uri
, uri
);
608 http_add_basic_authentication( HTTP_header_t
*http_hdr
, const char *username
, const char *password
) {
609 char *auth
= NULL
, *usr_pass
= NULL
, *b64_usr_pass
= NULL
;
610 int encoded_len
, pass_len
=0, out_len
;
612 if( http_hdr
==NULL
|| username
==NULL
) return -1;
614 if( password
!=NULL
) {
615 pass_len
= strlen(password
);
618 usr_pass
= malloc(strlen(username
)+pass_len
+2);
619 if( usr_pass
==NULL
) {
620 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
624 sprintf( usr_pass
, "%s:%s", username
, (password
==NULL
)?"":password
);
626 // Base 64 encode with at least 33% more data than the original size
627 encoded_len
= strlen(usr_pass
)*2;
628 b64_usr_pass
= malloc(encoded_len
);
629 if( b64_usr_pass
==NULL
) {
630 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
634 out_len
= base64_encode( usr_pass
, strlen(usr_pass
), b64_usr_pass
, encoded_len
);
636 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Base64 out overflow\n");
640 b64_usr_pass
[out_len
]='\0';
642 auth
= malloc(encoded_len
+22);
644 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
648 sprintf( auth
, "Authorization: Basic %s", b64_usr_pass
);
649 http_set_field( http_hdr
, auth
);
654 free( b64_usr_pass
);
661 http_debug_hdr( HTTP_header_t
*http_hdr
) {
664 if( http_hdr
==NULL
) return;
666 mp_msg(MSGT_NETWORK
,MSGL_V
,"--- HTTP DEBUG HEADER --- START ---\n");
667 mp_msg(MSGT_NETWORK
,MSGL_V
,"protocol: [%s]\n", http_hdr
->protocol
);
668 mp_msg(MSGT_NETWORK
,MSGL_V
,"http minor version: [%d]\n", http_hdr
->http_minor_version
);
669 mp_msg(MSGT_NETWORK
,MSGL_V
,"uri: [%s]\n", http_hdr
->uri
);
670 mp_msg(MSGT_NETWORK
,MSGL_V
,"method: [%s]\n", http_hdr
->method
);
671 mp_msg(MSGT_NETWORK
,MSGL_V
,"status code: [%d]\n", http_hdr
->status_code
);
672 mp_msg(MSGT_NETWORK
,MSGL_V
,"reason phrase: [%s]\n", http_hdr
->reason_phrase
);
673 mp_msg(MSGT_NETWORK
,MSGL_V
,"body size: [%d]\n", http_hdr
->body_size
);
675 mp_msg(MSGT_NETWORK
,MSGL_V
,"Fields:\n");
676 field
= http_hdr
->first_field
;
677 while( field
!=NULL
) {
678 mp_msg(MSGT_NETWORK
,MSGL_V
," %d - %s\n", i
++, field
->field_name
);
681 mp_msg(MSGT_NETWORK
,MSGL_V
,"--- HTTP DEBUG HEADER --- END ---\n");
685 base64_encode(const void *enc
, int encLen
, char *out
, int outMax
) {
686 static const char b64
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
688 unsigned char *encBuf
;
693 encBuf
= (unsigned char*)enc
;
708 } else if( shift
>0 ) {
709 // Pad last bits to 6 bits - will end next loop
713 // As per RFC 2045, section 6.8,
714 // pad output as necessary: 0 to 2 '=' chars.
723 // Encode 6 bit segments
725 if (outLen
>= outMax
)
728 *out
= b64
[ (bits
>> shift
) & 0x3F ];
735 static void print_icy_metadata(HTTP_header_t
*http_hdr
) {
736 const char *field_data
;
737 // note: I skip icy-notice1 and 2, as they contain html <BR>
738 // and are IMHO useless info ::atmos
739 if( (field_data
= http_get_field(http_hdr
, "icy-name")) != NULL
)
740 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Name : %s\n", field_data
);
741 if( (field_data
= http_get_field(http_hdr
, "icy-genre")) != NULL
)
742 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Genre : %s\n", field_data
);
743 if( (field_data
= http_get_field(http_hdr
, "icy-url")) != NULL
)
744 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Website: %s\n", field_data
);
745 // XXX: does this really mean public server? ::atmos
746 if( (field_data
= http_get_field(http_hdr
, "icy-pub")) != NULL
)
747 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Public : %s\n", atoi(field_data
)?"yes":"no");
748 if( (field_data
= http_get_field(http_hdr
, "icy-br")) != NULL
)
749 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Bitrate: %skbit/s\n", field_data
);
752 //! If this function succeeds you must closesocket stream->fd
753 static int http_streaming_start(stream_t
*stream
, int* file_format
) {
754 HTTP_header_t
*http_hdr
= NULL
;
757 int res
= STREAM_UNSUPPORTED
;
762 const char *content_length
;
764 URL_t
*url
= stream
->streaming_ctrl
->url
;
769 if (fd
> 0) closesocket(fd
);
770 fd
= http_send_request( url
, 0 );
776 http_hdr
= http_read_response( fd
);
777 if( http_hdr
==NULL
) {
781 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) ) {
782 http_debug_hdr( http_hdr
);
785 // Check if we can make partial content requests and thus seek in http-streams
786 if( http_hdr
!=NULL
&& http_hdr
->status_code
==200 ) {
787 const char *accept_ranges
= http_get_field(http_hdr
,"Accept-Ranges");
788 const char *server
= http_get_field(http_hdr
, "Server");
790 seekable
= strncmp(accept_ranges
,"bytes",5)==0;
791 else if (server
&& strcmp(server
, "gvs 1.0") == 0)
792 seekable
= 1; // HACK for youtube incorrectly claiming not to support seeking
795 print_icy_metadata(http_hdr
);
797 // Check if the response is an ICY status_code reason_phrase
798 if( !strcasecmp(http_hdr
->protocol
, "ICY") ||
799 http_get_field(http_hdr
, "Icy-MetaInt") ) {
800 switch( http_hdr
->status_code
) {
803 // If content-type == video/nsv we most likely have a winamp video stream
804 // otherwise it should be mp3. if there are more types consider adding mime type
805 // handling like later
806 if ( (field_data
= http_get_field(http_hdr
, "content-type")) != NULL
&& (!strcmp(field_data
, "video/nsv") || !strcmp(field_data
, "misc/ultravox")))
807 *file_format
= DEMUXER_TYPE_NSV
;
808 else if ( (field_data
= http_get_field(http_hdr
, "content-type")) != NULL
&& (!strcmp(field_data
, "audio/aacp") || !strcmp(field_data
, "audio/aac")))
809 *file_format
= DEMUXER_TYPE_AAC
;
811 *file_format
= DEMUXER_TYPE_AUDIO
;
815 case 400: // Server Full
816 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server is full, skipping!\n");
818 case 401: // Service Unavailable
819 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server return service unavailable, skipping!\n");
821 case 403: // Service Forbidden
822 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server return 'Service Forbidden'\n");
824 case 404: // Resource Not Found
825 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server couldn't find requested stream, skipping!\n");
828 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
833 // Assume standard http if not ICY
834 switch( http_hdr
->status_code
) {
836 content_length
= http_get_field(http_hdr
, "Content-Length");
837 if (content_length
) {
838 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", content_length
);
839 stream
->end_pos
= atoll(content_length
);
841 // Look if we can use the Content-Type
842 content_type
= http_get_field( http_hdr
, "Content-Type" );
843 if( content_type
!=NULL
) {
844 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", content_type
);
845 // Check in the mime type table for a demuxer type
847 while(mime_type_table
[i
].mime_type
!= NULL
) {
848 if( !strcasecmp( content_type
, mime_type_table
[i
].mime_type
) ) {
849 *file_format
= mime_type_table
[i
].demuxer_type
;
856 // Not found in the mime type table, don't fail,
857 // we should try raw HTTP
861 case 301: // Permanently
862 case 302: // Temporarily
863 case 303: // See Other
864 // TODO: RFC 2616, recommand to detect infinite redirection loops
865 next_url
= http_get_field( http_hdr
, "Location" );
866 if( next_url
!=NULL
) {
867 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
868 stream
->streaming_ctrl
->url
= url_redirect( &url
, next_url
);
869 if (!strcasecmp(url
->protocol
, "mms")) {
870 res
= STREAM_REDIRECTED
;
873 if (strcasecmp(url
->protocol
, "http")) {
874 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Unsupported http %d redirect to %s protocol\n", http_hdr
->status_code
, url
->protocol
);
879 url
->protocol
= strdup("unsv");
884 case 401: // Authentication required
885 if( http_authenticate(http_hdr
, url
, &auth_retry
)<0 )
890 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Server returned %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
896 if (fd
> 0) closesocket( fd
);
898 http_free( http_hdr
);
901 stream
->streaming_ctrl
->data
= (void*)http_hdr
;
906 static int fixup_open(stream_t
*stream
,int seekable
) {
907 HTTP_header_t
*http_hdr
= stream
->streaming_ctrl
->data
;
908 int is_icy
= http_hdr
&& http_get_field(http_hdr
, "Icy-MetaInt");
909 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
911 stream
->type
= STREAMTYPE_STREAM
;
912 if(!is_icy
&& !is_ultravox
&& seekable
)
914 stream
->flags
|= MP_STREAM_SEEK
;
915 stream
->seek
= http_seek
;
917 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
918 if ((!is_icy
&& !is_ultravox
) || scast_streaming_start(stream
))
919 if(nop_streaming_start( stream
)) {
920 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_start failed\n");
922 closesocket(stream
->fd
);
924 streaming_ctrl_free(stream
->streaming_ctrl
);
925 stream
->streaming_ctrl
= NULL
;
926 return STREAM_UNSUPPORTED
;
929 fixup_network_stream_cache(stream
);
933 static int open_s1(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
937 stream
->streaming_ctrl
= streaming_ctrl_new();
938 if( stream
->streaming_ctrl
==NULL
) {
941 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
942 url
= url_new(stream
->url
);
943 stream
->streaming_ctrl
->url
= check4proxies(url
);
946 mp_msg(MSGT_OPEN
, MSGL_V
, "STREAM_HTTP(1), URL: %s\n", stream
->url
);
947 seekable
= http_streaming_start(stream
, file_format
);
948 if((seekable
< 0) || (*file_format
== DEMUXER_TYPE_ASF
)) {
950 closesocket(stream
->fd
);
952 if (seekable
== STREAM_REDIRECTED
)
954 streaming_ctrl_free(stream
->streaming_ctrl
);
955 stream
->streaming_ctrl
= NULL
;
956 return STREAM_UNSUPPORTED
;
959 return fixup_open(stream
, seekable
);
962 static int open_s2(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
966 stream
->streaming_ctrl
= streaming_ctrl_new();
967 if( stream
->streaming_ctrl
==NULL
) {
970 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
971 url
= url_new(stream
->url
);
972 stream
->streaming_ctrl
->url
= check4proxies(url
);
975 mp_msg(MSGT_OPEN
, MSGL_V
, "STREAM_HTTP(2), URL: %s\n", stream
->url
);
976 seekable
= http_streaming_start(stream
, file_format
);
979 closesocket(stream
->fd
);
981 streaming_ctrl_free(stream
->streaming_ctrl
);
982 stream
->streaming_ctrl
= NULL
;
983 return STREAM_UNSUPPORTED
;
986 return fixup_open(stream
, seekable
);
990 const stream_info_t stream_info_http1
= {
993 "Bertrand, Albeau, Reimar Doeffinger, Arpi?",
996 {"http", "http_proxy", "unsv", "icyx", "noicyx", NULL
},
998 0 // Urls are an option string
1001 const stream_info_t stream_info_http2
= {
1004 "Bertrand, Albeu, Arpi? who?",
1005 "plain http, also used as fallback for many other protocols",
1007 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL
}, //all the others as fallback
1009 0 // Urls are an option string