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
;
57 * \brief first read any data from sc->buffer then from fd
58 * \param fd file descriptor to read data from
59 * \param buffer buffer to read into
60 * \param len how many bytes to read
61 * \param sc streaming control containing buffer to read from first
62 * \return len unless there is a read error or eof
64 static unsigned my_read(int fd
, char *buffer
, int len
, streaming_ctrl_t
*sc
) {
66 unsigned cp_len
= sc
->buffer_size
- sc
->buffer_pos
;
69 memcpy(buffer
, &sc
->buffer
[sc
->buffer_pos
], cp_len
);
70 sc
->buffer_pos
+= cp_len
;
73 int ret
= recv(fd
, &buffer
[pos
], len
- pos
, 0);
82 * \brief read and process (i.e. discard *g*) a block of ultravox metadata
83 * \param fd file descriptor to read from
84 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
85 * \return number of real data before next metadata block starts or 0 on error
87 static unsigned uvox_meta_read(int fd
, streaming_ctrl_t
*sc
) {
89 unsigned char info
[6] = {0, 0, 0, 0, 0, 0};
92 info_read
= my_read(fd
, info
, 1, sc
);
94 info_read
= my_read(fd
, info
, 6, sc
);
96 info_read
+= my_read(fd
, &info
[1], 5, sc
);
97 if (info_read
!= 6) // read error or eof
99 // sync byte and reserved flags
100 if (info
[0] != 0x5a || (info
[1] & 0xfc) != 0x00) {
101 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "Invalid or unknown uvox metadata\n");
105 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Encrypted ultravox data\n");
106 metaint
= info
[4] << 8 | info
[5];
107 if ((info
[3] & 0xf) < 0x07) { // discard any metadata nonsense
108 char *metabuf
= malloc(metaint
);
109 my_read(fd
, metabuf
, metaint
, sc
);
112 } while ((info
[3] & 0xf) < 0x07);
117 * \brief read one scast meta data entry and print it
118 * \param fd file descriptor to read from
119 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
121 static void scast_meta_read(int fd
, streaming_ctrl_t
*sc
) {
122 unsigned char tmp
= 0;
124 my_read(fd
, &tmp
, 1, sc
);
127 char *info
= malloc(metalen
+ 1);
128 unsigned nlen
= my_read(fd
, info
, metalen
, sc
);
130 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "\nICY Info: %s\n", info
);
136 * \brief read data from scast/ultravox stream without any metadata
137 * \param fd file descriptor to read from
138 * \param buffer buffer to read data into
139 * \param size number of bytes to read
140 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
142 static int scast_streaming_read(int fd
, char *buffer
, int size
,
143 streaming_ctrl_t
*sc
) {
144 scast_data_t
*sd
= (scast_data_t
*)sc
->data
;
148 // first read remaining data up to next metadata
149 block
= sd
->metaint
- sd
->metapos
;
152 ret
= my_read(fd
, buffer
, block
, sc
);
155 if (ret
!= block
) // read problems or eof
158 while (done
< size
) { // now comes the metadata
161 sd
->metaint
= uvox_meta_read(fd
, sc
);
166 scast_meta_read(fd
, sc
); // read and display metadata
169 if (block
> sd
->metaint
)
171 ret
= my_read(fd
, &buffer
[done
], block
, sc
);
174 if (ret
!= block
) // read problems or eof
180 static int scast_streaming_start(stream_t
*stream
) {
182 scast_data_t
*scast_data
;
183 HTTP_header_t
*http_hdr
= stream
->streaming_ctrl
->data
;
184 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
185 if (!stream
|| stream
->fd
< 0 || !http_hdr
)
190 metaint
= atoi(http_get_field(http_hdr
, "Icy-MetaInt"));
194 stream
->streaming_ctrl
->buffer
= malloc(http_hdr
->body_size
);
195 stream
->streaming_ctrl
->buffer_size
= http_hdr
->body_size
;
196 stream
->streaming_ctrl
->buffer_pos
= 0;
197 memcpy(stream
->streaming_ctrl
->buffer
, http_hdr
->body
, http_hdr
->body_size
);
198 scast_data
= malloc(sizeof(scast_data_t
));
199 scast_data
->metaint
= metaint
;
200 scast_data
->metapos
= 0;
201 scast_data
->is_ultravox
= is_ultravox
;
203 stream
->streaming_ctrl
->data
= scast_data
;
204 stream
->streaming_ctrl
->streaming_read
= scast_streaming_read
;
205 stream
->streaming_ctrl
->streaming_seek
= NULL
;
206 stream
->streaming_ctrl
->prebuffer_size
= 64 * 1024; // 64 KBytes
207 stream
->streaming_ctrl
->buffering
= 1;
208 stream
->streaming_ctrl
->status
= streaming_playing_e
;
212 static int nop_streaming_start( stream_t
*stream
) {
213 HTTP_header_t
*http_hdr
= NULL
;
217 if( stream
==NULL
) return -1;
221 fd
= http_send_request( stream
->streaming_ctrl
->url
, 0 );
222 if( fd
<0 ) return -1;
223 http_hdr
= http_read_response( fd
);
224 if( http_hdr
==NULL
) return -1;
226 switch( http_hdr
->status_code
) {
228 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", http_get_field(http_hdr
, "Content-Type") );
229 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", http_get_field(http_hdr
, "Content-Length") );
230 if( http_hdr
->body_size
>0 ) {
231 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
232 http_free( http_hdr
);
238 case 301: // Permanently
239 case 302: // Temporarily
240 case 303: // See Other
242 next_url
= http_get_field( http_hdr
, "Location" );
244 if (next_url
!= NULL
)
245 rd_url
=url_new(next_url
);
247 if (next_url
!= NULL
&& rd_url
!= NULL
) {
248 mp_msg(MSGT_NETWORK
,MSGL_STATUS
,"Redirected: Using this url instead %s\n",next_url
);
249 stream
->streaming_ctrl
->url
=check4proxies(rd_url
);
250 ret
=nop_streaming_start(stream
); //recursively get streaming started
252 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Redirection failed\n");
258 case 401: //Authorization required
259 case 403: //Forbidden
260 case 404: //Not found
261 case 500: //Server Error
263 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Server returned code %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
271 http_hdr
= (HTTP_header_t
*)stream
->streaming_ctrl
->data
;
272 if( http_hdr
->body_size
>0 ) {
273 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
274 http_free( http_hdr
);
275 stream
->streaming_ctrl
->data
= NULL
;
282 http_free( http_hdr
);
283 stream
->streaming_ctrl
->data
= NULL
;
286 stream
->streaming_ctrl
->streaming_read
= nop_streaming_read
;
287 stream
->streaming_ctrl
->streaming_seek
= nop_streaming_seek
;
288 stream
->streaming_ctrl
->prebuffer_size
= 64*1024; // 64 KBytes
289 stream
->streaming_ctrl
->buffering
= 1;
290 stream
->streaming_ctrl
->status
= streaming_playing_e
;
295 http_new_header(void) {
296 HTTP_header_t
*http_hdr
;
298 http_hdr
= malloc(sizeof(HTTP_header_t
));
299 if( http_hdr
==NULL
) return NULL
;
300 memset( http_hdr
, 0, sizeof(HTTP_header_t
) );
306 http_free( HTTP_header_t
*http_hdr
) {
307 HTTP_field_t
*field
, *field2free
;
308 if( http_hdr
==NULL
) return;
309 if( http_hdr
->protocol
!=NULL
) free( http_hdr
->protocol
);
310 if( http_hdr
->uri
!=NULL
) free( http_hdr
->uri
);
311 if( http_hdr
->reason_phrase
!=NULL
) free( http_hdr
->reason_phrase
);
312 if( http_hdr
->field_search
!=NULL
) free( http_hdr
->field_search
);
313 if( http_hdr
->method
!=NULL
) free( http_hdr
->method
);
314 if( http_hdr
->buffer
!=NULL
) free( http_hdr
->buffer
);
315 field
= http_hdr
->first_field
;
316 while( field
!=NULL
) {
318 if (field
->field_name
)
319 free(field
->field_name
);
328 http_response_append( HTTP_header_t
*http_hdr
, char *response
, int length
) {
329 if( http_hdr
==NULL
|| response
==NULL
|| length
<0 ) return -1;
331 if( (unsigned)length
> SIZE_MAX
- http_hdr
->buffer_size
- 1) {
332 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Bad size in memory (re)allocation\n");
335 http_hdr
->buffer
= realloc( http_hdr
->buffer
, http_hdr
->buffer_size
+length
+1 );
336 if( http_hdr
->buffer
==NULL
) {
337 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory (re)allocation failed\n");
340 memcpy( http_hdr
->buffer
+http_hdr
->buffer_size
, response
, length
);
341 http_hdr
->buffer_size
+= length
;
342 http_hdr
->buffer
[http_hdr
->buffer_size
]=0; // close the string!
343 return http_hdr
->buffer_size
;
347 http_is_header_entire( HTTP_header_t
*http_hdr
) {
348 if( http_hdr
==NULL
) return -1;
349 if( http_hdr
->buffer
==NULL
) return 0; // empty
351 if( strstr(http_hdr
->buffer
, "\r\n\r\n")==NULL
&&
352 strstr(http_hdr
->buffer
, "\n\n")==NULL
) return 0;
357 http_response_parse( HTTP_header_t
*http_hdr
) {
360 int pos_hdr_sep
, hdr_sep_len
;
362 if( http_hdr
==NULL
) return -1;
363 if( http_hdr
->is_parsed
) return 0;
366 hdr_ptr
= strstr( http_hdr
->buffer
, " " );
367 if( hdr_ptr
==NULL
) {
368 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. No space separator found.\n");
371 len
= hdr_ptr
-http_hdr
->buffer
;
372 http_hdr
->protocol
= malloc(len
+1);
373 if( http_hdr
->protocol
==NULL
) {
374 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
377 strncpy( http_hdr
->protocol
, http_hdr
->buffer
, len
);
378 http_hdr
->protocol
[len
]='\0';
379 if( !strncasecmp( http_hdr
->protocol
, "HTTP", 4) ) {
380 if( sscanf( http_hdr
->protocol
+5,"1.%d", &(http_hdr
->http_minor_version
) )!=1 ) {
381 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get HTTP minor version.\n");
386 // Get the status code
387 if( sscanf( ++hdr_ptr
, "%d", &(http_hdr
->status_code
) )!=1 ) {
388 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get status code.\n");
393 // Get the reason phrase
394 ptr
= strstr( hdr_ptr
, "\n" );
395 if( hdr_ptr
==NULL
) {
396 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get the reason phrase.\n");
400 http_hdr
->reason_phrase
= malloc(len
+1);
401 if( http_hdr
->reason_phrase
==NULL
) {
402 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
405 strncpy( http_hdr
->reason_phrase
, hdr_ptr
, len
);
406 if( http_hdr
->reason_phrase
[len
-1]=='\r' ) {
409 http_hdr
->reason_phrase
[len
]='\0';
411 // Set the position of the header separator: \r\n\r\n
413 ptr
= strstr( http_hdr
->buffer
, "\r\n\r\n" );
415 ptr
= strstr( http_hdr
->buffer
, "\n\n" );
417 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Header may be incomplete. No CRLF CRLF found.\n");
422 pos_hdr_sep
= ptr
-http_hdr
->buffer
;
424 // Point to the first line after the method line.
425 hdr_ptr
= strstr( http_hdr
->buffer
, "\n" )+1;
428 while( *ptr
!='\r' && *ptr
!='\n' ) ptr
++;
431 field
= realloc(field
, len
+1);
433 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
436 strncpy( field
, hdr_ptr
, len
);
438 http_set_field( http_hdr
, field
);
439 hdr_ptr
= ptr
+((*ptr
=='\r')?2:1);
440 } while( hdr_ptr
<(http_hdr
->buffer
+pos_hdr_sep
) );
442 if( field
!=NULL
) free( field
);
444 if( pos_hdr_sep
+hdr_sep_len
<http_hdr
->buffer_size
) {
445 // Response has data!
446 http_hdr
->body
= http_hdr
->buffer
+pos_hdr_sep
+hdr_sep_len
;
447 http_hdr
->body_size
= http_hdr
->buffer_size
-(pos_hdr_sep
+hdr_sep_len
);
450 http_hdr
->is_parsed
= 1;
455 http_build_request( HTTP_header_t
*http_hdr
) {
456 char *ptr
, *uri
=NULL
;
459 if( http_hdr
==NULL
) return NULL
;
461 if( http_hdr
->method
==NULL
) http_set_method( http_hdr
, "GET");
462 if( http_hdr
->uri
==NULL
) http_set_uri( http_hdr
, "/");
464 uri
= malloc(strlen(http_hdr
->uri
) + 1);
466 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
469 strcpy(uri
,http_hdr
->uri
);
472 //**** Compute the request length
473 // Add the Method line
474 len
= strlen(http_hdr
->method
)+strlen(uri
)+12;
476 field
= http_hdr
->first_field
;
477 while( field
!=NULL
) {
478 len
+= strlen(field
->field_name
)+2;
484 if( http_hdr
->body
!=NULL
) {
485 len
+= http_hdr
->body_size
;
487 // Free the buffer if it was previously used
488 if( http_hdr
->buffer
!=NULL
) {
489 free( http_hdr
->buffer
);
490 http_hdr
->buffer
= NULL
;
492 http_hdr
->buffer
= malloc(len
+1);
493 if( http_hdr
->buffer
==NULL
) {
494 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
497 http_hdr
->buffer_size
= len
;
499 //*** Building the request
500 ptr
= http_hdr
->buffer
;
501 // Add the method line
502 ptr
+= sprintf( ptr
, "%s %s HTTP/1.%d\r\n", http_hdr
->method
, uri
, http_hdr
->http_minor_version
);
503 field
= http_hdr
->first_field
;
505 while( field
!=NULL
) {
506 ptr
+= sprintf( ptr
, "%s\r\n", field
->field_name
);
509 ptr
+= sprintf( ptr
, "\r\n" );
511 if( http_hdr
->body
!=NULL
) {
512 memcpy( ptr
, http_hdr
->body
, http_hdr
->body_size
);
515 if( uri
) free( uri
);
516 return http_hdr
->buffer
;
520 http_get_field( HTTP_header_t
*http_hdr
, const char *field_name
) {
521 if( http_hdr
==NULL
|| field_name
==NULL
) return NULL
;
522 http_hdr
->field_search_pos
= http_hdr
->first_field
;
523 http_hdr
->field_search
= realloc( http_hdr
->field_search
, strlen(field_name
)+1 );
524 if( http_hdr
->field_search
==NULL
) {
525 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
528 strcpy( http_hdr
->field_search
, field_name
);
529 return http_get_next_field( http_hdr
);
533 http_get_next_field( HTTP_header_t
*http_hdr
) {
536 if( http_hdr
==NULL
) return NULL
;
538 field
= http_hdr
->field_search_pos
;
539 while( field
!=NULL
) {
540 ptr
= strstr( field
->field_name
, ":" );
541 if( ptr
==NULL
) return NULL
;
542 if( !strncasecmp( field
->field_name
, http_hdr
->field_search
, ptr
-(field
->field_name
) ) ) {
543 ptr
++; // Skip the column
544 while( ptr
[0]==' ' ) ptr
++; // Skip the spaces if there is some
545 http_hdr
->field_search_pos
= field
->next
;
546 return ptr
; // return the value without the field name
554 http_set_field( HTTP_header_t
*http_hdr
, const char *field_name
) {
555 HTTP_field_t
*new_field
;
556 if( http_hdr
==NULL
|| field_name
==NULL
) return;
558 new_field
= malloc(sizeof(HTTP_field_t
));
559 if( new_field
==NULL
) {
560 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
563 new_field
->next
= NULL
;
564 new_field
->field_name
= malloc(strlen(field_name
)+1);
565 if( new_field
->field_name
==NULL
) {
566 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
570 strcpy( new_field
->field_name
, field_name
);
572 if( http_hdr
->last_field
==NULL
) {
573 http_hdr
->first_field
= new_field
;
575 http_hdr
->last_field
->next
= new_field
;
577 http_hdr
->last_field
= new_field
;
578 http_hdr
->field_nb
++;
582 http_set_method( HTTP_header_t
*http_hdr
, const char *method
) {
583 if( http_hdr
==NULL
|| method
==NULL
) return;
585 http_hdr
->method
= malloc(strlen(method
)+1);
586 if( http_hdr
->method
==NULL
) {
587 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
590 strcpy( http_hdr
->method
, method
);
594 http_set_uri( HTTP_header_t
*http_hdr
, const char *uri
) {
595 if( http_hdr
==NULL
|| uri
==NULL
) return;
597 http_hdr
->uri
= malloc(strlen(uri
)+1);
598 if( http_hdr
->uri
==NULL
) {
599 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
602 strcpy( http_hdr
->uri
, uri
);
606 http_add_basic_authentication( HTTP_header_t
*http_hdr
, const char *username
, const char *password
) {
607 char *auth
= NULL
, *usr_pass
= NULL
, *b64_usr_pass
= NULL
;
608 int encoded_len
, pass_len
=0, out_len
;
610 if( http_hdr
==NULL
|| username
==NULL
) return -1;
612 if( password
!=NULL
) {
613 pass_len
= strlen(password
);
616 usr_pass
= malloc(strlen(username
)+pass_len
+2);
617 if( usr_pass
==NULL
) {
618 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
622 sprintf( usr_pass
, "%s:%s", username
, (password
==NULL
)?"":password
);
624 // Base 64 encode with at least 33% more data than the original size
625 encoded_len
= strlen(usr_pass
)*2;
626 b64_usr_pass
= malloc(encoded_len
);
627 if( b64_usr_pass
==NULL
) {
628 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
632 out_len
= base64_encode( usr_pass
, strlen(usr_pass
), b64_usr_pass
, encoded_len
);
634 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Base64 out overflow\n");
638 b64_usr_pass
[out_len
]='\0';
640 auth
= malloc(encoded_len
+22);
642 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
646 sprintf( auth
, "Authorization: Basic %s", b64_usr_pass
);
647 http_set_field( http_hdr
, auth
);
652 free( b64_usr_pass
);
659 http_debug_hdr( HTTP_header_t
*http_hdr
) {
662 if( http_hdr
==NULL
) return;
664 mp_msg(MSGT_NETWORK
,MSGL_V
,"--- HTTP DEBUG HEADER --- START ---\n");
665 mp_msg(MSGT_NETWORK
,MSGL_V
,"protocol: [%s]\n", http_hdr
->protocol
);
666 mp_msg(MSGT_NETWORK
,MSGL_V
,"http minor version: [%d]\n", http_hdr
->http_minor_version
);
667 mp_msg(MSGT_NETWORK
,MSGL_V
,"uri: [%s]\n", http_hdr
->uri
);
668 mp_msg(MSGT_NETWORK
,MSGL_V
,"method: [%s]\n", http_hdr
->method
);
669 mp_msg(MSGT_NETWORK
,MSGL_V
,"status code: [%d]\n", http_hdr
->status_code
);
670 mp_msg(MSGT_NETWORK
,MSGL_V
,"reason phrase: [%s]\n", http_hdr
->reason_phrase
);
671 mp_msg(MSGT_NETWORK
,MSGL_V
,"body size: [%d]\n", http_hdr
->body_size
);
673 mp_msg(MSGT_NETWORK
,MSGL_V
,"Fields:\n");
674 field
= http_hdr
->first_field
;
675 while( field
!=NULL
) {
676 mp_msg(MSGT_NETWORK
,MSGL_V
," %d - %s\n", i
++, field
->field_name
);
679 mp_msg(MSGT_NETWORK
,MSGL_V
,"--- HTTP DEBUG HEADER --- END ---\n");
683 base64_encode(const void *enc
, int encLen
, char *out
, int outMax
) {
684 static const char b64
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
686 unsigned char *encBuf
;
691 encBuf
= (unsigned char*)enc
;
706 } else if( shift
>0 ) {
707 // Pad last bits to 6 bits - will end next loop
711 // As per RFC 2045, section 6.8,
712 // pad output as necessary: 0 to 2 '=' chars.
721 // Encode 6 bit segments
723 if (outLen
>= outMax
)
726 *out
= b64
[ (bits
>> shift
) & 0x3F ];
733 static void print_icy_metadata(HTTP_header_t
*http_hdr
) {
734 const char *field_data
;
735 // note: I skip icy-notice1 and 2, as they contain html <BR>
736 // and are IMHO useless info ::atmos
737 if( (field_data
= http_get_field(http_hdr
, "icy-name")) != NULL
)
738 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Name : %s\n", field_data
);
739 if( (field_data
= http_get_field(http_hdr
, "icy-genre")) != NULL
)
740 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Genre : %s\n", field_data
);
741 if( (field_data
= http_get_field(http_hdr
, "icy-url")) != NULL
)
742 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Website: %s\n", field_data
);
743 // XXX: does this really mean public server? ::atmos
744 if( (field_data
= http_get_field(http_hdr
, "icy-pub")) != NULL
)
745 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Public : %s\n", atoi(field_data
)?"yes":"no");
746 if( (field_data
= http_get_field(http_hdr
, "icy-br")) != NULL
)
747 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Bitrate: %skbit/s\n", field_data
);
750 //! If this function succeeds you must closesocket stream->fd
751 static int http_streaming_start(stream_t
*stream
, int* file_format
) {
752 HTTP_header_t
*http_hdr
= NULL
;
755 int res
= STREAM_UNSUPPORTED
;
760 const char *content_length
;
762 URL_t
*url
= stream
->streaming_ctrl
->url
;
767 if (fd
> 0) closesocket(fd
);
768 fd
= http_send_request( url
, 0 );
774 http_hdr
= http_read_response( fd
);
775 if( http_hdr
==NULL
) {
779 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) ) {
780 http_debug_hdr( http_hdr
);
783 // Check if we can make partial content requests and thus seek in http-streams
784 if( http_hdr
!=NULL
&& http_hdr
->status_code
==200 ) {
785 const char *accept_ranges
= http_get_field(http_hdr
,"Accept-Ranges");
786 const char *server
= http_get_field(http_hdr
, "Server");
788 seekable
= strncmp(accept_ranges
,"bytes",5)==0;
789 else if (server
&& strcmp(server
, "gvs 1.0") == 0)
790 seekable
= 1; // HACK for youtube incorrectly claiming not to support seeking
793 print_icy_metadata(http_hdr
);
795 // Check if the response is an ICY status_code reason_phrase
796 if( !strcasecmp(http_hdr
->protocol
, "ICY") ||
797 http_get_field(http_hdr
, "Icy-MetaInt") ) {
798 switch( http_hdr
->status_code
) {
801 // If content-type == video/nsv we most likely have a winamp video stream
802 // otherwise it should be mp3. if there are more types consider adding mime type
803 // handling like later
804 if ( (field_data
= http_get_field(http_hdr
, "content-type")) != NULL
&& (!strcmp(field_data
, "video/nsv") || !strcmp(field_data
, "misc/ultravox")))
805 *file_format
= DEMUXER_TYPE_NSV
;
806 else if ( (field_data
= http_get_field(http_hdr
, "content-type")) != NULL
&& (!strcmp(field_data
, "audio/aacp") || !strcmp(field_data
, "audio/aac")))
807 *file_format
= DEMUXER_TYPE_AAC
;
809 *file_format
= DEMUXER_TYPE_AUDIO
;
813 case 400: // Server Full
814 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server is full, skipping!\n");
816 case 401: // Service Unavailable
817 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server return service unavailable, skipping!\n");
819 case 403: // Service Forbidden
820 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server return 'Service Forbidden'\n");
822 case 404: // Resource Not Found
823 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server couldn't find requested stream, skipping!\n");
826 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
831 // Assume standard http if not ICY
832 switch( http_hdr
->status_code
) {
834 content_length
= http_get_field(http_hdr
, "Content-Length");
835 if (content_length
) {
836 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", content_length
);
837 stream
->end_pos
= atoll(content_length
);
839 // Look if we can use the Content-Type
840 content_type
= http_get_field( http_hdr
, "Content-Type" );
841 if( content_type
!=NULL
) {
842 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", content_type
);
843 // Check in the mime type table for a demuxer type
845 while(mime_type_table
[i
].mime_type
!= NULL
) {
846 if( !strcasecmp( content_type
, mime_type_table
[i
].mime_type
) ) {
847 *file_format
= mime_type_table
[i
].demuxer_type
;
854 // Not found in the mime type table, don't fail,
855 // we should try raw HTTP
859 case 301: // Permanently
860 case 302: // Temporarily
861 case 303: // See Other
862 // TODO: RFC 2616, recommand to detect infinite redirection loops
863 next_url
= http_get_field( http_hdr
, "Location" );
864 if( next_url
!=NULL
) {
865 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
866 stream
->streaming_ctrl
->url
= url_redirect( &url
, next_url
);
867 if (!strcasecmp(url
->protocol
, "mms")) {
868 res
= STREAM_REDIRECTED
;
871 if (strcasecmp(url
->protocol
, "http")) {
872 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Unsupported http %d redirect to %s protocol\n", http_hdr
->status_code
, url
->protocol
);
877 url
->protocol
= strdup("unsv");
882 case 401: // Authentication required
883 if( http_authenticate(http_hdr
, url
, &auth_retry
)<0 )
888 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Server returned %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
894 if (fd
> 0) closesocket( fd
);
896 http_free( http_hdr
);
899 stream
->streaming_ctrl
->data
= (void*)http_hdr
;
904 static int fixup_open(stream_t
*stream
,int seekable
) {
905 HTTP_header_t
*http_hdr
= stream
->streaming_ctrl
->data
;
906 int is_icy
= http_hdr
&& http_get_field(http_hdr
, "Icy-MetaInt");
907 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
909 stream
->type
= STREAMTYPE_STREAM
;
910 if(!is_icy
&& !is_ultravox
&& seekable
)
912 stream
->flags
|= MP_STREAM_SEEK
;
913 stream
->seek
= http_seek
;
915 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
916 if ((!is_icy
&& !is_ultravox
) || scast_streaming_start(stream
))
917 if(nop_streaming_start( stream
)) {
918 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_start failed\n");
920 closesocket(stream
->fd
);
922 streaming_ctrl_free(stream
->streaming_ctrl
);
923 stream
->streaming_ctrl
= NULL
;
924 return STREAM_UNSUPPORTED
;
927 fixup_network_stream_cache(stream
);
931 static int open_s1(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
935 stream
->streaming_ctrl
= streaming_ctrl_new();
936 if( stream
->streaming_ctrl
==NULL
) {
939 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
940 url
= url_new(stream
->url
);
941 stream
->streaming_ctrl
->url
= check4proxies(url
);
944 mp_msg(MSGT_OPEN
, MSGL_V
, "STREAM_HTTP(1), URL: %s\n", stream
->url
);
945 seekable
= http_streaming_start(stream
, file_format
);
946 if((seekable
< 0) || (*file_format
== DEMUXER_TYPE_ASF
)) {
948 closesocket(stream
->fd
);
950 if (seekable
== STREAM_REDIRECTED
)
952 streaming_ctrl_free(stream
->streaming_ctrl
);
953 stream
->streaming_ctrl
= NULL
;
954 return STREAM_UNSUPPORTED
;
957 return fixup_open(stream
, seekable
);
960 static int open_s2(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
964 stream
->streaming_ctrl
= streaming_ctrl_new();
965 if( stream
->streaming_ctrl
==NULL
) {
968 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
969 url
= url_new(stream
->url
);
970 stream
->streaming_ctrl
->url
= check4proxies(url
);
973 mp_msg(MSGT_OPEN
, MSGL_V
, "STREAM_HTTP(2), URL: %s\n", stream
->url
);
974 seekable
= http_streaming_start(stream
, file_format
);
977 closesocket(stream
->fd
);
979 streaming_ctrl_free(stream
->streaming_ctrl
);
980 stream
->streaming_ctrl
= NULL
;
981 return STREAM_UNSUPPORTED
;
984 return fixup_open(stream
, seekable
);
988 const stream_info_t stream_info_http1
= {
991 "Bertrand, Albeau, Reimar Doeffinger, Arpi?",
994 {"http", "http_proxy", "unsv", "icyx", "noicyx", NULL
},
996 0 // Urls are an option string
999 const stream_info_t stream_info_http2
= {
1002 "Bertrand, Albeu, Arpi? who?",
1003 "plain http, also used as fallback for many other protocols",
1005 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL
}, //all the others as fallback
1007 0 // Urls are an option string