3 * by Bertrand Baudet <bertrand_baudet@yahoo.com>
4 * (C) 2001, MPlayer team.
15 #define closesocket close
26 #include "libmpdemux/demuxer.h"
31 extern const mime_struct_t mime_type_table
[];
32 extern int stream_cache_size
;
33 extern int network_bandwidth
;
35 extern int http_seek(stream_t
*stream
, off_t pos
);
44 * \brief first read any data from sc->buffer then from fd
45 * \param fd file descriptor to read data from
46 * \param buffer buffer to read into
47 * \param len how many bytes to read
48 * \param sc streaming control containing buffer to read from first
49 * \return len unless there is a read error or eof
51 static unsigned my_read(int fd
, char *buffer
, int len
, streaming_ctrl_t
*sc
) {
53 unsigned cp_len
= sc
->buffer_size
- sc
->buffer_pos
;
56 memcpy(buffer
, &sc
->buffer
[sc
->buffer_pos
], cp_len
);
57 sc
->buffer_pos
+= cp_len
;
60 int ret
= recv(fd
, &buffer
[pos
], len
- pos
, 0);
69 * \brief read and process (i.e. discard *g*) a block of ultravox metadata
70 * \param fd file descriptor to read from
71 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
72 * \return number of real data before next metadata block starts or 0 on error
74 static unsigned uvox_meta_read(int fd
, streaming_ctrl_t
*sc
) {
76 unsigned char info
[6] = {0, 0, 0, 0, 0, 0};
79 info_read
= my_read(fd
, info
, 1, sc
);
81 info_read
= my_read(fd
, info
, 6, sc
);
83 info_read
+= my_read(fd
, &info
[1], 5, sc
);
84 if (info_read
!= 6) // read error or eof
86 // sync byte and reserved flags
87 if (info
[0] != 0x5a || (info
[1] & 0xfc) != 0x00) {
88 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "Invalid or unknown uvox metadata\n");
92 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Encrypted ultravox data\n");
93 metaint
= info
[4] << 8 | info
[5];
94 if ((info
[3] & 0xf) < 0x07) { // discard any metadata nonsense
95 char *metabuf
= malloc(metaint
);
96 my_read(fd
, metabuf
, metaint
, sc
);
99 } while ((info
[3] & 0xf) < 0x07);
104 * \brief read one scast meta data entry and print it
105 * \param fd file descriptor to read from
106 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
108 static void scast_meta_read(int fd
, streaming_ctrl_t
*sc
) {
109 unsigned char tmp
= 0;
111 my_read(fd
, &tmp
, 1, sc
);
114 char *info
= malloc(metalen
+ 1);
115 unsigned nlen
= my_read(fd
, info
, metalen
, sc
);
117 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "\nICY Info: %s\n", info
);
123 * \brief read data from scast/ultravox stream without any metadata
124 * \param fd file descriptor to read from
125 * \param buffer buffer to read data into
126 * \param size number of bytes to read
127 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
129 static int scast_streaming_read(int fd
, char *buffer
, int size
,
130 streaming_ctrl_t
*sc
) {
131 scast_data_t
*sd
= (scast_data_t
*)sc
->data
;
135 // first read remaining data up to next metadata
136 block
= sd
->metaint
- sd
->metapos
;
139 ret
= my_read(fd
, buffer
, block
, sc
);
142 if (ret
!= block
) // read problems or eof
145 while (done
< size
) { // now comes the metadata
148 sd
->metaint
= uvox_meta_read(fd
, sc
);
153 scast_meta_read(fd
, sc
); // read and display metadata
156 if (block
> sd
->metaint
)
158 ret
= my_read(fd
, &buffer
[done
], block
, sc
);
161 if (ret
!= block
) // read problems or eof
167 static int scast_streaming_start(stream_t
*stream
) {
169 scast_data_t
*scast_data
;
170 HTTP_header_t
*http_hdr
= stream
->streaming_ctrl
->data
;
171 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
172 if (!stream
|| stream
->fd
< 0 || !http_hdr
)
177 metaint
= atoi(http_get_field(http_hdr
, "Icy-MetaInt"));
181 stream
->streaming_ctrl
->buffer
= malloc(http_hdr
->body_size
);
182 stream
->streaming_ctrl
->buffer_size
= http_hdr
->body_size
;
183 stream
->streaming_ctrl
->buffer_pos
= 0;
184 memcpy(stream
->streaming_ctrl
->buffer
, http_hdr
->body
, http_hdr
->body_size
);
185 scast_data
= malloc(sizeof(scast_data_t
));
186 scast_data
->metaint
= metaint
;
187 scast_data
->metapos
= 0;
188 scast_data
->is_ultravox
= is_ultravox
;
190 stream
->streaming_ctrl
->data
= scast_data
;
191 stream
->streaming_ctrl
->streaming_read
= scast_streaming_read
;
192 stream
->streaming_ctrl
->streaming_seek
= NULL
;
193 stream
->streaming_ctrl
->prebuffer_size
= 64 * 1024; // 64 KBytes
194 stream
->streaming_ctrl
->buffering
= 1;
195 stream
->streaming_ctrl
->status
= streaming_playing_e
;
199 static int nop_streaming_start( stream_t
*stream
) {
200 HTTP_header_t
*http_hdr
= NULL
;
204 if( stream
==NULL
) return -1;
208 fd
= http_send_request( stream
->streaming_ctrl
->url
, 0 );
209 if( fd
<0 ) return -1;
210 http_hdr
= http_read_response( fd
);
211 if( http_hdr
==NULL
) return -1;
213 switch( http_hdr
->status_code
) {
215 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", http_get_field(http_hdr
, "Content-Type") );
216 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", http_get_field(http_hdr
, "Content-Length") );
217 if( http_hdr
->body_size
>0 ) {
218 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
219 http_free( http_hdr
);
225 case 301: // Permanently
226 case 302: // Temporarily
227 case 303: // See Other
229 next_url
= http_get_field( http_hdr
, "Location" );
231 if (next_url
!= NULL
)
232 rd_url
=url_new(next_url
);
234 if (next_url
!= NULL
&& rd_url
!= NULL
) {
235 mp_msg(MSGT_NETWORK
,MSGL_STATUS
,"Redirected: Using this url instead %s\n",next_url
);
236 stream
->streaming_ctrl
->url
=check4proxies(rd_url
);
237 ret
=nop_streaming_start(stream
); //recursively get streaming started
239 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Redirection failed\n");
245 case 401: //Authorization required
246 case 403: //Forbidden
247 case 404: //Not found
248 case 500: //Server Error
250 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Server returned code %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
258 http_hdr
= (HTTP_header_t
*)stream
->streaming_ctrl
->data
;
259 if( http_hdr
->body_size
>0 ) {
260 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
261 http_free( http_hdr
);
262 stream
->streaming_ctrl
->data
= NULL
;
269 http_free( http_hdr
);
270 stream
->streaming_ctrl
->data
= NULL
;
273 stream
->streaming_ctrl
->streaming_read
= nop_streaming_read
;
274 stream
->streaming_ctrl
->streaming_seek
= nop_streaming_seek
;
275 stream
->streaming_ctrl
->prebuffer_size
= 64*1024; // 64 KBytes
276 stream
->streaming_ctrl
->buffering
= 1;
277 stream
->streaming_ctrl
->status
= streaming_playing_e
;
282 http_new_header(void) {
283 HTTP_header_t
*http_hdr
;
285 http_hdr
= malloc(sizeof(HTTP_header_t
));
286 if( http_hdr
==NULL
) return NULL
;
287 memset( http_hdr
, 0, sizeof(HTTP_header_t
) );
293 http_free( HTTP_header_t
*http_hdr
) {
294 HTTP_field_t
*field
, *field2free
;
295 if( http_hdr
==NULL
) return;
296 if( http_hdr
->protocol
!=NULL
) free( http_hdr
->protocol
);
297 if( http_hdr
->uri
!=NULL
) free( http_hdr
->uri
);
298 if( http_hdr
->reason_phrase
!=NULL
) free( http_hdr
->reason_phrase
);
299 if( http_hdr
->field_search
!=NULL
) free( http_hdr
->field_search
);
300 if( http_hdr
->method
!=NULL
) free( http_hdr
->method
);
301 if( http_hdr
->buffer
!=NULL
) free( http_hdr
->buffer
);
302 field
= http_hdr
->first_field
;
303 while( field
!=NULL
) {
305 if (field
->field_name
)
306 free(field
->field_name
);
315 http_response_append( HTTP_header_t
*http_hdr
, char *response
, int length
) {
316 if( http_hdr
==NULL
|| response
==NULL
|| length
<0 ) return -1;
318 if( (unsigned)length
> SIZE_MAX
- http_hdr
->buffer_size
- 1) {
319 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Bad size in memory (re)allocation\n");
322 http_hdr
->buffer
= (char*)realloc( http_hdr
->buffer
, http_hdr
->buffer_size
+length
+1 );
323 if( http_hdr
->buffer
==NULL
) {
324 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory (re)allocation failed\n");
327 memcpy( http_hdr
->buffer
+http_hdr
->buffer_size
, response
, length
);
328 http_hdr
->buffer_size
+= length
;
329 http_hdr
->buffer
[http_hdr
->buffer_size
]=0; // close the string!
330 return http_hdr
->buffer_size
;
334 http_is_header_entire( HTTP_header_t
*http_hdr
) {
335 if( http_hdr
==NULL
) return -1;
336 if( http_hdr
->buffer
==NULL
) return 0; // empty
338 if( strstr(http_hdr
->buffer
, "\r\n\r\n")==NULL
&&
339 strstr(http_hdr
->buffer
, "\n\n")==NULL
) return 0;
344 http_response_parse( HTTP_header_t
*http_hdr
) {
347 int pos_hdr_sep
, hdr_sep_len
;
349 if( http_hdr
==NULL
) return -1;
350 if( http_hdr
->is_parsed
) return 0;
353 hdr_ptr
= strstr( http_hdr
->buffer
, " " );
354 if( hdr_ptr
==NULL
) {
355 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. No space separator found.\n");
358 len
= hdr_ptr
-http_hdr
->buffer
;
359 http_hdr
->protocol
= malloc(len
+1);
360 if( http_hdr
->protocol
==NULL
) {
361 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
364 strncpy( http_hdr
->protocol
, http_hdr
->buffer
, len
);
365 http_hdr
->protocol
[len
]='\0';
366 if( !strncasecmp( http_hdr
->protocol
, "HTTP", 4) ) {
367 if( sscanf( http_hdr
->protocol
+5,"1.%d", &(http_hdr
->http_minor_version
) )!=1 ) {
368 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get HTTP minor version.\n");
373 // Get the status code
374 if( sscanf( ++hdr_ptr
, "%d", &(http_hdr
->status_code
) )!=1 ) {
375 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get status code.\n");
380 // Get the reason phrase
381 ptr
= strstr( hdr_ptr
, "\n" );
382 if( hdr_ptr
==NULL
) {
383 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get the reason phrase.\n");
387 http_hdr
->reason_phrase
= malloc(len
+1);
388 if( http_hdr
->reason_phrase
==NULL
) {
389 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
392 strncpy( http_hdr
->reason_phrase
, hdr_ptr
, len
);
393 if( http_hdr
->reason_phrase
[len
-1]=='\r' ) {
396 http_hdr
->reason_phrase
[len
]='\0';
398 // Set the position of the header separator: \r\n\r\n
400 ptr
= strstr( http_hdr
->buffer
, "\r\n\r\n" );
402 ptr
= strstr( http_hdr
->buffer
, "\n\n" );
404 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Header may be incomplete. No CRLF CRLF found.\n");
409 pos_hdr_sep
= ptr
-http_hdr
->buffer
;
411 // Point to the first line after the method line.
412 hdr_ptr
= strstr( http_hdr
->buffer
, "\n" )+1;
415 while( *ptr
!='\r' && *ptr
!='\n' ) ptr
++;
418 field
= (char*)realloc(field
, len
+1);
420 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
423 strncpy( field
, hdr_ptr
, len
);
425 http_set_field( http_hdr
, field
);
426 hdr_ptr
= ptr
+((*ptr
=='\r')?2:1);
427 } while( hdr_ptr
<(http_hdr
->buffer
+pos_hdr_sep
) );
429 if( field
!=NULL
) free( field
);
431 if( pos_hdr_sep
+hdr_sep_len
<http_hdr
->buffer_size
) {
432 // Response has data!
433 http_hdr
->body
= http_hdr
->buffer
+pos_hdr_sep
+hdr_sep_len
;
434 http_hdr
->body_size
= http_hdr
->buffer_size
-(pos_hdr_sep
+hdr_sep_len
);
437 http_hdr
->is_parsed
= 1;
442 http_build_request( HTTP_header_t
*http_hdr
) {
443 char *ptr
, *uri
=NULL
;
446 if( http_hdr
==NULL
) return NULL
;
448 if( http_hdr
->method
==NULL
) http_set_method( http_hdr
, "GET");
449 if( http_hdr
->uri
==NULL
) http_set_uri( http_hdr
, "/");
451 uri
= malloc(strlen(http_hdr
->uri
) + 1);
453 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
456 strcpy(uri
,http_hdr
->uri
);
459 //**** Compute the request length
460 // Add the Method line
461 len
= strlen(http_hdr
->method
)+strlen(uri
)+12;
463 field
= http_hdr
->first_field
;
464 while( field
!=NULL
) {
465 len
+= strlen(field
->field_name
)+2;
471 if( http_hdr
->body
!=NULL
) {
472 len
+= http_hdr
->body_size
;
474 // Free the buffer if it was previously used
475 if( http_hdr
->buffer
!=NULL
) {
476 free( http_hdr
->buffer
);
477 http_hdr
->buffer
= NULL
;
479 http_hdr
->buffer
= malloc(len
+1);
480 if( http_hdr
->buffer
==NULL
) {
481 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
484 http_hdr
->buffer_size
= len
;
486 //*** Building the request
487 ptr
= http_hdr
->buffer
;
488 // Add the method line
489 ptr
+= sprintf( ptr
, "%s %s HTTP/1.%d\r\n", http_hdr
->method
, uri
, http_hdr
->http_minor_version
);
490 field
= http_hdr
->first_field
;
492 while( field
!=NULL
) {
493 ptr
+= sprintf( ptr
, "%s\r\n", field
->field_name
);
496 ptr
+= sprintf( ptr
, "\r\n" );
498 if( http_hdr
->body
!=NULL
) {
499 memcpy( ptr
, http_hdr
->body
, http_hdr
->body_size
);
502 if( uri
) free( uri
);
503 return http_hdr
->buffer
;
507 http_get_field( HTTP_header_t
*http_hdr
, const char *field_name
) {
508 if( http_hdr
==NULL
|| field_name
==NULL
) return NULL
;
509 http_hdr
->field_search_pos
= http_hdr
->first_field
;
510 http_hdr
->field_search
= (char*)realloc( http_hdr
->field_search
, strlen(field_name
)+1 );
511 if( http_hdr
->field_search
==NULL
) {
512 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
515 strcpy( http_hdr
->field_search
, field_name
);
516 return http_get_next_field( http_hdr
);
520 http_get_next_field( HTTP_header_t
*http_hdr
) {
523 if( http_hdr
==NULL
) return NULL
;
525 field
= http_hdr
->field_search_pos
;
526 while( field
!=NULL
) {
527 ptr
= strstr( field
->field_name
, ":" );
528 if( ptr
==NULL
) return NULL
;
529 if( !strncasecmp( field
->field_name
, http_hdr
->field_search
, ptr
-(field
->field_name
) ) ) {
530 ptr
++; // Skip the column
531 while( ptr
[0]==' ' ) ptr
++; // Skip the spaces if there is some
532 http_hdr
->field_search_pos
= field
->next
;
533 return ptr
; // return the value without the field name
541 http_set_field( HTTP_header_t
*http_hdr
, const char *field_name
) {
542 HTTP_field_t
*new_field
;
543 if( http_hdr
==NULL
|| field_name
==NULL
) return;
545 new_field
= malloc(sizeof(HTTP_field_t
));
546 if( new_field
==NULL
) {
547 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
550 new_field
->next
= NULL
;
551 new_field
->field_name
= malloc(strlen(field_name
)+1);
552 if( new_field
->field_name
==NULL
) {
553 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
556 strcpy( new_field
->field_name
, field_name
);
558 if( http_hdr
->last_field
==NULL
) {
559 http_hdr
->first_field
= new_field
;
561 http_hdr
->last_field
->next
= new_field
;
563 http_hdr
->last_field
= new_field
;
564 http_hdr
->field_nb
++;
568 http_set_method( HTTP_header_t
*http_hdr
, const char *method
) {
569 if( http_hdr
==NULL
|| method
==NULL
) return;
571 http_hdr
->method
= malloc(strlen(method
)+1);
572 if( http_hdr
->method
==NULL
) {
573 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
576 strcpy( http_hdr
->method
, method
);
580 http_set_uri( HTTP_header_t
*http_hdr
, const char *uri
) {
581 if( http_hdr
==NULL
|| uri
==NULL
) return;
583 http_hdr
->uri
= malloc(strlen(uri
)+1);
584 if( http_hdr
->uri
==NULL
) {
585 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
588 strcpy( http_hdr
->uri
, uri
);
592 http_add_basic_authentication( HTTP_header_t
*http_hdr
, const char *username
, const char *password
) {
593 char *auth
= NULL
, *usr_pass
= NULL
, *b64_usr_pass
= NULL
;
594 int encoded_len
, pass_len
=0, out_len
;
596 if( http_hdr
==NULL
|| username
==NULL
) return -1;
598 if( password
!=NULL
) {
599 pass_len
= strlen(password
);
602 usr_pass
= malloc(strlen(username
)+pass_len
+2);
603 if( usr_pass
==NULL
) {
604 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
608 sprintf( usr_pass
, "%s:%s", username
, (password
==NULL
)?"":password
);
610 // Base 64 encode with at least 33% more data than the original size
611 encoded_len
= strlen(usr_pass
)*2;
612 b64_usr_pass
= malloc(encoded_len
);
613 if( b64_usr_pass
==NULL
) {
614 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
618 out_len
= base64_encode( usr_pass
, strlen(usr_pass
), b64_usr_pass
, encoded_len
);
620 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Base64 out overflow\n");
624 b64_usr_pass
[out_len
]='\0';
626 auth
= malloc(encoded_len
+22);
628 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
632 sprintf( auth
, "Authorization: Basic %s", b64_usr_pass
);
633 http_set_field( http_hdr
, auth
);
638 free( b64_usr_pass
);
645 http_debug_hdr( HTTP_header_t
*http_hdr
) {
648 if( http_hdr
==NULL
) return;
650 mp_msg(MSGT_NETWORK
,MSGL_V
,"--- HTTP DEBUG HEADER --- START ---\n");
651 mp_msg(MSGT_NETWORK
,MSGL_V
,"protocol: [%s]\n", http_hdr
->protocol
);
652 mp_msg(MSGT_NETWORK
,MSGL_V
,"http minor version: [%d]\n", http_hdr
->http_minor_version
);
653 mp_msg(MSGT_NETWORK
,MSGL_V
,"uri: [%s]\n", http_hdr
->uri
);
654 mp_msg(MSGT_NETWORK
,MSGL_V
,"method: [%s]\n", http_hdr
->method
);
655 mp_msg(MSGT_NETWORK
,MSGL_V
,"status code: [%d]\n", http_hdr
->status_code
);
656 mp_msg(MSGT_NETWORK
,MSGL_V
,"reason phrase: [%s]\n", http_hdr
->reason_phrase
);
657 mp_msg(MSGT_NETWORK
,MSGL_V
,"body size: [%d]\n", http_hdr
->body_size
);
659 mp_msg(MSGT_NETWORK
,MSGL_V
,"Fields:\n");
660 field
= http_hdr
->first_field
;
661 while( field
!=NULL
) {
662 mp_msg(MSGT_NETWORK
,MSGL_V
," %d - %s\n", i
++, field
->field_name
);
665 mp_msg(MSGT_NETWORK
,MSGL_V
,"--- HTTP DEBUG HEADER --- END ---\n");
669 base64_encode(const void *enc
, int encLen
, char *out
, int outMax
) {
670 static const char b64
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
672 unsigned char *encBuf
;
677 encBuf
= (unsigned char*)enc
;
692 } else if( shift
>0 ) {
693 // Pad last bits to 6 bits - will end next loop
697 // As per RFC 2045, section 6.8,
698 // pad output as necessary: 0 to 2 '=' chars.
707 // Encode 6 bit segments
709 if (outLen
>= outMax
)
712 *out
= b64
[ (bits
>> shift
) & 0x3F ];
719 //! If this function succeeds you must closesocket stream->fd
720 static int http_streaming_start(stream_t
*stream
, int* file_format
) {
721 HTTP_header_t
*http_hdr
= NULL
;
724 int res
= STREAM_UNSUPPORTED
;
730 URL_t
*url
= stream
->streaming_ctrl
->url
;
735 if (fd
> 0) closesocket(fd
);
736 fd
= http_send_request( url
, 0 );
742 http_hdr
= http_read_response( fd
);
743 if( http_hdr
==NULL
) {
747 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) ) {
748 http_debug_hdr( http_hdr
);
751 // Check if we can make partial content requests and thus seek in http-streams
752 if( http_hdr
!=NULL
&& http_hdr
->status_code
==200 ) {
754 if( (accept_ranges
= http_get_field(http_hdr
,"Accept-Ranges")) != NULL
)
755 seekable
= strncmp(accept_ranges
,"bytes",5)==0;
758 // Check if the response is an ICY status_code reason_phrase
759 if( !strcasecmp(http_hdr
->protocol
, "ICY") ) {
760 switch( http_hdr
->status_code
) {
762 char *field_data
= NULL
;
763 // note: I skip icy-notice1 and 2, as they contain html <BR>
764 // and are IMHO useless info ::atmos
765 if( (field_data
= http_get_field(http_hdr
, "icy-name")) != NULL
)
766 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Name : %s\n", field_data
); field_data
= NULL
;
767 if( (field_data
= http_get_field(http_hdr
, "icy-genre")) != NULL
)
768 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Genre : %s\n", field_data
); field_data
= NULL
;
769 if( (field_data
= http_get_field(http_hdr
, "icy-url")) != NULL
)
770 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Website: %s\n", field_data
); field_data
= NULL
;
771 // XXX: does this really mean public server? ::atmos
772 if( (field_data
= http_get_field(http_hdr
, "icy-pub")) != NULL
)
773 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Public : %s\n", atoi(field_data
)?"yes":"no"); field_data
= NULL
;
774 if( (field_data
= http_get_field(http_hdr
, "icy-br")) != NULL
)
775 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Bitrate: %skbit/s\n", field_data
); field_data
= NULL
;
777 // If content-type == video/nsv we most likely have a winamp video stream
778 // otherwise it should be mp3. if there are more types consider adding mime type
779 // handling like later
780 if ( (field_data
= http_get_field(http_hdr
, "content-type")) != NULL
&& (!strcmp(field_data
, "video/nsv") || !strcmp(field_data
, "misc/ultravox")))
781 *file_format
= DEMUXER_TYPE_NSV
;
782 else if ( (field_data
= http_get_field(http_hdr
, "content-type")) != NULL
&& (!strcmp(field_data
, "audio/aacp") || !strcmp(field_data
, "audio/aac")))
783 *file_format
= DEMUXER_TYPE_AAC
;
785 *file_format
= DEMUXER_TYPE_AUDIO
;
789 case 400: // Server Full
790 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server is full, skipping!\n");
792 case 401: // Service Unavailable
793 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server return service unavailable, skipping!\n");
795 case 403: // Service Forbidden
796 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server return 'Service Forbidden'\n");
798 case 404: // Resource Not Found
799 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server couldn't find requested stream, skipping!\n");
802 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
807 // Assume standard http if not ICY
808 switch( http_hdr
->status_code
) {
810 // Look if we can use the Content-Type
811 content_type
= http_get_field( http_hdr
, "Content-Type" );
812 if( content_type
!=NULL
) {
813 char *content_length
= NULL
;
814 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", content_type
);
815 if( (content_length
= http_get_field(http_hdr
, "Content-Length")) != NULL
)
816 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", http_get_field(http_hdr
, "Content-Length"));
817 // Check in the mime type table for a demuxer type
819 while(mime_type_table
[i
].mime_type
!= NULL
) {
820 if( !strcasecmp( content_type
, mime_type_table
[i
].mime_type
) ) {
821 *file_format
= mime_type_table
[i
].demuxer_type
;
828 // Not found in the mime type table, don't fail,
829 // we should try raw HTTP
833 case 301: // Permanently
834 case 302: // Temporarily
835 case 303: // See Other
836 // TODO: RFC 2616, recommand to detect infinite redirection loops
837 next_url
= http_get_field( http_hdr
, "Location" );
838 if( next_url
!=NULL
) {
839 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
840 stream
->streaming_ctrl
->url
= url_redirect( &url
, next_url
);
841 if (!strcasecmp(url
->protocol
, "mms")) {
842 res
= STREAM_REDIRECTED
;
845 if (strcasecmp(url
->protocol
, "http")) {
846 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Unsupported http %d redirect to %s protocol\n", http_hdr
->status_code
, url
->protocol
);
851 url
->protocol
= strdup("unsv");
856 case 401: // Authentication required
857 if( http_authenticate(http_hdr
, url
, &auth_retry
)<0 )
862 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Server returned %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
868 if (fd
> 0) closesocket( fd
);
870 http_free( http_hdr
);
873 stream
->streaming_ctrl
->data
= (void*)http_hdr
;
878 static int fixup_open(stream_t
*stream
,int seekable
) {
879 HTTP_header_t
*http_hdr
= stream
->streaming_ctrl
->data
;
880 int is_icy
= http_hdr
&& http_get_field(http_hdr
, "Icy-MetaInt");
881 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
883 stream
->type
= STREAMTYPE_STREAM
;
884 if(!is_icy
&& !is_ultravox
&& seekable
)
886 stream
->flags
|= STREAM_SEEK
;
887 stream
->seek
= http_seek
;
889 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
890 if ((!is_icy
&& !is_ultravox
) || scast_streaming_start(stream
))
891 if(nop_streaming_start( stream
)) {
892 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_start failed\n");
894 closesocket(stream
->fd
);
896 streaming_ctrl_free(stream
->streaming_ctrl
);
897 stream
->streaming_ctrl
= NULL
;
898 return STREAM_UNSUPPORTED
;
901 fixup_network_stream_cache(stream
);
905 static int open_s1(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
909 stream
->streaming_ctrl
= streaming_ctrl_new();
910 if( stream
->streaming_ctrl
==NULL
) {
913 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
914 url
= url_new(stream
->url
);
915 stream
->streaming_ctrl
->url
= check4proxies(url
);
918 mp_msg(MSGT_OPEN
, MSGL_V
, "STREAM_HTTP(1), URL: %s\n", stream
->url
);
919 seekable
= http_streaming_start(stream
, file_format
);
920 if((seekable
< 0) || (*file_format
== DEMUXER_TYPE_ASF
)) {
922 closesocket(stream
->fd
);
924 if (seekable
== STREAM_REDIRECTED
)
926 streaming_ctrl_free(stream
->streaming_ctrl
);
927 stream
->streaming_ctrl
= NULL
;
928 return STREAM_UNSUPPORTED
;
931 return fixup_open(stream
, seekable
);
934 static int open_s2(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
938 stream
->streaming_ctrl
= streaming_ctrl_new();
939 if( stream
->streaming_ctrl
==NULL
) {
942 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
943 url
= url_new(stream
->url
);
944 stream
->streaming_ctrl
->url
= check4proxies(url
);
947 mp_msg(MSGT_OPEN
, MSGL_V
, "STREAM_HTTP(2), URL: %s\n", stream
->url
);
948 seekable
= http_streaming_start(stream
, file_format
);
951 closesocket(stream
->fd
);
953 streaming_ctrl_free(stream
->streaming_ctrl
);
954 stream
->streaming_ctrl
= NULL
;
955 return STREAM_UNSUPPORTED
;
958 return fixup_open(stream
, seekable
);
962 const stream_info_t stream_info_http1
= {
965 "Bertrand, Albeau, Reimar Doeffinger, Arpi?",
968 {"http", "http_proxy", "unsv", NULL
},
970 0 // Urls are an option string
973 const stream_info_t stream_info_http2
= {
976 "Bertrand, Albeu, Arpi? who?",
977 "plain http, also used as fallback for many other protocols",
979 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL
}, //all the others as fallback
981 0 // Urls are an option string