Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / stream / http.c
blobac484f84d9ed23de46cbfb713a3cc5ede667b8f7
1 /*
2 * HTTP Helper
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com>
4 * (C) 2001, MPlayer team.
5 */
7 #include "config.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
14 #ifndef HAVE_WINSOCK2
15 #define closesocket close
16 #else
17 #include <winsock2.h>
18 #include <ws2tcpip.h>
19 #endif
21 #include "http.h"
22 #include "url.h"
23 #include "mp_msg.h"
25 #include "stream.h"
26 #include "libmpdemux/demuxer.h"
27 #include "network.h"
28 #include "help_mp.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);
37 typedef struct {
38 unsigned metaint;
39 unsigned metapos;
40 int is_ultravox;
41 } scast_data_t;
43 /**
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) {
52 unsigned pos = 0;
53 unsigned cp_len = sc->buffer_size - sc->buffer_pos;
54 if (cp_len > len)
55 cp_len = len;
56 memcpy(buffer, &sc->buffer[sc->buffer_pos], cp_len);
57 sc->buffer_pos += cp_len;
58 pos += cp_len;
59 while (pos < len) {
60 int ret = recv(fd, &buffer[pos], len - pos, 0);
61 if (ret <= 0)
62 break;
63 pos += ret;
65 return pos;
68 /**
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) {
75 unsigned metaint;
76 unsigned char info[6] = {0, 0, 0, 0, 0, 0};
77 int info_read;
78 do {
79 info_read = my_read(fd, info, 1, sc);
80 if (info[0] == 0x00)
81 info_read = my_read(fd, info, 6, sc);
82 else
83 info_read += my_read(fd, &info[1], 5, sc);
84 if (info_read != 6) // read error or eof
85 return 0;
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");
89 return 0;
91 if (info[1] & 0x01)
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);
97 free(metabuf);
99 } while ((info[3] & 0xf) < 0x07);
100 return metaint;
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;
110 unsigned metalen;
111 my_read(fd, &tmp, 1, sc);
112 metalen = tmp * 16;
113 if (metalen > 0) {
114 char *info = malloc(metalen + 1);
115 unsigned nlen = my_read(fd, info, metalen, sc);
116 info[nlen] = 0;
117 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\nICY Info: %s\n", info);
118 free(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;
132 unsigned block, ret;
133 unsigned done = 0;
135 // first read remaining data up to next metadata
136 block = sd->metaint - sd->metapos;
137 if (block > size)
138 block = size;
139 ret = my_read(fd, buffer, block, sc);
140 sd->metapos += ret;
141 done += ret;
142 if (ret != block) // read problems or eof
143 size = done;
145 while (done < size) { // now comes the metadata
146 if (sd->is_ultravox)
148 sd->metaint = uvox_meta_read(fd, sc);
149 if (!sd->metaint)
150 size = done;
152 else
153 scast_meta_read(fd, sc); // read and display metadata
154 sd->metapos = 0;
155 block = size - done;
156 if (block > sd->metaint)
157 block = sd->metaint;
158 ret = my_read(fd, &buffer[done], block, sc);
159 sd->metapos += ret;
160 done += ret;
161 if (ret != block) // read problems or eof
162 size = done;
164 return done;
167 static int scast_streaming_start(stream_t *stream) {
168 int metaint;
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)
173 return -1;
174 if (is_ultravox)
175 metaint = 0;
176 else {
177 metaint = atoi(http_get_field(http_hdr, "Icy-MetaInt"));
178 if (metaint <= 0)
179 return -1;
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;
189 http_free(http_hdr);
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;
196 return 0;
199 static int nop_streaming_start( stream_t *stream ) {
200 HTTP_header_t *http_hdr = NULL;
201 char *next_url=NULL;
202 URL_t *rd_url=NULL;
203 int fd,ret;
204 if( stream==NULL ) return -1;
206 fd = stream->fd;
207 if( fd<0 ) {
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 ) {
214 case 200: // OK
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 );
220 return -1;
223 break;
224 // Redirect
225 case 301: // Permanently
226 case 302: // Temporarily
227 case 303: // See Other
228 ret=-1;
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
238 } else {
239 mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n");
240 closesocket( fd );
241 fd = -1;
243 return ret;
244 break;
245 case 401: //Authorization required
246 case 403: //Forbidden
247 case 404: //Not found
248 case 500: //Server Error
249 default:
250 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
251 closesocket( fd );
252 fd = -1;
253 return -1;
254 break;
256 stream->fd = fd;
257 } else {
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;
263 return -1;
268 if( http_hdr ) {
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;
278 return 0;
281 HTTP_header_t *
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) );
289 return http_hdr;
292 void
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 ) {
304 field2free = field;
305 if (field->field_name)
306 free(field->field_name);
307 field = field->next;
308 free( field2free );
310 free( http_hdr );
311 http_hdr = NULL;
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");
320 return -1;
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");
325 return -1;
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;
340 return 1;
344 http_response_parse( HTTP_header_t *http_hdr ) {
345 char *hdr_ptr, *ptr;
346 char *field=NULL;
347 int pos_hdr_sep, hdr_sep_len;
348 size_t len;
349 if( http_hdr==NULL ) return -1;
350 if( http_hdr->is_parsed ) return 0;
352 // Get the protocol
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");
356 return -1;
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");
362 return -1;
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");
369 return -1;
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");
376 return -1;
378 hdr_ptr += 4;
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");
384 return -1;
386 len = ptr-hdr_ptr;
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");
390 return -1;
392 strncpy( http_hdr->reason_phrase, hdr_ptr, len );
393 if( http_hdr->reason_phrase[len-1]=='\r' ) {
394 len--;
396 http_hdr->reason_phrase[len]='\0';
398 // Set the position of the header separator: \r\n\r\n
399 hdr_sep_len = 4;
400 ptr = strstr( http_hdr->buffer, "\r\n\r\n" );
401 if( ptr==NULL ) {
402 ptr = strstr( http_hdr->buffer, "\n\n" );
403 if( ptr==NULL ) {
404 mp_msg(MSGT_NETWORK,MSGL_ERR,"Header may be incomplete. No CRLF CRLF found.\n");
405 return -1;
407 hdr_sep_len = 2;
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;
413 do {
414 ptr = hdr_ptr;
415 while( *ptr!='\r' && *ptr!='\n' ) ptr++;
416 len = ptr-hdr_ptr;
417 if( len==0 ) break;
418 field = (char*)realloc(field, len+1);
419 if( field==NULL ) {
420 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
421 return -1;
423 strncpy( field, hdr_ptr, len );
424 field[len]='\0';
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;
438 return 0;
441 char *
442 http_build_request( HTTP_header_t *http_hdr ) {
443 char *ptr, *uri=NULL;
444 int len;
445 HTTP_field_t *field;
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, "/");
450 else {
451 uri = malloc(strlen(http_hdr->uri) + 1);
452 if( uri==NULL ) {
453 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
454 return NULL;
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;
462 // Add the fields
463 field = http_hdr->first_field;
464 while( field!=NULL ) {
465 len += strlen(field->field_name)+2;
466 field = field->next;
468 // Add the CRLF
469 len += 2;
470 // Add the body
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");
482 return NULL;
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;
491 // Add the field
492 while( field!=NULL ) {
493 ptr += sprintf( ptr, "%s\r\n", field->field_name );
494 field = field->next;
496 ptr += sprintf( ptr, "\r\n" );
497 // Add the body
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;
506 char *
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");
513 return NULL;
515 strcpy( http_hdr->field_search, field_name );
516 return http_get_next_field( http_hdr );
519 char *
520 http_get_next_field( HTTP_header_t *http_hdr ) {
521 char *ptr;
522 HTTP_field_t *field;
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
535 field = field->next;
537 return NULL;
540 void
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");
548 return;
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");
554 return;
556 strcpy( new_field->field_name, field_name );
558 if( http_hdr->last_field==NULL ) {
559 http_hdr->first_field = new_field;
560 } else {
561 http_hdr->last_field->next = new_field;
563 http_hdr->last_field = new_field;
564 http_hdr->field_nb++;
567 void
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");
574 return;
576 strcpy( http_hdr->method, method );
579 void
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");
586 return;
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;
595 int res = -1;
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");
605 goto out;
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");
615 goto out;
618 out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len);
619 if( out_len<0 ) {
620 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n");
621 goto out;
624 b64_usr_pass[out_len]='\0';
626 auth = malloc(encoded_len+22);
627 if( auth==NULL ) {
628 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
629 goto out;
632 sprintf( auth, "Authorization: Basic %s", b64_usr_pass);
633 http_set_field( http_hdr, auth );
634 res = 0;
636 out:
637 free( usr_pass );
638 free( b64_usr_pass );
639 free( auth );
641 return res;
644 void
645 http_debug_hdr( HTTP_header_t *http_hdr ) {
646 HTTP_field_t *field;
647 int i = 0;
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 );
663 field = field->next;
665 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n");
668 int
669 base64_encode(const void *enc, int encLen, char *out, int outMax) {
670 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
672 unsigned char *encBuf;
673 int outLen;
674 unsigned int bits;
675 unsigned int shift;
677 encBuf = (unsigned char*)enc;
678 outLen = 0;
679 bits = 0;
680 shift = 0;
681 outMax &= ~3;
683 while(1) {
684 if( encLen>0 ) {
685 // Shift in byte
686 bits <<= 8;
687 bits |= *encBuf;
688 shift += 8;
689 // Next byte
690 encBuf++;
691 encLen--;
692 } else if( shift>0 ) {
693 // Pad last bits to 6 bits - will end next loop
694 bits <<= 6 - shift;
695 shift = 6;
696 } else {
697 // As per RFC 2045, section 6.8,
698 // pad output as necessary: 0 to 2 '=' chars.
699 while( outLen & 3 ){
700 *out++ = '=';
701 outLen++;
704 return outLen;
707 // Encode 6 bit segments
708 while( shift>=6 ) {
709 if (outLen >= outMax)
710 return -1;
711 shift -= 6;
712 *out = b64[ (bits >> shift) & 0x3F ];
713 out++;
714 outLen++;
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;
722 unsigned int i;
723 int fd = stream->fd;
724 int res = STREAM_UNSUPPORTED;
725 int redirect = 0;
726 int auth_retry=0;
727 int seekable=0;
728 char *content_type;
729 char *next_url;
730 URL_t *url = stream->streaming_ctrl->url;
734 redirect = 0;
735 if (fd > 0) closesocket(fd);
736 fd = http_send_request( url, 0 );
737 if( fd<0 ) {
738 goto err_out;
741 http_free(http_hdr);
742 http_hdr = http_read_response( fd );
743 if( http_hdr==NULL ) {
744 goto err_out;
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 ) {
753 char *accept_ranges;
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 ) {
761 case 200: { // OK
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;
784 else
785 *file_format = DEMUXER_TYPE_AUDIO;
786 res = STREAM_ERROR;
787 goto out;
789 case 400: // Server Full
790 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n");
791 goto err_out;
792 case 401: // Service Unavailable
793 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n");
794 goto err_out;
795 case 403: // Service Forbidden
796 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n");
797 goto err_out;
798 case 404: // Resource Not Found
799 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n");
800 goto err_out;
801 default:
802 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
803 goto err_out;
807 // Assume standard http if not ICY
808 switch( http_hdr->status_code ) {
809 case 200: // OK
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
818 i = 0;
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;
822 res = seekable;
823 goto out;
825 i++;
828 // Not found in the mime type table, don't fail,
829 // we should try raw HTTP
830 res = seekable;
831 goto out;
832 // Redirect
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;
843 goto err_out;
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);
847 goto err_out;
849 if (is_ultravox) {
850 free(url->protocol);
851 url->protocol = strdup("unsv");
853 redirect = 1;
855 break;
856 case 401: // Authentication required
857 if( http_authenticate(http_hdr, url, &auth_retry)<0 )
858 goto err_out;
859 redirect = 1;
860 break;
861 default:
862 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
863 goto err_out;
865 } while( redirect );
867 err_out:
868 if (fd > 0) closesocket( fd );
869 fd = -1;
870 http_free( http_hdr );
871 http_hdr = NULL;
872 out:
873 stream->streaming_ctrl->data = (void*)http_hdr;
874 stream->fd = fd;
875 return res;
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");
893 if (stream->fd >= 0)
894 closesocket(stream->fd);
895 stream->fd = -1;
896 streaming_ctrl_free(stream->streaming_ctrl);
897 stream->streaming_ctrl = NULL;
898 return STREAM_UNSUPPORTED;
901 fixup_network_stream_cache(stream);
902 return STREAM_OK;
905 static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) {
906 int seekable=0;
907 URL_t *url;
909 stream->streaming_ctrl = streaming_ctrl_new();
910 if( stream->streaming_ctrl==NULL ) {
911 return STREAM_ERROR;
913 stream->streaming_ctrl->bandwidth = network_bandwidth;
914 url = url_new(stream->url);
915 stream->streaming_ctrl->url = check4proxies(url);
916 url_free(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)) {
921 if (stream->fd >= 0)
922 closesocket(stream->fd);
923 stream->fd = -1;
924 if (seekable == STREAM_REDIRECTED)
925 return seekable;
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) {
935 int seekable=0;
936 URL_t *url;
938 stream->streaming_ctrl = streaming_ctrl_new();
939 if( stream->streaming_ctrl==NULL ) {
940 return STREAM_ERROR;
942 stream->streaming_ctrl->bandwidth = network_bandwidth;
943 url = url_new(stream->url);
944 stream->streaming_ctrl->url = check4proxies(url);
945 url_free(url);
947 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(2), URL: %s\n", stream->url);
948 seekable = http_streaming_start(stream, file_format);
949 if(seekable < 0) {
950 if (stream->fd >= 0)
951 closesocket(stream->fd);
952 stream->fd = -1;
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 = {
963 "http streaming",
964 "null",
965 "Bertrand, Albeau, Reimar Doeffinger, Arpi?",
966 "plain http",
967 open_s1,
968 {"http", "http_proxy", "unsv", NULL},
969 NULL,
970 0 // Urls are an option string
973 const stream_info_t stream_info_http2 = {
974 "http streaming",
975 "null",
976 "Bertrand, Albeu, Arpi? who?",
977 "plain http, also used as fallback for many other protocols",
978 open_s2,
979 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback
980 NULL,
981 0 // Urls are an option string