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>
47 #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(50, 17, 0)
48 #define AV_BASE64_SIZE(x) (((x)+2) / 3 * 4 + 1)
51 extern int stream_cache_size
;
52 extern int network_bandwidth
;
61 * \brief first read any data from sc->buffer then from fd
62 * \param fd file descriptor to read data from
63 * \param buffer buffer to read into
64 * \param len how many bytes to read
65 * \param sc streaming control containing buffer to read from first
66 * \return len unless there is a read error or eof
68 static unsigned my_read(int fd
, char *buffer
, int len
, streaming_ctrl_t
*sc
) {
70 unsigned cp_len
= sc
->buffer_size
- sc
->buffer_pos
;
73 memcpy(buffer
, &sc
->buffer
[sc
->buffer_pos
], cp_len
);
74 sc
->buffer_pos
+= cp_len
;
77 int ret
= recv(fd
, &buffer
[pos
], len
- pos
, 0);
86 * \brief read and process (i.e. discard *g*) a block of ultravox metadata
87 * \param fd file descriptor to read from
88 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
89 * \return number of real data before next metadata block starts or 0 on error
91 static unsigned uvox_meta_read(int fd
, streaming_ctrl_t
*sc
) {
93 unsigned char info
[6] = {0, 0, 0, 0, 0, 0};
96 info_read
= my_read(fd
, info
, 1, sc
);
98 info_read
= my_read(fd
, info
, 6, sc
);
100 info_read
+= my_read(fd
, &info
[1], 5, sc
);
101 if (info_read
!= 6) // read error or eof
103 // sync byte and reserved flags
104 if (info
[0] != 0x5a || (info
[1] & 0xfc) != 0x00) {
105 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "Invalid or unknown uvox metadata\n");
109 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Encrypted ultravox data\n");
110 metaint
= info
[4] << 8 | info
[5];
111 if ((info
[3] & 0xf) < 0x07) { // discard any metadata nonsense
112 char *metabuf
= malloc(metaint
);
113 my_read(fd
, metabuf
, metaint
, sc
);
116 } while ((info
[3] & 0xf) < 0x07);
121 * \brief read one scast meta data entry and print it
122 * \param fd file descriptor to read from
123 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
125 static void scast_meta_read(int fd
, streaming_ctrl_t
*sc
) {
126 unsigned char tmp
= 0;
128 my_read(fd
, &tmp
, 1, sc
);
132 uint8_t *info
= malloc(metalen
+ 1);
133 unsigned nlen
= my_read(fd
, info
, metalen
, sc
);
134 // avoid breaking the user's terminal too much
135 if (nlen
> 256) nlen
= 256;
136 for (i
= 0; i
< nlen
; i
++)
137 if (info
[i
] && info
[i
] < 32) info
[i
] = '?';
139 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "\nICY Info: %s\n", info
);
145 * \brief read data from scast/ultravox stream without any metadata
146 * \param fd file descriptor to read from
147 * \param buffer buffer to read data into
148 * \param size number of bytes to read
149 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
151 static int scast_streaming_read(int fd
, char *buffer
, int size
,
152 streaming_ctrl_t
*sc
) {
153 scast_data_t
*sd
= (scast_data_t
*)sc
->data
;
157 // first read remaining data up to next metadata
158 block
= sd
->metaint
- sd
->metapos
;
161 ret
= my_read(fd
, buffer
, block
, sc
);
164 if (ret
!= block
) // read problems or eof
167 while (done
< size
) { // now comes the metadata
170 sd
->metaint
= uvox_meta_read(fd
, sc
);
175 scast_meta_read(fd
, sc
); // read and display metadata
178 if (block
> sd
->metaint
)
180 ret
= my_read(fd
, &buffer
[done
], block
, sc
);
183 if (ret
!= block
) // read problems or eof
189 static int scast_streaming_start(stream_t
*stream
) {
191 scast_data_t
*scast_data
;
192 HTTP_header_t
*http_hdr
= stream
->streaming_ctrl
->data
;
193 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
194 if (!stream
|| stream
->fd
< 0 || !http_hdr
)
199 metaint
= atoi(http_get_field(http_hdr
, "Icy-MetaInt"));
203 stream
->streaming_ctrl
->buffer
= malloc(http_hdr
->body_size
);
204 stream
->streaming_ctrl
->buffer_size
= http_hdr
->body_size
;
205 stream
->streaming_ctrl
->buffer_pos
= 0;
206 memcpy(stream
->streaming_ctrl
->buffer
, http_hdr
->body
, http_hdr
->body_size
);
207 scast_data
= malloc(sizeof(scast_data_t
));
208 scast_data
->metaint
= metaint
;
209 scast_data
->metapos
= 0;
210 scast_data
->is_ultravox
= is_ultravox
;
212 stream
->streaming_ctrl
->data
= scast_data
;
213 stream
->streaming_ctrl
->streaming_read
= scast_streaming_read
;
214 stream
->streaming_ctrl
->streaming_seek
= NULL
;
215 stream
->streaming_ctrl
->prebuffer_size
= 64 * 1024; // 64 KBytes
216 stream
->streaming_ctrl
->buffering
= 1;
217 stream
->streaming_ctrl
->status
= streaming_playing_e
;
221 static int nop_streaming_start( stream_t
*stream
) {
222 HTTP_header_t
*http_hdr
= NULL
;
226 if( stream
==NULL
) return -1;
230 fd
= http_send_request( stream
->streaming_ctrl
->url
, 0 );
231 if( fd
<0 ) return -1;
232 http_hdr
= http_read_response( fd
);
233 if( http_hdr
==NULL
) return -1;
235 switch( http_hdr
->status_code
) {
237 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", http_get_field(http_hdr
, "Content-Type") );
238 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", http_get_field(http_hdr
, "Content-Length") );
239 if( http_hdr
->body_size
>0 ) {
240 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
241 http_free( http_hdr
);
247 case 301: // Permanently
248 case 302: // Temporarily
249 case 303: // See Other
250 case 307: // Temporarily (since HTTP/1.1)
252 next_url
= http_get_field( http_hdr
, "Location" );
254 if (next_url
!= NULL
)
255 rd_url
=url_new(next_url
);
257 if (next_url
!= NULL
&& rd_url
!= NULL
) {
258 mp_msg(MSGT_NETWORK
,MSGL_STATUS
,"Redirected: Using this url instead %s\n",next_url
);
259 stream
->streaming_ctrl
->url
=check4proxies(rd_url
);
260 ret
=nop_streaming_start(stream
); //recursively get streaming started
262 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Redirection failed\n");
268 case 401: //Authorization required
269 case 403: //Forbidden
270 case 404: //Not found
271 case 500: //Server Error
273 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Server returned code %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
281 http_hdr
= (HTTP_header_t
*)stream
->streaming_ctrl
->data
;
282 if( http_hdr
->body_size
>0 ) {
283 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
284 http_free( http_hdr
);
285 stream
->streaming_ctrl
->data
= NULL
;
292 http_free( http_hdr
);
293 stream
->streaming_ctrl
->data
= NULL
;
296 stream
->streaming_ctrl
->streaming_read
= nop_streaming_read
;
297 stream
->streaming_ctrl
->streaming_seek
= nop_streaming_seek
;
298 stream
->streaming_ctrl
->prebuffer_size
= 64*1024; // 64 KBytes
299 stream
->streaming_ctrl
->buffering
= 1;
300 stream
->streaming_ctrl
->status
= streaming_playing_e
;
305 http_new_header(void) {
306 HTTP_header_t
*http_hdr
;
308 http_hdr
= calloc(1, sizeof(*http_hdr
));
309 if( http_hdr
==NULL
) return NULL
;
315 http_free( HTTP_header_t
*http_hdr
) {
316 HTTP_field_t
*field
, *field2free
;
317 if( http_hdr
==NULL
) return;
318 free(http_hdr
->protocol
);
320 free(http_hdr
->reason_phrase
);
321 free(http_hdr
->field_search
);
322 free(http_hdr
->method
);
323 free(http_hdr
->buffer
);
324 field
= http_hdr
->first_field
;
325 while( field
!=NULL
) {
327 free(field
->field_name
);
336 http_response_append( HTTP_header_t
*http_hdr
, char *response
, int length
) {
337 if( http_hdr
==NULL
|| response
==NULL
|| length
<0 ) return -1;
339 if( (unsigned)length
> SIZE_MAX
- http_hdr
->buffer_size
- 1) {
340 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Bad size in memory (re)allocation\n");
343 http_hdr
->buffer
= realloc( http_hdr
->buffer
, http_hdr
->buffer_size
+length
+1 );
344 if( http_hdr
->buffer
==NULL
) {
345 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory (re)allocation failed\n");
348 memcpy( http_hdr
->buffer
+http_hdr
->buffer_size
, response
, length
);
349 http_hdr
->buffer_size
+= length
;
350 http_hdr
->buffer
[http_hdr
->buffer_size
]=0; // close the string!
351 return http_hdr
->buffer_size
;
355 http_is_header_entire( HTTP_header_t
*http_hdr
) {
356 if( http_hdr
==NULL
) return -1;
357 if( http_hdr
->buffer
==NULL
) return 0; // empty
359 if( strstr(http_hdr
->buffer
, "\r\n\r\n")==NULL
&&
360 strstr(http_hdr
->buffer
, "\n\n")==NULL
) return 0;
365 http_response_parse( HTTP_header_t
*http_hdr
) {
368 int pos_hdr_sep
, hdr_sep_len
;
370 if( http_hdr
==NULL
) return -1;
371 if( http_hdr
->is_parsed
) return 0;
374 hdr_ptr
= strstr( http_hdr
->buffer
, " " );
375 if( hdr_ptr
==NULL
) {
376 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. No space separator found.\n");
379 len
= hdr_ptr
-http_hdr
->buffer
;
380 http_hdr
->protocol
= malloc(len
+1);
381 if( http_hdr
->protocol
==NULL
) {
382 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
385 strncpy( http_hdr
->protocol
, http_hdr
->buffer
, len
);
386 http_hdr
->protocol
[len
]='\0';
387 if( !strncasecmp( http_hdr
->protocol
, "HTTP", 4) ) {
388 if( sscanf( http_hdr
->protocol
+5,"1.%d", &(http_hdr
->http_minor_version
) )!=1 ) {
389 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get HTTP minor version.\n");
394 // Get the status code
395 if( sscanf( ++hdr_ptr
, "%d", &(http_hdr
->status_code
) )!=1 ) {
396 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get status code.\n");
401 // Get the reason phrase
402 ptr
= strstr( hdr_ptr
, "\n" );
403 if( hdr_ptr
==NULL
) {
404 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get the reason phrase.\n");
408 http_hdr
->reason_phrase
= malloc(len
+1);
409 if( http_hdr
->reason_phrase
==NULL
) {
410 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
413 strncpy( http_hdr
->reason_phrase
, hdr_ptr
, len
);
414 if( http_hdr
->reason_phrase
[len
-1]=='\r' ) {
417 http_hdr
->reason_phrase
[len
]='\0';
419 // Set the position of the header separator: \r\n\r\n
421 ptr
= strstr( http_hdr
->buffer
, "\r\n\r\n" );
423 ptr
= strstr( http_hdr
->buffer
, "\n\n" );
425 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Header may be incomplete. No CRLF CRLF found.\n");
430 pos_hdr_sep
= ptr
-http_hdr
->buffer
;
432 // Point to the first line after the method line.
433 hdr_ptr
= strstr( http_hdr
->buffer
, "\n" )+1;
436 while( *ptr
!='\r' && *ptr
!='\n' ) ptr
++;
439 field
= realloc(field
, len
+1);
441 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
444 strncpy( field
, hdr_ptr
, len
);
446 http_set_field( http_hdr
, field
);
447 hdr_ptr
= ptr
+((*ptr
=='\r')?2:1);
448 } while( hdr_ptr
<(http_hdr
->buffer
+pos_hdr_sep
) );
452 if( pos_hdr_sep
+hdr_sep_len
<http_hdr
->buffer_size
) {
453 // Response has data!
454 http_hdr
->body
= http_hdr
->buffer
+pos_hdr_sep
+hdr_sep_len
;
455 http_hdr
->body_size
= http_hdr
->buffer_size
-(pos_hdr_sep
+hdr_sep_len
);
458 http_hdr
->is_parsed
= 1;
463 http_build_request( HTTP_header_t
*http_hdr
) {
464 char *ptr
, *uri
=NULL
;
467 if( http_hdr
==NULL
) return NULL
;
469 if( http_hdr
->method
==NULL
) http_set_method( http_hdr
, "GET");
470 if( http_hdr
->uri
==NULL
) http_set_uri( http_hdr
, "/");
472 uri
= malloc(strlen(http_hdr
->uri
) + 1);
474 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
477 strcpy(uri
,http_hdr
->uri
);
480 //**** Compute the request length
481 // Add the Method line
482 len
= strlen(http_hdr
->method
)+strlen(uri
)+12;
484 field
= http_hdr
->first_field
;
485 while( field
!=NULL
) {
486 len
+= strlen(field
->field_name
)+2;
492 if( http_hdr
->body
!=NULL
) {
493 len
+= http_hdr
->body_size
;
495 // Free the buffer if it was previously used
496 if( http_hdr
->buffer
!=NULL
) {
497 free( http_hdr
->buffer
);
498 http_hdr
->buffer
= NULL
;
500 http_hdr
->buffer
= malloc(len
+1);
501 if( http_hdr
->buffer
==NULL
) {
502 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
505 http_hdr
->buffer_size
= len
;
507 //*** Building the request
508 ptr
= http_hdr
->buffer
;
509 // Add the method line
510 ptr
+= sprintf( ptr
, "%s %s HTTP/1.%d\r\n", http_hdr
->method
, uri
, http_hdr
->http_minor_version
);
511 field
= http_hdr
->first_field
;
513 while( field
!=NULL
) {
514 ptr
+= sprintf( ptr
, "%s\r\n", field
->field_name
);
517 ptr
+= sprintf( ptr
, "\r\n" );
519 if( http_hdr
->body
!=NULL
) {
520 memcpy( ptr
, http_hdr
->body
, http_hdr
->body_size
);
524 return http_hdr
->buffer
;
528 http_get_field( HTTP_header_t
*http_hdr
, const char *field_name
) {
529 if( http_hdr
==NULL
|| field_name
==NULL
) return NULL
;
530 http_hdr
->field_search_pos
= http_hdr
->first_field
;
531 http_hdr
->field_search
= realloc( http_hdr
->field_search
, strlen(field_name
)+1 );
532 if( http_hdr
->field_search
==NULL
) {
533 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
536 strcpy( http_hdr
->field_search
, field_name
);
537 return http_get_next_field( http_hdr
);
541 http_get_next_field( HTTP_header_t
*http_hdr
) {
544 if( http_hdr
==NULL
) return NULL
;
546 field
= http_hdr
->field_search_pos
;
547 while( field
!=NULL
) {
548 ptr
= strstr( field
->field_name
, ":" );
549 if( ptr
==NULL
) return NULL
;
550 if( !strncasecmp( field
->field_name
, http_hdr
->field_search
, ptr
-(field
->field_name
) ) ) {
551 ptr
++; // Skip the column
552 while( ptr
[0]==' ' ) ptr
++; // Skip the spaces if there is some
553 http_hdr
->field_search_pos
= field
->next
;
554 return ptr
; // return the value without the field name
562 http_set_field( HTTP_header_t
*http_hdr
, const char *field_name
) {
563 HTTP_field_t
*new_field
;
564 if( http_hdr
==NULL
|| field_name
==NULL
) return;
566 new_field
= malloc(sizeof(HTTP_field_t
));
567 if( new_field
==NULL
) {
568 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
571 new_field
->next
= NULL
;
572 new_field
->field_name
= malloc(strlen(field_name
)+1);
573 if( new_field
->field_name
==NULL
) {
574 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
578 strcpy( new_field
->field_name
, field_name
);
580 if( http_hdr
->last_field
==NULL
) {
581 http_hdr
->first_field
= new_field
;
583 http_hdr
->last_field
->next
= new_field
;
585 http_hdr
->last_field
= new_field
;
586 http_hdr
->field_nb
++;
590 http_set_method( HTTP_header_t
*http_hdr
, const char *method
) {
591 if( http_hdr
==NULL
|| method
==NULL
) return;
593 http_hdr
->method
= malloc(strlen(method
)+1);
594 if( http_hdr
->method
==NULL
) {
595 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
598 strcpy( http_hdr
->method
, method
);
602 http_set_uri( HTTP_header_t
*http_hdr
, const char *uri
) {
603 if( http_hdr
==NULL
|| uri
==NULL
) return;
605 http_hdr
->uri
= malloc(strlen(uri
)+1);
606 if( http_hdr
->uri
==NULL
) {
607 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
610 strcpy( http_hdr
->uri
, uri
);
614 http_add_authentication( HTTP_header_t
*http_hdr
, const char *username
, const char *password
, const char *auth_str
) {
615 char *auth
= NULL
, *usr_pass
= NULL
, *b64_usr_pass
= NULL
;
616 int encoded_len
, pass_len
=0;
617 size_t auth_len
, usr_pass_len
;
619 if( http_hdr
==NULL
|| username
==NULL
) return -1;
621 if( password
!=NULL
) {
622 pass_len
= strlen(password
);
625 usr_pass_len
= strlen(username
) + 1 + pass_len
;
626 usr_pass
= malloc(usr_pass_len
+ 1);
627 if( usr_pass
==NULL
) {
628 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
632 sprintf( usr_pass
, "%s:%s", username
, (password
==NULL
)?"":password
);
634 encoded_len
= AV_BASE64_SIZE(usr_pass_len
);
635 b64_usr_pass
= malloc(encoded_len
);
636 if( b64_usr_pass
==NULL
) {
637 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
640 av_base64_encode(b64_usr_pass
, encoded_len
, usr_pass
, usr_pass_len
);
642 auth_len
= encoded_len
+ 100;
643 auth
= malloc(auth_len
);
645 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
649 snprintf(auth
, auth_len
, "%s: Basic %s", auth_str
, b64_usr_pass
);
650 http_set_field( http_hdr
, auth
);
655 free( b64_usr_pass
);
662 http_add_basic_authentication( HTTP_header_t
*http_hdr
, const char *username
, const char *password
) {
663 return http_add_authentication(http_hdr
, username
, password
, "Authorization");
667 http_add_basic_proxy_authentication( HTTP_header_t
*http_hdr
, const char *username
, const char *password
) {
668 return http_add_authentication(http_hdr
, username
, password
, "Proxy-Authorization");
672 http_debug_hdr( HTTP_header_t
*http_hdr
) {
675 if( http_hdr
==NULL
) return;
677 mp_msg(MSGT_NETWORK
,MSGL_V
,"--- HTTP DEBUG HEADER --- START ---\n");
678 mp_msg(MSGT_NETWORK
,MSGL_V
,"protocol: [%s]\n", http_hdr
->protocol
);
679 mp_msg(MSGT_NETWORK
,MSGL_V
,"http minor version: [%d]\n", http_hdr
->http_minor_version
);
680 mp_msg(MSGT_NETWORK
,MSGL_V
,"uri: [%s]\n", http_hdr
->uri
);
681 mp_msg(MSGT_NETWORK
,MSGL_V
,"method: [%s]\n", http_hdr
->method
);
682 mp_msg(MSGT_NETWORK
,MSGL_V
,"status code: [%d]\n", http_hdr
->status_code
);
683 mp_msg(MSGT_NETWORK
,MSGL_V
,"reason phrase: [%s]\n", http_hdr
->reason_phrase
);
684 mp_msg(MSGT_NETWORK
,MSGL_V
,"body size: [%zd]\n", http_hdr
->body_size
);
686 mp_msg(MSGT_NETWORK
,MSGL_V
,"Fields:\n");
687 field
= http_hdr
->first_field
;
688 while( field
!=NULL
) {
689 mp_msg(MSGT_NETWORK
,MSGL_V
," %d - %s\n", i
++, field
->field_name
);
692 mp_msg(MSGT_NETWORK
,MSGL_V
,"--- HTTP DEBUG HEADER --- END ---\n");
695 static void print_icy_metadata(HTTP_header_t
*http_hdr
) {
696 const char *field_data
;
697 // note: I skip icy-notice1 and 2, as they contain html <BR>
698 // and are IMHO useless info ::atmos
699 if( (field_data
= http_get_field(http_hdr
, "icy-name")) != NULL
)
700 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Name : %s\n", field_data
);
701 if( (field_data
= http_get_field(http_hdr
, "icy-genre")) != NULL
)
702 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Genre : %s\n", field_data
);
703 if( (field_data
= http_get_field(http_hdr
, "icy-url")) != NULL
)
704 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Website: %s\n", field_data
);
705 // XXX: does this really mean public server? ::atmos
706 if( (field_data
= http_get_field(http_hdr
, "icy-pub")) != NULL
)
707 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Public : %s\n", atoi(field_data
)?"yes":"no");
708 if( (field_data
= http_get_field(http_hdr
, "icy-br")) != NULL
)
709 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Bitrate: %skbit/s\n", field_data
);
712 //! If this function succeeds you must closesocket stream->fd
713 static int http_streaming_start(stream_t
*stream
, int* file_format
) {
714 HTTP_header_t
*http_hdr
= NULL
;
716 int res
= STREAM_UNSUPPORTED
;
721 const char *content_length
;
723 URL_t
*url
= stream
->streaming_ctrl
->url
;
728 if (fd
> 0) closesocket(fd
);
729 fd
= http_send_request( url
, 0 );
735 http_hdr
= http_read_response( fd
);
736 if( http_hdr
==NULL
) {
740 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) ) {
741 http_debug_hdr( http_hdr
);
744 // Check if we can make partial content requests and thus seek in http-streams
745 if( http_hdr
!=NULL
&& http_hdr
->status_code
==200 ) {
746 const char *accept_ranges
= http_get_field(http_hdr
,"Accept-Ranges");
747 const char *server
= http_get_field(http_hdr
, "Server");
749 seekable
= strncmp(accept_ranges
,"bytes",5)==0;
750 else if (server
&& (strcmp(server
, "gvs 1.0") == 0 ||
751 strncmp(server
, "MakeMKV", 7) == 0)) {
752 // HACK for youtube and MakeMKV incorrectly claiming not to support seeking
753 mp_msg(MSGT_NETWORK
, MSGL_WARN
, "Broken webserver, incorrectly claims to not support Accept-Ranges\n");
758 print_icy_metadata(http_hdr
);
760 // Check if the response is an ICY status_code reason_phrase
761 if( !strcasecmp(http_hdr
->protocol
, "ICY") ||
762 http_get_field(http_hdr
, "Icy-MetaInt") ) {
763 switch( http_hdr
->status_code
) {
766 // If content-type == video/nsv we most likely have a winamp video stream
767 // otherwise it should be mp3. if there are more types consider adding mime type
768 // handling like later
769 if ( (field_data
= http_get_field(http_hdr
, "content-type")) != NULL
&& (!strcmp(field_data
, "video/nsv") || !strcmp(field_data
, "misc/ultravox")))
770 *file_format
= DEMUXER_TYPE_NSV
;
771 else if ( (field_data
= http_get_field(http_hdr
, "content-type")) != NULL
&& (!strcmp(field_data
, "audio/aacp") || !strcmp(field_data
, "audio/aac")))
772 *file_format
= DEMUXER_TYPE_AAC
;
774 *file_format
= DEMUXER_TYPE_AUDIO
;
778 case 400: // Server Full
779 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server is full, skipping!\n");
781 case 401: // Service Unavailable
782 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server return service unavailable, skipping!\n");
784 case 403: // Service Forbidden
785 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server return 'Service Forbidden'\n");
787 case 404: // Resource Not Found
788 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server couldn't find requested stream, skipping!\n");
791 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
796 // Assume standard http if not ICY
797 switch( http_hdr
->status_code
) {
799 content_length
= http_get_field(http_hdr
, "Content-Length");
800 if (content_length
) {
801 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", content_length
);
802 stream
->end_pos
= atoll(content_length
);
804 // Look if we can use the Content-Type
805 content_type
= http_get_field( http_hdr
, "Content-Type" );
806 if( content_type
!=NULL
) {
809 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", content_type
);
810 // Check in the mime type table for a demuxer type
811 for (i
= 0; mime_type_table
[i
].mime_type
!= NULL
; i
++) {
812 if( !strcasecmp( content_type
, mime_type_table
[i
].mime_type
) ) {
813 *file_format
= mime_type_table
[i
].demuxer_type
;
819 // Not found in the mime type table, don't fail,
820 // we should try raw HTTP
824 case 301: // Permanently
825 case 302: // Temporarily
826 case 303: // See Other
827 case 307: // Temporarily (since HTTP/1.1)
828 // TODO: RFC 2616, recommand to detect infinite redirection loops
829 next_url
= http_get_field( http_hdr
, "Location" );
830 if( next_url
!=NULL
) {
831 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
832 stream
->streaming_ctrl
->url
= url_redirect( &url
, next_url
);
833 if (!strcasecmp(url
->protocol
, "mms")) {
834 res
= STREAM_REDIRECTED
;
837 if (strcasecmp(url
->protocol
, "http")) {
838 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Unsupported http %d redirect to %s protocol\n", http_hdr
->status_code
, url
->protocol
);
843 url
->protocol
= strdup("unsv");
848 case 401: // Authentication required
849 if( http_authenticate(http_hdr
, url
, &auth_retry
)<0 )
854 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Server returned %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
860 if (fd
> 0) closesocket( fd
);
862 http_free( http_hdr
);
865 stream
->streaming_ctrl
->data
= (void*)http_hdr
;
870 static int fixup_open(stream_t
*stream
,int seekable
) {
871 HTTP_header_t
*http_hdr
= stream
->streaming_ctrl
->data
;
872 int is_icy
= http_hdr
&& http_get_field(http_hdr
, "Icy-MetaInt");
873 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
875 stream
->type
= STREAMTYPE_STREAM
;
876 if(!is_icy
&& !is_ultravox
&& seekable
)
878 stream
->flags
|= MP_STREAM_SEEK
;
879 stream
->seek
= http_seek
;
881 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
882 if ((!is_icy
&& !is_ultravox
) || scast_streaming_start(stream
))
883 if(nop_streaming_start( stream
)) {
884 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_start failed\n");
886 closesocket(stream
->fd
);
888 streaming_ctrl_free(stream
->streaming_ctrl
);
889 stream
->streaming_ctrl
= NULL
;
890 return STREAM_UNSUPPORTED
;
893 fixup_network_stream_cache(stream
);
897 static int open_s1(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
901 stream
->streaming_ctrl
= streaming_ctrl_new();
902 if( stream
->streaming_ctrl
==NULL
) {
905 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
906 url
= url_new(stream
->url
);
907 stream
->streaming_ctrl
->url
= check4proxies(url
);
910 mp_msg(MSGT_OPEN
, MSGL_V
, "STREAM_HTTP(1), URL: %s\n", stream
->url
);
911 seekable
= http_streaming_start(stream
, file_format
);
912 if((seekable
< 0) || (*file_format
== DEMUXER_TYPE_ASF
)) {
914 closesocket(stream
->fd
);
916 if (seekable
== STREAM_REDIRECTED
)
918 streaming_ctrl_free(stream
->streaming_ctrl
);
919 stream
->streaming_ctrl
= NULL
;
920 return STREAM_UNSUPPORTED
;
923 return fixup_open(stream
, seekable
);
926 static int open_s2(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
930 stream
->streaming_ctrl
= streaming_ctrl_new();
931 if( stream
->streaming_ctrl
==NULL
) {
934 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
935 url
= url_new(stream
->url
);
936 stream
->streaming_ctrl
->url
= check4proxies(url
);
939 mp_msg(MSGT_OPEN
, MSGL_V
, "STREAM_HTTP(2), URL: %s\n", stream
->url
);
940 seekable
= http_streaming_start(stream
, file_format
);
943 closesocket(stream
->fd
);
945 streaming_ctrl_free(stream
->streaming_ctrl
);
946 stream
->streaming_ctrl
= NULL
;
947 return STREAM_UNSUPPORTED
;
950 return fixup_open(stream
, seekable
);
954 const stream_info_t stream_info_http1
= {
957 "Bertrand, Albeau, Reimar Doeffinger, Arpi?",
960 {"http", "http_proxy", "unsv", "icyx", "noicyx", NULL
},
962 0 // Urls are an option string
965 const stream_info_t stream_info_http2
= {
968 "Bertrand, Albeu, Arpi? who?",
969 "plain http, also used as fallback for many other protocols",
971 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL
}, //all the others as fallback
973 0 // Urls are an option string