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"
44 #include "libavutil/base64.h"
46 #include <libavutil/avutil.h>
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
);
128 uint8_t *info
= malloc(metalen
+ 1);
129 unsigned nlen
= my_read(fd
, info
, metalen
, sc
);
130 // avoid breaking the user's terminal too much
131 if (nlen
> 256) nlen
= 256;
132 for (i
= 0; i
< nlen
; i
++)
133 if (info
[i
] && info
[i
] < 32) info
[i
] = '?';
135 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "\nICY Info: %s\n", info
);
141 * \brief read data from scast/ultravox stream without any metadata
142 * \param fd file descriptor to read from
143 * \param buffer buffer to read data into
144 * \param size number of bytes to read
145 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
147 static int scast_streaming_read(int fd
, char *buffer
, int size
,
148 streaming_ctrl_t
*sc
) {
149 scast_data_t
*sd
= (scast_data_t
*)sc
->data
;
153 // first read remaining data up to next metadata
154 block
= sd
->metaint
- sd
->metapos
;
157 ret
= my_read(fd
, buffer
, block
, sc
);
160 if (ret
!= block
) // read problems or eof
163 while (done
< size
) { // now comes the metadata
166 sd
->metaint
= uvox_meta_read(fd
, sc
);
171 scast_meta_read(fd
, sc
); // read and display metadata
174 if (block
> sd
->metaint
)
176 ret
= my_read(fd
, &buffer
[done
], block
, sc
);
179 if (ret
!= block
) // read problems or eof
185 static int scast_streaming_start(stream_t
*stream
) {
187 scast_data_t
*scast_data
;
188 HTTP_header_t
*http_hdr
= stream
->streaming_ctrl
->data
;
189 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
190 if (!stream
|| stream
->fd
< 0 || !http_hdr
)
195 metaint
= atoi(http_get_field(http_hdr
, "Icy-MetaInt"));
199 stream
->streaming_ctrl
->buffer
= malloc(http_hdr
->body_size
);
200 stream
->streaming_ctrl
->buffer_size
= http_hdr
->body_size
;
201 stream
->streaming_ctrl
->buffer_pos
= 0;
202 memcpy(stream
->streaming_ctrl
->buffer
, http_hdr
->body
, http_hdr
->body_size
);
203 scast_data
= malloc(sizeof(scast_data_t
));
204 scast_data
->metaint
= metaint
;
205 scast_data
->metapos
= 0;
206 scast_data
->is_ultravox
= is_ultravox
;
208 stream
->streaming_ctrl
->data
= scast_data
;
209 stream
->streaming_ctrl
->streaming_read
= scast_streaming_read
;
210 stream
->streaming_ctrl
->streaming_seek
= NULL
;
211 stream
->streaming_ctrl
->prebuffer_size
= 64 * 1024; // 64 KBytes
212 stream
->streaming_ctrl
->buffering
= 1;
213 stream
->streaming_ctrl
->status
= streaming_playing_e
;
217 static int nop_streaming_start( stream_t
*stream
) {
218 HTTP_header_t
*http_hdr
= NULL
;
222 if( stream
==NULL
) return -1;
226 fd
= http_send_request( stream
->streaming_ctrl
->url
, 0 );
227 if( fd
<0 ) return -1;
228 http_hdr
= http_read_response( fd
);
229 if( http_hdr
==NULL
) return -1;
231 switch( http_hdr
->status_code
) {
233 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", http_get_field(http_hdr
, "Content-Type") );
234 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", http_get_field(http_hdr
, "Content-Length") );
235 if( http_hdr
->body_size
>0 ) {
236 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
237 http_free( http_hdr
);
243 case 301: // Permanently
244 case 302: // Temporarily
245 case 303: // See Other
246 case 307: // Temporarily (since HTTP/1.1)
248 next_url
= http_get_field( http_hdr
, "Location" );
250 if (next_url
!= NULL
)
251 rd_url
=url_new(next_url
);
253 if (next_url
!= NULL
&& rd_url
!= NULL
) {
254 mp_msg(MSGT_NETWORK
,MSGL_STATUS
,"Redirected: Using this url instead %s\n",next_url
);
255 stream
->streaming_ctrl
->url
=check4proxies(rd_url
);
256 ret
=nop_streaming_start(stream
); //recursively get streaming started
258 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Redirection failed\n");
264 case 401: //Authorization required
265 case 403: //Forbidden
266 case 404: //Not found
267 case 500: //Server Error
269 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Server returned code %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
277 http_hdr
= (HTTP_header_t
*)stream
->streaming_ctrl
->data
;
278 if( http_hdr
->body_size
>0 ) {
279 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
280 http_free( http_hdr
);
281 stream
->streaming_ctrl
->data
= NULL
;
288 http_free( http_hdr
);
289 stream
->streaming_ctrl
->data
= NULL
;
292 stream
->streaming_ctrl
->streaming_read
= nop_streaming_read
;
293 stream
->streaming_ctrl
->streaming_seek
= nop_streaming_seek
;
294 stream
->streaming_ctrl
->prebuffer_size
= 64*1024; // 64 KBytes
295 stream
->streaming_ctrl
->buffering
= 1;
296 stream
->streaming_ctrl
->status
= streaming_playing_e
;
301 http_new_header(void) {
302 HTTP_header_t
*http_hdr
;
304 http_hdr
= calloc(1, sizeof(*http_hdr
));
305 if( http_hdr
==NULL
) return NULL
;
311 http_free( HTTP_header_t
*http_hdr
) {
312 HTTP_field_t
*field
, *field2free
;
313 if( http_hdr
==NULL
) return;
314 free(http_hdr
->protocol
);
316 free(http_hdr
->reason_phrase
);
317 free(http_hdr
->field_search
);
318 free(http_hdr
->method
);
319 free(http_hdr
->buffer
);
320 field
= http_hdr
->first_field
;
321 while( field
!=NULL
) {
323 free(field
->field_name
);
332 http_response_append( HTTP_header_t
*http_hdr
, char *response
, int length
) {
333 if( http_hdr
==NULL
|| response
==NULL
|| length
<0 ) return -1;
335 if( (unsigned)length
> SIZE_MAX
- http_hdr
->buffer_size
- 1) {
336 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Bad size in memory (re)allocation\n");
339 http_hdr
->buffer
= realloc( http_hdr
->buffer
, http_hdr
->buffer_size
+length
+1 );
340 if( http_hdr
->buffer
==NULL
) {
341 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory (re)allocation failed\n");
344 memcpy( http_hdr
->buffer
+http_hdr
->buffer_size
, response
, length
);
345 http_hdr
->buffer_size
+= length
;
346 http_hdr
->buffer
[http_hdr
->buffer_size
]=0; // close the string!
347 return http_hdr
->buffer_size
;
351 http_is_header_entire( HTTP_header_t
*http_hdr
) {
352 if( http_hdr
==NULL
) return -1;
353 if( http_hdr
->buffer
==NULL
) return 0; // empty
355 if( strstr(http_hdr
->buffer
, "\r\n\r\n")==NULL
&&
356 strstr(http_hdr
->buffer
, "\n\n")==NULL
) return 0;
361 http_response_parse( HTTP_header_t
*http_hdr
) {
364 int pos_hdr_sep
, hdr_sep_len
;
366 if( http_hdr
==NULL
) return -1;
367 if( http_hdr
->is_parsed
) return 0;
370 hdr_ptr
= strstr( http_hdr
->buffer
, " " );
371 if( hdr_ptr
==NULL
) {
372 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. No space separator found.\n");
375 len
= hdr_ptr
-http_hdr
->buffer
;
376 http_hdr
->protocol
= malloc(len
+1);
377 if( http_hdr
->protocol
==NULL
) {
378 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
381 strncpy( http_hdr
->protocol
, http_hdr
->buffer
, len
);
382 http_hdr
->protocol
[len
]='\0';
383 if( !strncasecmp( http_hdr
->protocol
, "HTTP", 4) ) {
384 if( sscanf( http_hdr
->protocol
+5,"1.%d", &(http_hdr
->http_minor_version
) )!=1 ) {
385 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get HTTP minor version.\n");
390 // Get the status code
391 if( sscanf( ++hdr_ptr
, "%d", &(http_hdr
->status_code
) )!=1 ) {
392 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get status code.\n");
397 // Get the reason phrase
398 ptr
= strstr( hdr_ptr
, "\n" );
399 if( hdr_ptr
==NULL
) {
400 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get the reason phrase.\n");
404 http_hdr
->reason_phrase
= malloc(len
+1);
405 if( http_hdr
->reason_phrase
==NULL
) {
406 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
409 strncpy( http_hdr
->reason_phrase
, hdr_ptr
, len
);
410 if( http_hdr
->reason_phrase
[len
-1]=='\r' ) {
413 http_hdr
->reason_phrase
[len
]='\0';
415 // Set the position of the header separator: \r\n\r\n
417 ptr
= strstr( http_hdr
->buffer
, "\r\n\r\n" );
419 ptr
= strstr( http_hdr
->buffer
, "\n\n" );
421 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Header may be incomplete. No CRLF CRLF found.\n");
426 pos_hdr_sep
= ptr
-http_hdr
->buffer
;
428 // Point to the first line after the method line.
429 hdr_ptr
= strstr( http_hdr
->buffer
, "\n" )+1;
432 while( *ptr
!='\r' && *ptr
!='\n' ) ptr
++;
435 field
= realloc(field
, len
+1);
437 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
440 strncpy( field
, hdr_ptr
, len
);
442 http_set_field( http_hdr
, field
);
443 hdr_ptr
= ptr
+((*ptr
=='\r')?2:1);
444 } while( hdr_ptr
<(http_hdr
->buffer
+pos_hdr_sep
) );
448 if( pos_hdr_sep
+hdr_sep_len
<http_hdr
->buffer_size
) {
449 // Response has data!
450 http_hdr
->body
= http_hdr
->buffer
+pos_hdr_sep
+hdr_sep_len
;
451 http_hdr
->body_size
= http_hdr
->buffer_size
-(pos_hdr_sep
+hdr_sep_len
);
454 http_hdr
->is_parsed
= 1;
459 http_build_request( HTTP_header_t
*http_hdr
) {
460 char *ptr
, *uri
=NULL
;
463 if( http_hdr
==NULL
) return NULL
;
465 if( http_hdr
->method
==NULL
) http_set_method( http_hdr
, "GET");
466 if( http_hdr
->uri
==NULL
) http_set_uri( http_hdr
, "/");
468 uri
= malloc(strlen(http_hdr
->uri
) + 1);
470 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
473 strcpy(uri
,http_hdr
->uri
);
476 //**** Compute the request length
477 // Add the Method line
478 len
= strlen(http_hdr
->method
)+strlen(uri
)+12;
480 field
= http_hdr
->first_field
;
481 while( field
!=NULL
) {
482 len
+= strlen(field
->field_name
)+2;
488 if( http_hdr
->body
!=NULL
) {
489 len
+= http_hdr
->body_size
;
491 // Free the buffer if it was previously used
492 if( http_hdr
->buffer
!=NULL
) {
493 free( http_hdr
->buffer
);
494 http_hdr
->buffer
= NULL
;
496 http_hdr
->buffer
= malloc(len
+1);
497 if( http_hdr
->buffer
==NULL
) {
498 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
501 http_hdr
->buffer_size
= len
;
503 //*** Building the request
504 ptr
= http_hdr
->buffer
;
505 // Add the method line
506 ptr
+= sprintf( ptr
, "%s %s HTTP/1.%d\r\n", http_hdr
->method
, uri
, http_hdr
->http_minor_version
);
507 field
= http_hdr
->first_field
;
509 while( field
!=NULL
) {
510 ptr
+= sprintf( ptr
, "%s\r\n", field
->field_name
);
513 ptr
+= sprintf( ptr
, "\r\n" );
515 if( http_hdr
->body
!=NULL
) {
516 memcpy( ptr
, http_hdr
->body
, http_hdr
->body_size
);
520 return http_hdr
->buffer
;
524 http_get_field( HTTP_header_t
*http_hdr
, const char *field_name
) {
525 if( http_hdr
==NULL
|| field_name
==NULL
) return NULL
;
526 http_hdr
->field_search_pos
= http_hdr
->first_field
;
527 http_hdr
->field_search
= realloc( http_hdr
->field_search
, strlen(field_name
)+1 );
528 if( http_hdr
->field_search
==NULL
) {
529 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
532 strcpy( http_hdr
->field_search
, field_name
);
533 return http_get_next_field( http_hdr
);
537 http_get_next_field( HTTP_header_t
*http_hdr
) {
540 if( http_hdr
==NULL
) return NULL
;
542 field
= http_hdr
->field_search_pos
;
543 while( field
!=NULL
) {
544 ptr
= strstr( field
->field_name
, ":" );
545 if( ptr
==NULL
) return NULL
;
546 if( !strncasecmp( field
->field_name
, http_hdr
->field_search
, ptr
-(field
->field_name
) ) ) {
547 ptr
++; // Skip the column
548 while( ptr
[0]==' ' ) ptr
++; // Skip the spaces if there is some
549 http_hdr
->field_search_pos
= field
->next
;
550 return ptr
; // return the value without the field name
558 http_set_field( HTTP_header_t
*http_hdr
, const char *field_name
) {
559 HTTP_field_t
*new_field
;
560 if( http_hdr
==NULL
|| field_name
==NULL
) return;
562 new_field
= malloc(sizeof(HTTP_field_t
));
563 if( new_field
==NULL
) {
564 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
567 new_field
->next
= NULL
;
568 new_field
->field_name
= malloc(strlen(field_name
)+1);
569 if( new_field
->field_name
==NULL
) {
570 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
574 strcpy( new_field
->field_name
, field_name
);
576 if( http_hdr
->last_field
==NULL
) {
577 http_hdr
->first_field
= new_field
;
579 http_hdr
->last_field
->next
= new_field
;
581 http_hdr
->last_field
= new_field
;
582 http_hdr
->field_nb
++;
586 http_set_method( HTTP_header_t
*http_hdr
, const char *method
) {
587 if( http_hdr
==NULL
|| method
==NULL
) return;
589 http_hdr
->method
= malloc(strlen(method
)+1);
590 if( http_hdr
->method
==NULL
) {
591 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
594 strcpy( http_hdr
->method
, method
);
598 http_set_uri( HTTP_header_t
*http_hdr
, const char *uri
) {
599 if( http_hdr
==NULL
|| uri
==NULL
) return;
601 http_hdr
->uri
= malloc(strlen(uri
)+1);
602 if( http_hdr
->uri
==NULL
) {
603 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
606 strcpy( http_hdr
->uri
, uri
);
610 http_add_authentication( HTTP_header_t
*http_hdr
, const char *username
, const char *password
, const char *auth_str
) {
611 char *auth
= NULL
, *usr_pass
= NULL
, *b64_usr_pass
= NULL
;
612 int encoded_len
, pass_len
=0;
613 size_t auth_len
, usr_pass_len
;
615 if( http_hdr
==NULL
|| username
==NULL
) return -1;
617 if( password
!=NULL
) {
618 pass_len
= strlen(password
);
621 usr_pass_len
= strlen(username
) + 1 + pass_len
;
622 usr_pass
= malloc(usr_pass_len
+ 1);
623 if( usr_pass
==NULL
) {
624 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
628 sprintf( usr_pass
, "%s:%s", username
, (password
==NULL
)?"":password
);
630 encoded_len
= AV_BASE64_SIZE(usr_pass_len
);
631 b64_usr_pass
= malloc(encoded_len
);
632 if( b64_usr_pass
==NULL
) {
633 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
636 av_base64_encode(b64_usr_pass
, encoded_len
, usr_pass
, usr_pass_len
);
638 auth_len
= encoded_len
+ 100;
639 auth
= malloc(auth_len
);
641 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
645 snprintf(auth
, auth_len
, "%s: Basic %s", auth_str
, b64_usr_pass
);
646 http_set_field( http_hdr
, auth
);
651 free( b64_usr_pass
);
658 http_add_basic_authentication( HTTP_header_t
*http_hdr
, const char *username
, const char *password
) {
659 return http_add_authentication(http_hdr
, username
, password
, "Authorization");
663 http_add_basic_proxy_authentication( HTTP_header_t
*http_hdr
, const char *username
, const char *password
) {
664 return http_add_authentication(http_hdr
, username
, password
, "Proxy-Authorization");
668 http_debug_hdr( HTTP_header_t
*http_hdr
) {
671 if( http_hdr
==NULL
) return;
673 mp_msg(MSGT_NETWORK
,MSGL_V
,"--- HTTP DEBUG HEADER --- START ---\n");
674 mp_msg(MSGT_NETWORK
,MSGL_V
,"protocol: [%s]\n", http_hdr
->protocol
);
675 mp_msg(MSGT_NETWORK
,MSGL_V
,"http minor version: [%d]\n", http_hdr
->http_minor_version
);
676 mp_msg(MSGT_NETWORK
,MSGL_V
,"uri: [%s]\n", http_hdr
->uri
);
677 mp_msg(MSGT_NETWORK
,MSGL_V
,"method: [%s]\n", http_hdr
->method
);
678 mp_msg(MSGT_NETWORK
,MSGL_V
,"status code: [%d]\n", http_hdr
->status_code
);
679 mp_msg(MSGT_NETWORK
,MSGL_V
,"reason phrase: [%s]\n", http_hdr
->reason_phrase
);
680 mp_msg(MSGT_NETWORK
,MSGL_V
,"body size: [%zd]\n", http_hdr
->body_size
);
682 mp_msg(MSGT_NETWORK
,MSGL_V
,"Fields:\n");
683 field
= http_hdr
->first_field
;
684 while( field
!=NULL
) {
685 mp_msg(MSGT_NETWORK
,MSGL_V
," %d - %s\n", i
++, field
->field_name
);
688 mp_msg(MSGT_NETWORK
,MSGL_V
,"--- HTTP DEBUG HEADER --- END ---\n");
691 static void print_icy_metadata(HTTP_header_t
*http_hdr
) {
692 const char *field_data
;
693 // note: I skip icy-notice1 and 2, as they contain html <BR>
694 // and are IMHO useless info ::atmos
695 if( (field_data
= http_get_field(http_hdr
, "icy-name")) != NULL
)
696 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Name : %s\n", field_data
);
697 if( (field_data
= http_get_field(http_hdr
, "icy-genre")) != NULL
)
698 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Genre : %s\n", field_data
);
699 if( (field_data
= http_get_field(http_hdr
, "icy-url")) != NULL
)
700 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Website: %s\n", field_data
);
701 // XXX: does this really mean public server? ::atmos
702 if( (field_data
= http_get_field(http_hdr
, "icy-pub")) != NULL
)
703 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Public : %s\n", atoi(field_data
)?"yes":"no");
704 if( (field_data
= http_get_field(http_hdr
, "icy-br")) != NULL
)
705 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Bitrate: %skbit/s\n", field_data
);
708 //! If this function succeeds you must closesocket stream->fd
709 static int http_streaming_start(stream_t
*stream
, int* file_format
) {
710 HTTP_header_t
*http_hdr
= NULL
;
712 int res
= STREAM_UNSUPPORTED
;
717 const char *content_length
;
719 URL_t
*url
= stream
->streaming_ctrl
->url
;
724 if (fd
> 0) closesocket(fd
);
725 fd
= http_send_request( url
, 0 );
731 http_hdr
= http_read_response( fd
);
732 if( http_hdr
==NULL
) {
736 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) ) {
737 http_debug_hdr( http_hdr
);
740 // Check if we can make partial content requests and thus seek in http-streams
741 if( http_hdr
!=NULL
&& http_hdr
->status_code
==200 ) {
742 const char *accept_ranges
= http_get_field(http_hdr
,"Accept-Ranges");
743 const char *server
= http_get_field(http_hdr
, "Server");
745 seekable
= strncmp(accept_ranges
,"bytes",5)==0;
746 else if (server
&& (strcmp(server
, "gvs 1.0") == 0 ||
747 strncmp(server
, "MakeMKV", 7) == 0)) {
748 // HACK for youtube and MakeMKV incorrectly claiming not to support seeking
749 mp_msg(MSGT_NETWORK
, MSGL_WARN
, "Broken webserver, incorrectly claims to not support Accept-Ranges\n");
754 print_icy_metadata(http_hdr
);
756 // Check if the response is an ICY status_code reason_phrase
757 if( !strcasecmp(http_hdr
->protocol
, "ICY") ||
758 http_get_field(http_hdr
, "Icy-MetaInt") ) {
759 switch( http_hdr
->status_code
) {
762 // If content-type == video/nsv we most likely have a winamp video stream
763 // otherwise it should be mp3. if there are more types consider adding mime type
764 // handling like later
765 if ( (field_data
= http_get_field(http_hdr
, "content-type")) != NULL
&& (!strcmp(field_data
, "video/nsv") || !strcmp(field_data
, "misc/ultravox")))
766 *file_format
= DEMUXER_TYPE_NSV
;
767 else if ( (field_data
= http_get_field(http_hdr
, "content-type")) != NULL
&& (!strcmp(field_data
, "audio/aacp") || !strcmp(field_data
, "audio/aac")))
768 *file_format
= DEMUXER_TYPE_AAC
;
770 *file_format
= DEMUXER_TYPE_AUDIO
;
774 case 400: // Server Full
775 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server is full, skipping!\n");
777 case 401: // Service Unavailable
778 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server return service unavailable, skipping!\n");
780 case 403: // Service Forbidden
781 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server return 'Service Forbidden'\n");
783 case 404: // Resource Not Found
784 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server couldn't find requested stream, skipping!\n");
787 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
792 // Assume standard http if not ICY
793 switch( http_hdr
->status_code
) {
795 content_length
= http_get_field(http_hdr
, "Content-Length");
796 if (content_length
) {
797 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", content_length
);
798 stream
->end_pos
= atoll(content_length
);
800 // Look if we can use the Content-Type
801 content_type
= http_get_field( http_hdr
, "Content-Type" );
802 if( content_type
!=NULL
) {
805 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", content_type
);
806 // Check in the mime type table for a demuxer type
807 for (i
= 0; mime_type_table
[i
].mime_type
!= NULL
; i
++) {
808 if( !strcasecmp( content_type
, mime_type_table
[i
].mime_type
) ) {
809 *file_format
= mime_type_table
[i
].demuxer_type
;
815 // Not found in the mime type table, don't fail,
816 // we should try raw HTTP
820 case 301: // Permanently
821 case 302: // Temporarily
822 case 303: // See Other
823 case 307: // Temporarily (since HTTP/1.1)
824 // TODO: RFC 2616, recommand to detect infinite redirection loops
825 next_url
= http_get_field( http_hdr
, "Location" );
826 if( next_url
!=NULL
) {
827 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
828 stream
->streaming_ctrl
->url
= url_redirect( &url
, next_url
);
829 if (!strcasecmp(url
->protocol
, "mms")) {
830 res
= STREAM_REDIRECTED
;
833 if (strcasecmp(url
->protocol
, "http")) {
834 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Unsupported http %d redirect to %s protocol\n", http_hdr
->status_code
, url
->protocol
);
839 url
->protocol
= strdup("unsv");
844 case 401: // Authentication required
845 if( http_authenticate(http_hdr
, url
, &auth_retry
)<0 )
850 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Server returned %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
856 if (fd
> 0) closesocket( fd
);
858 http_free( http_hdr
);
861 stream
->streaming_ctrl
->data
= (void*)http_hdr
;
866 static int fixup_open(stream_t
*stream
,int seekable
) {
867 HTTP_header_t
*http_hdr
= stream
->streaming_ctrl
->data
;
868 int is_icy
= http_hdr
&& http_get_field(http_hdr
, "Icy-MetaInt");
869 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
871 stream
->type
= STREAMTYPE_STREAM
;
872 if(!is_icy
&& !is_ultravox
&& seekable
)
874 stream
->flags
|= MP_STREAM_SEEK
;
875 stream
->seek
= http_seek
;
877 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
878 if ((!is_icy
&& !is_ultravox
) || scast_streaming_start(stream
))
879 if(nop_streaming_start( stream
)) {
880 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_start failed\n");
882 closesocket(stream
->fd
);
884 streaming_ctrl_free(stream
->streaming_ctrl
);
885 stream
->streaming_ctrl
= NULL
;
886 return STREAM_UNSUPPORTED
;
889 fixup_network_stream_cache(stream
);
893 static int open_s1(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
897 stream
->streaming_ctrl
= streaming_ctrl_new();
898 if( stream
->streaming_ctrl
==NULL
) {
901 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
902 url
= url_new(stream
->url
);
903 stream
->streaming_ctrl
->url
= check4proxies(url
);
906 mp_msg(MSGT_OPEN
, MSGL_V
, "STREAM_HTTP(1), URL: %s\n", stream
->url
);
907 seekable
= http_streaming_start(stream
, file_format
);
908 if((seekable
< 0) || (*file_format
== DEMUXER_TYPE_ASF
)) {
910 closesocket(stream
->fd
);
912 if (seekable
== STREAM_REDIRECTED
)
914 streaming_ctrl_free(stream
->streaming_ctrl
);
915 stream
->streaming_ctrl
= NULL
;
916 return STREAM_UNSUPPORTED
;
919 return fixup_open(stream
, seekable
);
922 static int open_s2(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
926 stream
->streaming_ctrl
= streaming_ctrl_new();
927 if( stream
->streaming_ctrl
==NULL
) {
930 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
931 url
= url_new(stream
->url
);
932 stream
->streaming_ctrl
->url
= check4proxies(url
);
935 mp_msg(MSGT_OPEN
, MSGL_V
, "STREAM_HTTP(2), URL: %s\n", stream
->url
);
936 seekable
= http_streaming_start(stream
, file_format
);
939 closesocket(stream
->fd
);
941 streaming_ctrl_free(stream
->streaming_ctrl
);
942 stream
->streaming_ctrl
= NULL
;
943 return STREAM_UNSUPPORTED
;
946 return fixup_open(stream
, seekable
);
950 const stream_info_t stream_info_http1
= {
953 "Bertrand, Albeau, Reimar Doeffinger, Arpi?",
956 {"http", "http_proxy", "unsv", "icyx", "noicyx", NULL
},
958 0 // Urls are an option string
961 const stream_info_t stream_info_http2
= {
964 "Bertrand, Albeu, Arpi? who?",
965 "plain http, also used as fallback for many other protocols",
967 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL
}, //all the others as fallback
969 0 // Urls are an option string