.gitignore: add /po and /locale
[mplayer.git] / stream / http.c
blob9372cb1262eaeee3b40238a6c2d3fd00838284cb
1 /*
2 * HTTP Helper
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.
23 #include "config.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #if !HAVE_WINSOCK2_H
31 #else
32 #include <winsock2.h>
33 #include <ws2tcpip.h>
34 #endif
36 #include "http.h"
37 #include "url.h"
38 #include "mp_msg.h"
40 #include "stream.h"
41 #include "libmpdemux/demuxer.h"
42 #include "network.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)
49 #endif
51 extern int stream_cache_size;
52 extern int network_bandwidth;
54 typedef struct {
55 unsigned metaint;
56 unsigned metapos;
57 int is_ultravox;
58 } scast_data_t;
60 /**
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) {
69 unsigned pos = 0;
70 unsigned cp_len = sc->buffer_size - sc->buffer_pos;
71 if (cp_len > len)
72 cp_len = len;
73 memcpy(buffer, &sc->buffer[sc->buffer_pos], cp_len);
74 sc->buffer_pos += cp_len;
75 pos += cp_len;
76 while (pos < len) {
77 int ret = recv(fd, &buffer[pos], len - pos, 0);
78 if (ret <= 0)
79 break;
80 pos += ret;
82 return pos;
85 /**
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) {
92 unsigned metaint;
93 unsigned char info[6] = {0, 0, 0, 0, 0, 0};
94 int info_read;
95 do {
96 info_read = my_read(fd, info, 1, sc);
97 if (info[0] == 0x00)
98 info_read = my_read(fd, info, 6, sc);
99 else
100 info_read += my_read(fd, &info[1], 5, sc);
101 if (info_read != 6) // read error or eof
102 return 0;
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");
106 return 0;
108 if (info[1] & 0x01)
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);
114 free(metabuf);
116 } while ((info[3] & 0xf) < 0x07);
117 return metaint;
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;
127 unsigned metalen;
128 my_read(fd, &tmp, 1, sc);
129 metalen = tmp * 16;
130 if (metalen > 0) {
131 int i;
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] = '?';
138 info[nlen] = 0;
139 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\nICY Info: %s\n", info);
140 free(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;
154 unsigned block, ret;
155 unsigned done = 0;
157 // first read remaining data up to next metadata
158 block = sd->metaint - sd->metapos;
159 if (block > size)
160 block = size;
161 ret = my_read(fd, buffer, block, sc);
162 sd->metapos += ret;
163 done += ret;
164 if (ret != block) // read problems or eof
165 size = done;
167 while (done < size) { // now comes the metadata
168 if (sd->is_ultravox)
170 sd->metaint = uvox_meta_read(fd, sc);
171 if (!sd->metaint)
172 size = done;
174 else
175 scast_meta_read(fd, sc); // read and display metadata
176 sd->metapos = 0;
177 block = size - done;
178 if (block > sd->metaint)
179 block = sd->metaint;
180 ret = my_read(fd, &buffer[done], block, sc);
181 sd->metapos += ret;
182 done += ret;
183 if (ret != block) // read problems or eof
184 size = done;
186 return done;
189 static int scast_streaming_start(stream_t *stream) {
190 int metaint;
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)
195 return -1;
196 if (is_ultravox)
197 metaint = 0;
198 else {
199 metaint = atoi(http_get_field(http_hdr, "Icy-MetaInt"));
200 if (metaint <= 0)
201 return -1;
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;
211 http_free(http_hdr);
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;
218 return 0;
221 static int nop_streaming_start( stream_t *stream ) {
222 HTTP_header_t *http_hdr = NULL;
223 char *next_url=NULL;
224 URL_t *rd_url=NULL;
225 int fd,ret;
226 if( stream==NULL ) return -1;
228 fd = stream->fd;
229 if( fd<0 ) {
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 ) {
236 case 200: // OK
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 );
242 return -1;
245 break;
246 // Redirect
247 case 301: // Permanently
248 case 302: // Temporarily
249 case 303: // See Other
250 case 307: // Temporarily (since HTTP/1.1)
251 ret=-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
261 } else {
262 mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n");
263 closesocket( fd );
264 fd = -1;
266 return ret;
267 break;
268 case 401: //Authorization required
269 case 403: //Forbidden
270 case 404: //Not found
271 case 500: //Server Error
272 default:
273 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
274 closesocket( fd );
275 fd = -1;
276 return -1;
277 break;
279 stream->fd = fd;
280 } else {
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;
286 return -1;
291 if( http_hdr ) {
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;
301 return 0;
304 HTTP_header_t *
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;
311 return http_hdr;
314 void
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);
319 free(http_hdr->uri);
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 ) {
326 field2free = field;
327 free(field->field_name);
328 field = field->next;
329 free( field2free );
331 free( http_hdr );
332 http_hdr = NULL;
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");
341 return -1;
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");
346 return -1;
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;
361 return 1;
365 http_response_parse( HTTP_header_t *http_hdr ) {
366 char *hdr_ptr, *ptr;
367 char *field=NULL;
368 int pos_hdr_sep, hdr_sep_len;
369 size_t len;
370 if( http_hdr==NULL ) return -1;
371 if( http_hdr->is_parsed ) return 0;
373 // Get the protocol
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");
377 return -1;
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");
383 return -1;
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");
390 return -1;
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");
397 return -1;
399 hdr_ptr += 4;
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");
405 return -1;
407 len = ptr-hdr_ptr;
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");
411 return -1;
413 strncpy( http_hdr->reason_phrase, hdr_ptr, len );
414 if( http_hdr->reason_phrase[len-1]=='\r' ) {
415 len--;
417 http_hdr->reason_phrase[len]='\0';
419 // Set the position of the header separator: \r\n\r\n
420 hdr_sep_len = 4;
421 ptr = strstr( http_hdr->buffer, "\r\n\r\n" );
422 if( ptr==NULL ) {
423 ptr = strstr( http_hdr->buffer, "\n\n" );
424 if( ptr==NULL ) {
425 mp_msg(MSGT_NETWORK,MSGL_ERR,"Header may be incomplete. No CRLF CRLF found.\n");
426 return -1;
428 hdr_sep_len = 2;
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;
434 do {
435 ptr = hdr_ptr;
436 while( *ptr!='\r' && *ptr!='\n' ) ptr++;
437 len = ptr-hdr_ptr;
438 if( len==0 ) break;
439 field = realloc(field, len+1);
440 if( field==NULL ) {
441 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
442 return -1;
444 strncpy( field, hdr_ptr, len );
445 field[len]='\0';
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) );
450 free(field);
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;
459 return 0;
462 char *
463 http_build_request( HTTP_header_t *http_hdr ) {
464 char *ptr, *uri=NULL;
465 int len;
466 HTTP_field_t *field;
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, "/");
471 else {
472 uri = malloc(strlen(http_hdr->uri) + 1);
473 if( uri==NULL ) {
474 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
475 return NULL;
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;
483 // Add the fields
484 field = http_hdr->first_field;
485 while( field!=NULL ) {
486 len += strlen(field->field_name)+2;
487 field = field->next;
489 // Add the CRLF
490 len += 2;
491 // Add the body
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");
503 return NULL;
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;
512 // Add the field
513 while( field!=NULL ) {
514 ptr += sprintf( ptr, "%s\r\n", field->field_name );
515 field = field->next;
517 ptr += sprintf( ptr, "\r\n" );
518 // Add the body
519 if( http_hdr->body!=NULL ) {
520 memcpy( ptr, http_hdr->body, http_hdr->body_size );
523 free(uri);
524 return http_hdr->buffer;
527 char *
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");
534 return NULL;
536 strcpy( http_hdr->field_search, field_name );
537 return http_get_next_field( http_hdr );
540 char *
541 http_get_next_field( HTTP_header_t *http_hdr ) {
542 char *ptr;
543 HTTP_field_t *field;
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
556 field = field->next;
558 return NULL;
561 void
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");
569 return;
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");
575 free(new_field);
576 return;
578 strcpy( new_field->field_name, field_name );
580 if( http_hdr->last_field==NULL ) {
581 http_hdr->first_field = new_field;
582 } else {
583 http_hdr->last_field->next = new_field;
585 http_hdr->last_field = new_field;
586 http_hdr->field_nb++;
589 void
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");
596 return;
598 strcpy( http_hdr->method, method );
601 void
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");
608 return;
610 strcpy( http_hdr->uri, uri );
613 static int
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;
618 int res = -1;
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");
629 goto out;
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");
638 goto out;
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);
644 if( auth==NULL ) {
645 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
646 goto out;
649 snprintf(auth, auth_len, "%s: Basic %s", auth_str, b64_usr_pass);
650 http_set_field( http_hdr, auth );
651 res = 0;
653 out:
654 free( usr_pass );
655 free( b64_usr_pass );
656 free( auth );
658 return res;
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");
671 void
672 http_debug_hdr( HTTP_header_t *http_hdr ) {
673 HTTP_field_t *field;
674 int i = 0;
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 );
690 field = field->next;
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;
715 int fd = stream->fd;
716 int res = STREAM_UNSUPPORTED;
717 int redirect = 0;
718 int auth_retry=0;
719 int seekable=0;
720 char *content_type;
721 const char *content_length;
722 char *next_url;
723 URL_t *url = stream->streaming_ctrl->url;
727 redirect = 0;
728 if (fd > 0) closesocket(fd);
729 fd = http_send_request( url, 0 );
730 if( fd<0 ) {
731 goto err_out;
734 http_free(http_hdr);
735 http_hdr = http_read_response( fd );
736 if( http_hdr==NULL ) {
737 goto err_out;
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");
748 if (accept_ranges)
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");
754 seekable = 1;
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 ) {
764 case 200: { // OK
765 char *field_data;
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;
773 else
774 *file_format = DEMUXER_TYPE_AUDIO;
775 res = STREAM_ERROR;
776 goto out;
778 case 400: // Server Full
779 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n");
780 goto err_out;
781 case 401: // Service Unavailable
782 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n");
783 goto err_out;
784 case 403: // Service Forbidden
785 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n");
786 goto err_out;
787 case 404: // Resource Not Found
788 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n");
789 goto err_out;
790 default:
791 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
792 goto err_out;
796 // Assume standard http if not ICY
797 switch( http_hdr->status_code ) {
798 case 200: // OK
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 ) {
807 unsigned int i;
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;
814 res = seekable;
815 goto out;
819 // Not found in the mime type table, don't fail,
820 // we should try raw HTTP
821 res = seekable;
822 goto out;
823 // Redirect
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;
835 goto err_out;
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);
839 goto err_out;
841 if (is_ultravox) {
842 free(url->protocol);
843 url->protocol = strdup("unsv");
845 redirect = 1;
847 break;
848 case 401: // Authentication required
849 if( http_authenticate(http_hdr, url, &auth_retry)<0 )
850 goto err_out;
851 redirect = 1;
852 break;
853 default:
854 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
855 goto err_out;
857 } while( redirect );
859 err_out:
860 if (fd > 0) closesocket( fd );
861 fd = -1;
862 http_free( http_hdr );
863 http_hdr = NULL;
864 out:
865 stream->streaming_ctrl->data = (void*)http_hdr;
866 stream->fd = fd;
867 return res;
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");
885 if (stream->fd >= 0)
886 closesocket(stream->fd);
887 stream->fd = -1;
888 streaming_ctrl_free(stream->streaming_ctrl);
889 stream->streaming_ctrl = NULL;
890 return STREAM_UNSUPPORTED;
893 fixup_network_stream_cache(stream);
894 return STREAM_OK;
897 static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) {
898 int seekable=0;
899 URL_t *url;
901 stream->streaming_ctrl = streaming_ctrl_new();
902 if( stream->streaming_ctrl==NULL ) {
903 return STREAM_ERROR;
905 stream->streaming_ctrl->bandwidth = network_bandwidth;
906 url = url_new(stream->url);
907 stream->streaming_ctrl->url = check4proxies(url);
908 url_free(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)) {
913 if (stream->fd >= 0)
914 closesocket(stream->fd);
915 stream->fd = -1;
916 if (seekable == STREAM_REDIRECTED)
917 return seekable;
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) {
927 int seekable=0;
928 URL_t *url;
930 stream->streaming_ctrl = streaming_ctrl_new();
931 if( stream->streaming_ctrl==NULL ) {
932 return STREAM_ERROR;
934 stream->streaming_ctrl->bandwidth = network_bandwidth;
935 url = url_new(stream->url);
936 stream->streaming_ctrl->url = check4proxies(url);
937 url_free(url);
939 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(2), URL: %s\n", stream->url);
940 seekable = http_streaming_start(stream, file_format);
941 if(seekable < 0) {
942 if (stream->fd >= 0)
943 closesocket(stream->fd);
944 stream->fd = -1;
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 = {
955 "http streaming",
956 "null",
957 "Bertrand, Albeau, Reimar Doeffinger, Arpi?",
958 "plain http",
959 open_s1,
960 {"http", "http_proxy", "unsv", "icyx", "noicyx", NULL},
961 NULL,
962 0 // Urls are an option string
965 const stream_info_t stream_info_http2 = {
966 "http streaming",
967 "null",
968 "Bertrand, Albeu, Arpi? who?",
969 "plain http, also used as fallback for many other protocols",
970 open_s2,
971 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback
972 NULL,
973 0 // Urls are an option string