cleanup: Silence compilation warnings on MinGW-w64
[mplayer.git] / stream / http.c
blob349a6c483dc3bd5a96ec914ab4469ca1d02a5cc4
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>
48 extern int stream_cache_size;
49 extern int network_bandwidth;
51 typedef struct {
52 unsigned metaint;
53 unsigned metapos;
54 int is_ultravox;
55 } scast_data_t;
57 /**
58 * \brief first read any data from sc->buffer then from fd
59 * \param fd file descriptor to read data from
60 * \param buffer buffer to read into
61 * \param len how many bytes to read
62 * \param sc streaming control containing buffer to read from first
63 * \return len unless there is a read error or eof
65 static unsigned my_read(int fd, char *buffer, int len, streaming_ctrl_t *sc) {
66 unsigned pos = 0;
67 unsigned cp_len = sc->buffer_size - sc->buffer_pos;
68 if (cp_len > len)
69 cp_len = len;
70 memcpy(buffer, &sc->buffer[sc->buffer_pos], cp_len);
71 sc->buffer_pos += cp_len;
72 pos += cp_len;
73 while (pos < len) {
74 int ret = recv(fd, &buffer[pos], len - pos, 0);
75 if (ret <= 0)
76 break;
77 pos += ret;
79 return pos;
82 /**
83 * \brief read and process (i.e. discard *g*) a block of ultravox metadata
84 * \param fd file descriptor to read from
85 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
86 * \return number of real data before next metadata block starts or 0 on error
88 static unsigned uvox_meta_read(int fd, streaming_ctrl_t *sc) {
89 unsigned metaint;
90 unsigned char info[6] = {0, 0, 0, 0, 0, 0};
91 int info_read;
92 do {
93 info_read = my_read(fd, info, 1, sc);
94 if (info[0] == 0x00)
95 info_read = my_read(fd, info, 6, sc);
96 else
97 info_read += my_read(fd, &info[1], 5, sc);
98 if (info_read != 6) // read error or eof
99 return 0;
100 // sync byte and reserved flags
101 if (info[0] != 0x5a || (info[1] & 0xfc) != 0x00) {
102 mp_msg(MSGT_DEMUXER, MSGL_ERR, "Invalid or unknown uvox metadata\n");
103 return 0;
105 if (info[1] & 0x01)
106 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Encrypted ultravox data\n");
107 metaint = info[4] << 8 | info[5];
108 if ((info[3] & 0xf) < 0x07) { // discard any metadata nonsense
109 char *metabuf = malloc(metaint);
110 my_read(fd, metabuf, metaint, sc);
111 free(metabuf);
113 } while ((info[3] & 0xf) < 0x07);
114 return metaint;
118 * \brief read one scast meta data entry and print it
119 * \param fd file descriptor to read from
120 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
122 static void scast_meta_read(int fd, streaming_ctrl_t *sc) {
123 unsigned char tmp = 0;
124 unsigned metalen;
125 my_read(fd, &tmp, 1, sc);
126 metalen = tmp * 16;
127 if (metalen > 0) {
128 int i;
129 uint8_t *info = malloc(metalen + 1);
130 unsigned nlen = my_read(fd, info, metalen, sc);
131 // avoid breaking the user's terminal too much
132 if (nlen > 256) nlen = 256;
133 for (i = 0; i < nlen; i++)
134 if (info[i] && info[i] < 32) info[i] = '?';
135 info[nlen] = 0;
136 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\nICY Info: %s\n", info);
137 free(info);
142 * \brief read data from scast/ultravox stream without any metadata
143 * \param fd file descriptor to read from
144 * \param buffer buffer to read data into
145 * \param size number of bytes to read
146 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
148 static int scast_streaming_read(int fd, char *buffer, int size,
149 streaming_ctrl_t *sc) {
150 scast_data_t *sd = (scast_data_t *)sc->data;
151 unsigned block, ret;
152 unsigned done = 0;
154 // first read remaining data up to next metadata
155 block = sd->metaint - sd->metapos;
156 if (block > size)
157 block = size;
158 ret = my_read(fd, buffer, block, sc);
159 sd->metapos += ret;
160 done += ret;
161 if (ret != block) // read problems or eof
162 size = done;
164 while (done < size) { // now comes the metadata
165 if (sd->is_ultravox)
167 sd->metaint = uvox_meta_read(fd, sc);
168 if (!sd->metaint)
169 size = done;
171 else
172 scast_meta_read(fd, sc); // read and display metadata
173 sd->metapos = 0;
174 block = size - done;
175 if (block > sd->metaint)
176 block = sd->metaint;
177 ret = my_read(fd, &buffer[done], block, sc);
178 sd->metapos += ret;
179 done += ret;
180 if (ret != block) // read problems or eof
181 size = done;
183 return done;
186 static int scast_streaming_start(stream_t *stream) {
187 int metaint;
188 scast_data_t *scast_data;
189 HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
190 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
191 if (!stream || stream->fd < 0 || !http_hdr)
192 return -1;
193 if (is_ultravox)
194 metaint = 0;
195 else {
196 metaint = atoi(http_get_field(http_hdr, "Icy-MetaInt"));
197 if (metaint <= 0)
198 return -1;
200 stream->streaming_ctrl->buffer = malloc(http_hdr->body_size);
201 stream->streaming_ctrl->buffer_size = http_hdr->body_size;
202 stream->streaming_ctrl->buffer_pos = 0;
203 memcpy(stream->streaming_ctrl->buffer, http_hdr->body, http_hdr->body_size);
204 scast_data = malloc(sizeof(scast_data_t));
205 scast_data->metaint = metaint;
206 scast_data->metapos = 0;
207 scast_data->is_ultravox = is_ultravox;
208 http_free(http_hdr);
209 stream->streaming_ctrl->data = scast_data;
210 stream->streaming_ctrl->streaming_read = scast_streaming_read;
211 stream->streaming_ctrl->streaming_seek = NULL;
212 stream->streaming_ctrl->prebuffer_size = 64 * 1024; // 64 KBytes
213 stream->streaming_ctrl->buffering = 1;
214 stream->streaming_ctrl->status = streaming_playing_e;
215 return 0;
218 static int nop_streaming_start( stream_t *stream ) {
219 HTTP_header_t *http_hdr = NULL;
220 char *next_url=NULL;
221 URL_t *rd_url=NULL;
222 int fd,ret;
223 if( stream==NULL ) return -1;
225 fd = stream->fd;
226 if( fd<0 ) {
227 fd = http_send_request( stream->streaming_ctrl->url, 0 );
228 if( fd<0 ) return -1;
229 http_hdr = http_read_response( fd );
230 if( http_hdr==NULL ) return -1;
232 switch( http_hdr->status_code ) {
233 case 200: // OK
234 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
235 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
236 if( http_hdr->body_size>0 ) {
237 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
238 http_free( http_hdr );
239 return -1;
242 break;
243 // Redirect
244 case 301: // Permanently
245 case 302: // Temporarily
246 case 303: // See Other
247 case 307: // Temporarily (since HTTP/1.1)
248 ret=-1;
249 next_url = http_get_field( http_hdr, "Location" );
251 if (next_url != NULL)
252 rd_url=url_new(next_url);
254 if (next_url != NULL && rd_url != NULL) {
255 mp_msg(MSGT_NETWORK,MSGL_STATUS,"Redirected: Using this url instead %s\n",next_url);
256 stream->streaming_ctrl->url=check4proxies(rd_url);
257 ret=nop_streaming_start(stream); //recursively get streaming started
258 } else {
259 mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n");
260 closesocket( fd );
261 fd = -1;
263 return ret;
264 break;
265 case 401: //Authorization required
266 case 403: //Forbidden
267 case 404: //Not found
268 case 500: //Server Error
269 default:
270 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
271 closesocket( fd );
272 fd = -1;
273 return -1;
274 break;
276 stream->fd = fd;
277 } else {
278 http_hdr = (HTTP_header_t*)stream->streaming_ctrl->data;
279 if( http_hdr->body_size>0 ) {
280 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
281 http_free( http_hdr );
282 stream->streaming_ctrl->data = NULL;
283 return -1;
288 if( http_hdr ) {
289 http_free( http_hdr );
290 stream->streaming_ctrl->data = NULL;
293 stream->streaming_ctrl->streaming_read = nop_streaming_read;
294 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
295 stream->streaming_ctrl->prebuffer_size = 64*1024; // 64 KBytes
296 stream->streaming_ctrl->buffering = 1;
297 stream->streaming_ctrl->status = streaming_playing_e;
298 return 0;
301 HTTP_header_t *
302 http_new_header(void) {
303 HTTP_header_t *http_hdr;
305 http_hdr = calloc(1, sizeof(*http_hdr));
306 if( http_hdr==NULL ) return NULL;
308 return http_hdr;
311 void
312 http_free( HTTP_header_t *http_hdr ) {
313 HTTP_field_t *field, *field2free;
314 if( http_hdr==NULL ) return;
315 free(http_hdr->protocol);
316 free(http_hdr->uri);
317 free(http_hdr->reason_phrase);
318 free(http_hdr->field_search);
319 free(http_hdr->method);
320 free(http_hdr->buffer);
321 field = http_hdr->first_field;
322 while( field!=NULL ) {
323 field2free = field;
324 free(field->field_name);
325 field = field->next;
326 free( field2free );
328 free( http_hdr );
329 http_hdr = NULL;
333 http_response_append( HTTP_header_t *http_hdr, char *response, int length ) {
334 if( http_hdr==NULL || response==NULL || length<0 ) return -1;
336 if( (unsigned)length > SIZE_MAX - http_hdr->buffer_size - 1) {
337 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Bad size in memory (re)allocation\n");
338 return -1;
340 http_hdr->buffer = realloc( http_hdr->buffer, http_hdr->buffer_size+length+1 );
341 if( http_hdr->buffer==NULL ) {
342 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory (re)allocation failed\n");
343 return -1;
345 memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length );
346 http_hdr->buffer_size += length;
347 http_hdr->buffer[http_hdr->buffer_size]=0; // close the string!
348 return http_hdr->buffer_size;
352 http_is_header_entire( HTTP_header_t *http_hdr ) {
353 if( http_hdr==NULL ) return -1;
354 if( http_hdr->buffer==NULL ) return 0; // empty
356 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL &&
357 strstr(http_hdr->buffer, "\n\n")==NULL ) return 0;
358 return 1;
362 http_response_parse( HTTP_header_t *http_hdr ) {
363 char *hdr_ptr, *ptr;
364 char *field=NULL;
365 int pos_hdr_sep, hdr_sep_len;
366 size_t len;
367 if( http_hdr==NULL ) return -1;
368 if( http_hdr->is_parsed ) return 0;
370 // Get the protocol
371 hdr_ptr = strstr( http_hdr->buffer, " " );
372 if( hdr_ptr==NULL ) {
373 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. No space separator found.\n");
374 return -1;
376 len = hdr_ptr-http_hdr->buffer;
377 http_hdr->protocol = malloc(len+1);
378 if( http_hdr->protocol==NULL ) {
379 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
380 return -1;
382 strncpy( http_hdr->protocol, http_hdr->buffer, len );
383 http_hdr->protocol[len]='\0';
384 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) {
385 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) {
386 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get HTTP minor version.\n");
387 return -1;
391 // Get the status code
392 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) {
393 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get status code.\n");
394 return -1;
396 hdr_ptr += 4;
398 // Get the reason phrase
399 ptr = strstr( hdr_ptr, "\n" );
400 if( hdr_ptr==NULL ) {
401 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get the reason phrase.\n");
402 return -1;
404 len = ptr-hdr_ptr;
405 http_hdr->reason_phrase = malloc(len+1);
406 if( http_hdr->reason_phrase==NULL ) {
407 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
408 return -1;
410 strncpy( http_hdr->reason_phrase, hdr_ptr, len );
411 if( http_hdr->reason_phrase[len-1]=='\r' ) {
412 len--;
414 http_hdr->reason_phrase[len]='\0';
416 // Set the position of the header separator: \r\n\r\n
417 hdr_sep_len = 4;
418 ptr = strstr( http_hdr->buffer, "\r\n\r\n" );
419 if( ptr==NULL ) {
420 ptr = strstr( http_hdr->buffer, "\n\n" );
421 if( ptr==NULL ) {
422 mp_msg(MSGT_NETWORK,MSGL_ERR,"Header may be incomplete. No CRLF CRLF found.\n");
423 return -1;
425 hdr_sep_len = 2;
427 pos_hdr_sep = ptr-http_hdr->buffer;
429 // Point to the first line after the method line.
430 hdr_ptr = strstr( http_hdr->buffer, "\n" )+1;
431 do {
432 ptr = hdr_ptr;
433 while( *ptr!='\r' && *ptr!='\n' ) ptr++;
434 len = ptr-hdr_ptr;
435 if( len==0 ) break;
436 field = realloc(field, len+1);
437 if( field==NULL ) {
438 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
439 return -1;
441 strncpy( field, hdr_ptr, len );
442 field[len]='\0';
443 http_set_field( http_hdr, field );
444 hdr_ptr = ptr+((*ptr=='\r')?2:1);
445 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) );
447 free(field);
449 if( pos_hdr_sep+hdr_sep_len<http_hdr->buffer_size ) {
450 // Response has data!
451 http_hdr->body = http_hdr->buffer+pos_hdr_sep+hdr_sep_len;
452 http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+hdr_sep_len);
455 http_hdr->is_parsed = 1;
456 return 0;
459 char *
460 http_build_request( HTTP_header_t *http_hdr ) {
461 char *ptr, *uri=NULL;
462 int len;
463 HTTP_field_t *field;
464 if( http_hdr==NULL ) return NULL;
466 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET");
467 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/");
468 else {
469 uri = malloc(strlen(http_hdr->uri) + 1);
470 if( uri==NULL ) {
471 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
472 return NULL;
474 strcpy(uri,http_hdr->uri);
477 //**** Compute the request length
478 // Add the Method line
479 len = strlen(http_hdr->method)+strlen(uri)+12;
480 // Add the fields
481 field = http_hdr->first_field;
482 while( field!=NULL ) {
483 len += strlen(field->field_name)+2;
484 field = field->next;
486 // Add the CRLF
487 len += 2;
488 // Add the body
489 if( http_hdr->body!=NULL ) {
490 len += http_hdr->body_size;
492 // Free the buffer if it was previously used
493 if( http_hdr->buffer!=NULL ) {
494 free( http_hdr->buffer );
495 http_hdr->buffer = NULL;
497 http_hdr->buffer = malloc(len+1);
498 if( http_hdr->buffer==NULL ) {
499 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
500 return NULL;
502 http_hdr->buffer_size = len;
504 //*** Building the request
505 ptr = http_hdr->buffer;
506 // Add the method line
507 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version );
508 field = http_hdr->first_field;
509 // Add the field
510 while( field!=NULL ) {
511 ptr += sprintf( ptr, "%s\r\n", field->field_name );
512 field = field->next;
514 ptr += sprintf( ptr, "\r\n" );
515 // Add the body
516 if( http_hdr->body!=NULL ) {
517 memcpy( ptr, http_hdr->body, http_hdr->body_size );
520 free(uri);
521 return http_hdr->buffer;
524 char *
525 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) {
526 if( http_hdr==NULL || field_name==NULL ) return NULL;
527 http_hdr->field_search_pos = http_hdr->first_field;
528 http_hdr->field_search = realloc( http_hdr->field_search, strlen(field_name)+1 );
529 if( http_hdr->field_search==NULL ) {
530 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
531 return NULL;
533 strcpy( http_hdr->field_search, field_name );
534 return http_get_next_field( http_hdr );
537 char *
538 http_get_next_field( HTTP_header_t *http_hdr ) {
539 char *ptr;
540 HTTP_field_t *field;
541 if( http_hdr==NULL ) return NULL;
543 field = http_hdr->field_search_pos;
544 while( field!=NULL ) {
545 ptr = strstr( field->field_name, ":" );
546 if( ptr==NULL ) return NULL;
547 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) {
548 ptr++; // Skip the column
549 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some
550 http_hdr->field_search_pos = field->next;
551 return ptr; // return the value without the field name
553 field = field->next;
555 return NULL;
558 void
559 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) {
560 HTTP_field_t *new_field;
561 if( http_hdr==NULL || field_name==NULL ) return;
563 new_field = malloc(sizeof(HTTP_field_t));
564 if( new_field==NULL ) {
565 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
566 return;
568 new_field->next = NULL;
569 new_field->field_name = malloc(strlen(field_name)+1);
570 if( new_field->field_name==NULL ) {
571 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
572 free(new_field);
573 return;
575 strcpy( new_field->field_name, field_name );
577 if( http_hdr->last_field==NULL ) {
578 http_hdr->first_field = new_field;
579 } else {
580 http_hdr->last_field->next = new_field;
582 http_hdr->last_field = new_field;
583 http_hdr->field_nb++;
586 void
587 http_set_method( HTTP_header_t *http_hdr, const char *method ) {
588 if( http_hdr==NULL || method==NULL ) return;
590 http_hdr->method = malloc(strlen(method)+1);
591 if( http_hdr->method==NULL ) {
592 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
593 return;
595 strcpy( http_hdr->method, method );
598 void
599 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) {
600 if( http_hdr==NULL || uri==NULL ) return;
602 http_hdr->uri = malloc(strlen(uri)+1);
603 if( http_hdr->uri==NULL ) {
604 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
605 return;
607 strcpy( http_hdr->uri, uri );
610 static int
611 http_add_authentication( HTTP_header_t *http_hdr, const char *username, const char *password, const char *auth_str ) {
612 char *auth = NULL, *usr_pass = NULL, *b64_usr_pass = NULL;
613 int encoded_len, pass_len=0;
614 size_t auth_len, usr_pass_len;
615 int res = -1;
616 if( http_hdr==NULL || username==NULL ) return -1;
618 if( password!=NULL ) {
619 pass_len = strlen(password);
622 usr_pass_len = strlen(username) + 1 + pass_len;
623 usr_pass = malloc(usr_pass_len + 1);
624 if( usr_pass==NULL ) {
625 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
626 goto out;
629 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password );
631 encoded_len = AV_BASE64_SIZE(usr_pass_len);
632 b64_usr_pass = malloc(encoded_len);
633 if( b64_usr_pass==NULL ) {
634 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
635 goto out;
637 av_base64_encode(b64_usr_pass, encoded_len, usr_pass, usr_pass_len);
639 auth_len = encoded_len + 100;
640 auth = malloc(auth_len);
641 if( auth==NULL ) {
642 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
643 goto out;
646 snprintf(auth, auth_len, "%s: Basic %s", auth_str, b64_usr_pass);
647 http_set_field( http_hdr, auth );
648 res = 0;
650 out:
651 free( usr_pass );
652 free( b64_usr_pass );
653 free( auth );
655 return res;
659 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) {
660 return http_add_authentication(http_hdr, username, password, "Authorization");
664 http_add_basic_proxy_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) {
665 return http_add_authentication(http_hdr, username, password, "Proxy-Authorization");
668 void
669 http_debug_hdr( HTTP_header_t *http_hdr ) {
670 HTTP_field_t *field;
671 int i = 0;
672 if( http_hdr==NULL ) return;
674 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n");
675 mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol );
676 mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version );
677 mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri );
678 mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method );
679 mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code );
680 mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase );
681 mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%zd]\n", http_hdr->body_size );
683 mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n");
684 field = http_hdr->first_field;
685 while( field!=NULL ) {
686 mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name );
687 field = field->next;
689 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n");
692 static void print_icy_metadata(HTTP_header_t *http_hdr) {
693 const char *field_data;
694 // note: I skip icy-notice1 and 2, as they contain html <BR>
695 // and are IMHO useless info ::atmos
696 if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL )
697 mp_msg(MSGT_NETWORK,MSGL_INFO,"Name : %s\n", field_data);
698 if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL )
699 mp_msg(MSGT_NETWORK,MSGL_INFO,"Genre : %s\n", field_data);
700 if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL )
701 mp_msg(MSGT_NETWORK,MSGL_INFO,"Website: %s\n", field_data);
702 // XXX: does this really mean public server? ::atmos
703 if( (field_data = http_get_field(http_hdr, "icy-pub")) != NULL )
704 mp_msg(MSGT_NETWORK,MSGL_INFO,"Public : %s\n", atoi(field_data)?"yes":"no");
705 if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL )
706 mp_msg(MSGT_NETWORK,MSGL_INFO,"Bitrate: %skbit/s\n", field_data);
709 //! If this function succeeds you must closesocket stream->fd
710 static int http_streaming_start(stream_t *stream, int* file_format) {
711 HTTP_header_t *http_hdr = NULL;
712 int fd = stream->fd;
713 int res = STREAM_UNSUPPORTED;
714 int redirect = 0;
715 int auth_retry=0;
716 int seekable=0;
717 char *content_type;
718 const char *content_length;
719 char *next_url;
720 URL_t *url = stream->streaming_ctrl->url;
724 redirect = 0;
725 if (fd > 0) closesocket(fd);
726 fd = http_send_request( url, 0 );
727 if( fd<0 ) {
728 goto err_out;
731 http_free(http_hdr);
732 http_hdr = http_read_response( fd );
733 if( http_hdr==NULL ) {
734 goto err_out;
737 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
738 http_debug_hdr( http_hdr );
741 // Check if we can make partial content requests and thus seek in http-streams
742 if( http_hdr!=NULL && http_hdr->status_code==200 ) {
743 const char *accept_ranges = http_get_field(http_hdr,"Accept-Ranges");
744 const char *server = http_get_field(http_hdr, "Server");
745 if (accept_ranges)
746 seekable = strncmp(accept_ranges,"bytes",5)==0;
747 else if (server && (strcmp(server, "gvs 1.0") == 0 ||
748 strncmp(server, "MakeMKV", 7) == 0)) {
749 // HACK for youtube and MakeMKV incorrectly claiming not to support seeking
750 mp_msg(MSGT_NETWORK, MSGL_WARN, "Broken webserver, incorrectly claims to not support Accept-Ranges\n");
751 seekable = 1;
755 print_icy_metadata(http_hdr);
757 // Check if the response is an ICY status_code reason_phrase
758 if( !strcasecmp(http_hdr->protocol, "ICY") ||
759 http_get_field(http_hdr, "Icy-MetaInt") ) {
760 switch( http_hdr->status_code ) {
761 case 200: { // OK
762 char *field_data;
763 // If content-type == video/nsv we most likely have a winamp video stream
764 // otherwise it should be mp3. if there are more types consider adding mime type
765 // handling like later
766 if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "video/nsv") || !strcmp(field_data, "misc/ultravox")))
767 *file_format = DEMUXER_TYPE_NSV;
768 else if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "audio/aacp") || !strcmp(field_data, "audio/aac")))
769 *file_format = DEMUXER_TYPE_AAC;
770 else
771 *file_format = DEMUXER_TYPE_AUDIO;
772 res = STREAM_ERROR;
773 goto out;
775 case 400: // Server Full
776 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n");
777 goto err_out;
778 case 401: // Service Unavailable
779 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n");
780 goto err_out;
781 case 403: // Service Forbidden
782 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n");
783 goto err_out;
784 case 404: // Resource Not Found
785 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n");
786 goto err_out;
787 default:
788 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
789 goto err_out;
793 // Assume standard http if not ICY
794 switch( http_hdr->status_code ) {
795 case 200: // OK
796 content_length = http_get_field(http_hdr, "Content-Length");
797 if (content_length) {
798 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", content_length);
799 stream->end_pos = atoll(content_length);
801 // Look if we can use the Content-Type
802 content_type = http_get_field( http_hdr, "Content-Type" );
803 if( content_type!=NULL ) {
804 unsigned int i;
806 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type );
807 // Check in the mime type table for a demuxer type
808 for (i = 0; mime_type_table[i].mime_type != NULL; i++) {
809 if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) {
810 *file_format = mime_type_table[i].demuxer_type;
811 res = seekable;
812 goto out;
816 // Not found in the mime type table, don't fail,
817 // we should try raw HTTP
818 res = seekable;
819 goto out;
820 // Redirect
821 case 301: // Permanently
822 case 302: // Temporarily
823 case 303: // See Other
824 case 307: // Temporarily (since HTTP/1.1)
825 // TODO: RFC 2616, recommand to detect infinite redirection loops
826 next_url = http_get_field( http_hdr, "Location" );
827 if( next_url!=NULL ) {
828 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
829 stream->streaming_ctrl->url = url_redirect( &url, next_url );
830 if (!strcasecmp(url->protocol, "mms")) {
831 res = STREAM_REDIRECTED;
832 goto err_out;
834 if (strcasecmp(url->protocol, "http")) {
835 mp_msg(MSGT_NETWORK,MSGL_ERR,"Unsupported http %d redirect to %s protocol\n", http_hdr->status_code, url->protocol);
836 goto err_out;
838 if (is_ultravox) {
839 free(url->protocol);
840 url->protocol = strdup("unsv");
842 redirect = 1;
844 break;
845 case 401: // Authentication required
846 if( http_authenticate(http_hdr, url, &auth_retry)<0 )
847 goto err_out;
848 redirect = 1;
849 break;
850 default:
851 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
852 goto err_out;
854 } while( redirect );
856 err_out:
857 if (fd > 0) closesocket( fd );
858 fd = -1;
859 http_free( http_hdr );
860 http_hdr = NULL;
861 out:
862 stream->streaming_ctrl->data = (void*)http_hdr;
863 stream->fd = fd;
864 return res;
867 static int fixup_open(stream_t *stream,int seekable) {
868 HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
869 int is_icy = http_hdr && http_get_field(http_hdr, "Icy-MetaInt");
870 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
872 stream->type = STREAMTYPE_STREAM;
873 if(!is_icy && !is_ultravox && seekable)
875 stream->flags |= MP_STREAM_SEEK;
876 stream->seek = http_seek;
878 stream->streaming_ctrl->bandwidth = network_bandwidth;
879 if ((!is_icy && !is_ultravox) || scast_streaming_start(stream))
880 if(nop_streaming_start( stream )) {
881 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_start failed\n");
882 if (stream->fd >= 0)
883 closesocket(stream->fd);
884 stream->fd = -1;
885 streaming_ctrl_free(stream->streaming_ctrl);
886 stream->streaming_ctrl = NULL;
887 return STREAM_UNSUPPORTED;
890 fixup_network_stream_cache(stream);
891 return STREAM_OK;
894 static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) {
895 int seekable=0;
896 URL_t *url;
898 stream->streaming_ctrl = streaming_ctrl_new();
899 if( stream->streaming_ctrl==NULL ) {
900 return STREAM_ERROR;
902 stream->streaming_ctrl->bandwidth = network_bandwidth;
903 url = url_new(stream->url);
904 stream->streaming_ctrl->url = check4proxies(url);
905 url_free(url);
907 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(1), URL: %s\n", stream->url);
908 seekable = http_streaming_start(stream, file_format);
909 if((seekable < 0) || (*file_format == DEMUXER_TYPE_ASF)) {
910 if (stream->fd >= 0)
911 closesocket(stream->fd);
912 stream->fd = -1;
913 if (seekable == STREAM_REDIRECTED)
914 return seekable;
915 streaming_ctrl_free(stream->streaming_ctrl);
916 stream->streaming_ctrl = NULL;
917 return STREAM_UNSUPPORTED;
920 return fixup_open(stream, seekable);
923 static int open_s2(stream_t *stream,int mode, void* opts, int* file_format) {
924 int seekable=0;
925 URL_t *url;
927 stream->streaming_ctrl = streaming_ctrl_new();
928 if( stream->streaming_ctrl==NULL ) {
929 return STREAM_ERROR;
931 stream->streaming_ctrl->bandwidth = network_bandwidth;
932 url = url_new(stream->url);
933 stream->streaming_ctrl->url = check4proxies(url);
934 url_free(url);
936 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(2), URL: %s\n", stream->url);
937 seekable = http_streaming_start(stream, file_format);
938 if(seekable < 0) {
939 if (stream->fd >= 0)
940 closesocket(stream->fd);
941 stream->fd = -1;
942 streaming_ctrl_free(stream->streaming_ctrl);
943 stream->streaming_ctrl = NULL;
944 return STREAM_UNSUPPORTED;
947 return fixup_open(stream, seekable);
951 const stream_info_t stream_info_http1 = {
952 "http streaming",
953 "null",
954 "Bertrand, Albeau, Reimar Doeffinger, Arpi?",
955 "plain http",
956 open_s1,
957 {"http", "http_proxy", "unsv", "icyx", "noicyx", NULL},
958 NULL,
959 0 // Urls are an option string
962 const stream_info_t stream_info_http2 = {
963 "http streaming",
964 "null",
965 "Bertrand, Albeu, Arpi? who?",
966 "plain http, also used as fallback for many other protocols",
967 open_s2,
968 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback
969 NULL,
970 0 // Urls are an option string