Mercurial > mplayer.hg
annotate stream/http.c @ 35894:ae62be3d8de2
Handle the severely broken headers QuickTime Streaming Server sends.
Instead of ending the header with \r\n\r\n it ends with
\r\n<4 byte MP3 header>\r\n.
And programs like wget just silently accept this without even
printing a warning!!
| author | reimar |
|---|---|
| date | Sun, 17 Mar 2013 10:50:41 +0000 |
| parents | 6065fb95813e |
| children | 389d43c448b3 |
| rev | line source |
|---|---|
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
1 /* |
| 902 | 2 * HTTP Helper |
|
30426
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
3 * |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
4 * Copyright (C) 2001 Bertrand Baudet <bertrand_baudet@yahoo.com> |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
5 * |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
6 * This file is part of MPlayer. |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
7 * |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
8 * MPlayer is free software; you can redistribute it and/or modify |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
9 * it under the terms of the GNU General Public License as published by |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
10 * the Free Software Foundation; either version 2 of the License, or |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
11 * (at your option) any later version. |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
12 * |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
13 * MPlayer is distributed in the hope that it will be useful, |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
16 * GNU General Public License for more details. |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
17 * |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
18 * You should have received a copy of the GNU General Public License along |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
19 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
|
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 902 | 21 */ |
| 22 | |
|
15614
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
23 #include "config.h" |
|
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
24 |
| 870 | 25 #include <stdio.h> |
| 26 #include <stdlib.h> | |
| 27 #include <string.h> | |
| 15585 | 28 #include <unistd.h> |
| 870 | 29 |
| 28402 | 30 #if !HAVE_WINSOCK2_H |
|
27472
c0b233cd30ca
Revert moving closesocket definition and network headers to network.h.
diego
parents:
27464
diff
changeset
|
31 #else |
|
c0b233cd30ca
Revert moving closesocket definition and network headers to network.h.
diego
parents:
27464
diff
changeset
|
32 #include <winsock2.h> |
|
c0b233cd30ca
Revert moving closesocket definition and network headers to network.h.
diego
parents:
27464
diff
changeset
|
33 #include <ws2tcpip.h> |
|
c0b233cd30ca
Revert moving closesocket definition and network headers to network.h.
diego
parents:
27464
diff
changeset
|
34 #endif |
|
c0b233cd30ca
Revert moving closesocket definition and network headers to network.h.
diego
parents:
27464
diff
changeset
|
35 |
|
c0b233cd30ca
Revert moving closesocket definition and network headers to network.h.
diego
parents:
27464
diff
changeset
|
36 #include "http.h" |
|
4816
f1dea39a50bb
Fixed the http response parser when the http header only has the HTTP
bertrand
parents:
4311
diff
changeset
|
37 #include "url.h" |
| 5915 | 38 #include "mp_msg.h" |
| 870 | 39 |
| 15585 | 40 #include "stream.h" |
|
19312
ab8d6b6deb63
proper inclusion of demuxer.h (including libmpdemux in Makefile only was to make previous split easier)
ben
parents:
19271
diff
changeset
|
41 #include "libmpdemux/demuxer.h" |
| 15585 | 42 #include "network.h" |
| 43 #include "help_mp.h" | |
| 44 | |
| 32585 | 45 #include "libavutil/base64.h" |
| 15585 | 46 |
| 16013 | 47 typedef struct { |
| 48 unsigned metaint; | |
| 49 unsigned metapos; | |
| 50 int is_ultravox; | |
| 51 } scast_data_t; | |
| 52 | |
| 53 /** | |
| 54 * \brief first read any data from sc->buffer then from fd | |
| 55 * \param fd file descriptor to read data from | |
| 56 * \param buffer buffer to read into | |
| 57 * \param len how many bytes to read | |
| 58 * \param sc streaming control containing buffer to read from first | |
| 59 * \return len unless there is a read error or eof | |
| 60 */ | |
| 61 static unsigned my_read(int fd, char *buffer, int len, streaming_ctrl_t *sc) { | |
| 62 unsigned pos = 0; | |
| 63 unsigned cp_len = sc->buffer_size - sc->buffer_pos; | |
| 64 if (cp_len > len) | |
| 65 cp_len = len; | |
| 66 memcpy(buffer, &sc->buffer[sc->buffer_pos], cp_len); | |
| 67 sc->buffer_pos += cp_len; | |
| 68 pos += cp_len; | |
| 69 while (pos < len) { | |
| 16070 | 70 int ret = recv(fd, &buffer[pos], len - pos, 0); |
| 16013 | 71 if (ret <= 0) |
| 72 break; | |
| 73 pos += ret; | |
| 74 } | |
| 75 return pos; | |
| 76 } | |
| 77 | |
| 16032 | 78 /** |
| 79 * \brief read and process (i.e. discard *g*) a block of ultravox metadata | |
| 80 * \param fd file descriptor to read from | |
| 81 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd | |
| 82 * \return number of real data before next metadata block starts or 0 on error | |
| 34343 | 83 * |
| 84 * You can use unsv://samples.mplayerhq.hu/V-codecs/VP5/vp5_artefacts.nsv to | |
| 85 * test. | |
| 16032 | 86 */ |
| 16013 | 87 static unsigned uvox_meta_read(int fd, streaming_ctrl_t *sc) { |
| 88 unsigned metaint; | |
| 16070 | 89 unsigned char info[6] = {0, 0, 0, 0, 0, 0}; |
| 90 int info_read; | |
| 16013 | 91 do { |
| 16070 | 92 info_read = my_read(fd, info, 1, sc); |
| 16013 | 93 if (info[0] == 0x00) |
| 16070 | 94 info_read = my_read(fd, info, 6, sc); |
| 16013 | 95 else |
| 16070 | 96 info_read += my_read(fd, &info[1], 5, sc); |
| 97 if (info_read != 6) // read error or eof | |
| 98 return 0; | |
|
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
99 // sync byte and reserved flags |
|
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
100 if (info[0] != 0x5a || (info[1] & 0xfc) != 0x00) { |
| 16013 | 101 mp_msg(MSGT_DEMUXER, MSGL_ERR, "Invalid or unknown uvox metadata\n"); |
| 102 return 0; | |
| 103 } | |
|
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
104 if (info[1] & 0x01) |
|
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
105 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Encrypted ultravox data\n"); |
| 16013 | 106 metaint = info[4] << 8 | info[5]; |
|
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
107 if ((info[3] & 0xf) < 0x07) { // discard any metadata nonsense |
| 16013 | 108 char *metabuf = malloc(metaint); |
| 109 my_read(fd, metabuf, metaint, sc); | |
| 110 free(metabuf); | |
| 111 } | |
|
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
112 } while ((info[3] & 0xf) < 0x07); |
| 16013 | 113 return metaint; |
| 114 } | |
| 115 | |
| 116 /** | |
| 117 * \brief read one scast meta data entry and print it | |
| 16032 | 118 * \param fd file descriptor to read from |
| 119 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd | |
| 16013 | 120 */ |
| 121 static void scast_meta_read(int fd, streaming_ctrl_t *sc) { | |
| 122 unsigned char tmp = 0; | |
| 123 unsigned metalen; | |
| 124 my_read(fd, &tmp, 1, sc); | |
| 125 metalen = tmp * 16; | |
| 126 if (metalen > 0) { | |
| 30938 | 127 int i; |
|
30941
fb3bde3ec3a8
Change type to uint8_t to avoid checks depending on char signedness.
reimar
parents:
30938
diff
changeset
|
128 uint8_t *info = malloc(metalen + 1); |
| 16013 | 129 unsigned nlen = my_read(fd, info, metalen, sc); |
| 30938 | 130 // avoid breaking the user's terminal too much |
| 131 if (nlen > 256) nlen = 256; | |
| 132 for (i = 0; i < nlen; i++) | |
| 133 if (info[i] && info[i] < 32) info[i] = '?'; | |
| 16013 | 134 info[nlen] = 0; |
| 135 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\nICY Info: %s\n", info); | |
| 136 free(info); | |
| 137 } | |
| 138 } | |
| 139 | |
| 16032 | 140 /** |
| 141 * \brief read data from scast/ultravox stream without any metadata | |
| 142 * \param fd file descriptor to read from | |
| 143 * \param buffer buffer to read data into | |
| 144 * \param size number of bytes to read | |
| 145 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd | |
| 146 */ | |
| 16013 | 147 static int scast_streaming_read(int fd, char *buffer, int size, |
| 148 streaming_ctrl_t *sc) { | |
| 149 scast_data_t *sd = (scast_data_t *)sc->data; | |
| 150 unsigned block, ret; | |
| 151 unsigned done = 0; | |
| 152 | |
| 153 // first read remaining data up to next metadata | |
| 154 block = sd->metaint - sd->metapos; | |
| 155 if (block > size) | |
| 156 block = size; | |
| 157 ret = my_read(fd, buffer, block, sc); | |
| 158 sd->metapos += ret; | |
| 159 done += ret; | |
| 160 if (ret != block) // read problems or eof | |
| 161 size = done; | |
| 162 | |
| 163 while (done < size) { // now comes the metadata | |
| 164 if (sd->is_ultravox) | |
| 16070 | 165 { |
| 16013 | 166 sd->metaint = uvox_meta_read(fd, sc); |
| 16070 | 167 if (!sd->metaint) |
| 168 size = done; | |
| 169 } | |
| 16013 | 170 else |
| 171 scast_meta_read(fd, sc); // read and display metadata | |
| 172 sd->metapos = 0; | |
| 173 block = size - done; | |
| 174 if (block > sd->metaint) | |
| 175 block = sd->metaint; | |
| 176 ret = my_read(fd, &buffer[done], block, sc); | |
| 177 sd->metapos += ret; | |
| 178 done += ret; | |
| 179 if (ret != block) // read problems or eof | |
| 180 size = done; | |
| 181 } | |
| 182 return done; | |
| 183 } | |
| 184 | |
| 185 static int scast_streaming_start(stream_t *stream) { | |
| 186 int metaint; | |
| 187 scast_data_t *scast_data; | |
| 188 HTTP_header_t *http_hdr = stream->streaming_ctrl->data; | |
| 16070 | 189 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0; |
| 16013 | 190 if (!stream || stream->fd < 0 || !http_hdr) |
| 191 return -1; | |
| 192 if (is_ultravox) | |
| 193 metaint = 0; | |
| 194 else { | |
| 195 metaint = atoi(http_get_field(http_hdr, "Icy-MetaInt")); | |
| 196 if (metaint <= 0) | |
| 197 return -1; | |
| 198 } | |
| 199 stream->streaming_ctrl->buffer = malloc(http_hdr->body_size); | |
| 200 stream->streaming_ctrl->buffer_size = http_hdr->body_size; | |
| 201 stream->streaming_ctrl->buffer_pos = 0; | |
| 202 memcpy(stream->streaming_ctrl->buffer, http_hdr->body, http_hdr->body_size); | |
| 203 scast_data = malloc(sizeof(scast_data_t)); | |
| 204 scast_data->metaint = metaint; | |
| 205 scast_data->metapos = 0; | |
| 206 scast_data->is_ultravox = is_ultravox; | |
| 207 http_free(http_hdr); | |
| 208 stream->streaming_ctrl->data = scast_data; | |
| 209 stream->streaming_ctrl->streaming_read = scast_streaming_read; | |
| 210 stream->streaming_ctrl->streaming_seek = NULL; | |
| 211 stream->streaming_ctrl->prebuffer_size = 64 * 1024; // 64 KBytes | |
| 212 stream->streaming_ctrl->buffering = 1; | |
| 213 stream->streaming_ctrl->status = streaming_playing_e; | |
| 214 return 0; | |
| 215 } | |
| 216 | |
| 15585 | 217 static int nop_streaming_start( stream_t *stream ) { |
| 218 HTTP_header_t *http_hdr = NULL; | |
| 219 char *next_url=NULL; | |
| 220 int fd,ret; | |
| 221 if( stream==NULL ) return -1; | |
| 222 | |
| 223 fd = stream->fd; | |
| 224 if( fd<0 ) { | |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
225 fd = http_send_request( stream->streaming_ctrl->url, 0 ); |
| 15585 | 226 if( fd<0 ) return -1; |
| 227 http_hdr = http_read_response( fd ); | |
| 228 if( http_hdr==NULL ) return -1; | |
| 229 | |
| 230 switch( http_hdr->status_code ) { | |
| 231 case 200: // OK | |
| 232 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") ); | |
| 233 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); | |
| 234 if( http_hdr->body_size>0 ) { | |
| 235 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { | |
| 236 http_free( http_hdr ); | |
| 237 return -1; | |
| 238 } | |
| 239 } | |
| 240 break; | |
| 241 // Redirect | |
| 242 case 301: // Permanently | |
| 243 case 302: // Temporarily | |
|
19459
6dad4b1efb82
Handle 303 (See Other) redirect, part of a patch by Benjamin Zores (ben at geexbox org)
reimar
parents:
19312
diff
changeset
|
244 case 303: // See Other |
|
32630
2653cb380532
Add support for HTTP 307 (Temporary Redirect) responses.
cboesch
parents:
32585
diff
changeset
|
245 case 307: // Temporarily (since HTTP/1.1) |
| 15585 | 246 ret=-1; |
| 247 next_url = http_get_field( http_hdr, "Location" ); | |
| 248 | |
|
35301
8f6d3f8ffa61
Add url_new_with_proxy function to reduce code duplication and memleaks.
reimar
parents:
35229
diff
changeset
|
249 if (next_url != NULL) { |
| 15585 | 250 mp_msg(MSGT_NETWORK,MSGL_STATUS,"Redirected: Using this url instead %s\n",next_url); |
|
35301
8f6d3f8ffa61
Add url_new_with_proxy function to reduce code duplication and memleaks.
reimar
parents:
35229
diff
changeset
|
251 stream->streaming_ctrl->url=url_new_with_proxy(next_url); |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
252 ret=nop_streaming_start(stream); //recursively get streaming started |
| 15585 | 253 } else { |
| 254 mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n"); | |
| 255 closesocket( fd ); | |
| 256 fd = -1; | |
| 257 } | |
| 258 return ret; | |
| 259 break; | |
| 260 case 401: //Authorization required | |
| 261 case 403: //Forbidden | |
| 262 case 404: //Not found | |
| 263 case 500: //Server Error | |
| 264 default: | |
| 265 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); | |
| 266 closesocket( fd ); | |
| 267 fd = -1; | |
| 268 return -1; | |
| 269 break; | |
| 270 } | |
| 271 stream->fd = fd; | |
| 272 } else { | |
| 273 http_hdr = (HTTP_header_t*)stream->streaming_ctrl->data; | |
| 274 if( http_hdr->body_size>0 ) { | |
| 275 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { | |
| 276 http_free( http_hdr ); | |
| 277 stream->streaming_ctrl->data = NULL; | |
| 278 return -1; | |
| 279 } | |
| 280 } | |
| 281 } | |
| 282 | |
| 283 if( http_hdr ) { | |
| 284 http_free( http_hdr ); | |
| 285 stream->streaming_ctrl->data = NULL; | |
| 286 } | |
| 287 | |
| 288 stream->streaming_ctrl->streaming_read = nop_streaming_read; | |
| 289 stream->streaming_ctrl->streaming_seek = nop_streaming_seek; | |
| 290 stream->streaming_ctrl->prebuffer_size = 64*1024; // 64 KBytes | |
| 291 stream->streaming_ctrl->buffering = 1; | |
| 292 stream->streaming_ctrl->status = streaming_playing_e; | |
| 293 return 0; | |
| 294 } | |
| 295 | |
| 870 | 296 HTTP_header_t * |
|
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
16948
diff
changeset
|
297 http_new_header(void) { |
| 870 | 298 HTTP_header_t *http_hdr; |
| 299 | |
| 32636 | 300 http_hdr = calloc(1, sizeof(*http_hdr)); |
| 870 | 301 if( http_hdr==NULL ) return NULL; |
| 302 | |
| 303 return http_hdr; | |
| 304 } | |
| 305 | |
| 306 void | |
| 307 http_free( HTTP_header_t *http_hdr ) { | |
| 3039 | 308 HTTP_field_t *field, *field2free; |
| 870 | 309 if( http_hdr==NULL ) return; |
|
32511
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32052
diff
changeset
|
310 free(http_hdr->protocol); |
|
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32052
diff
changeset
|
311 free(http_hdr->uri); |
|
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32052
diff
changeset
|
312 free(http_hdr->reason_phrase); |
|
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32052
diff
changeset
|
313 free(http_hdr->field_search); |
|
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32052
diff
changeset
|
314 free(http_hdr->method); |
|
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32052
diff
changeset
|
315 free(http_hdr->buffer); |
| 3039 | 316 field = http_hdr->first_field; |
| 317 while( field!=NULL ) { | |
| 318 field2free = field; | |
|
32511
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32052
diff
changeset
|
319 free(field->field_name); |
| 3039 | 320 field = field->next; |
| 321 free( field2free ); | |
| 322 } | |
| 870 | 323 free( http_hdr ); |
| 3039 | 324 http_hdr = NULL; |
| 870 | 325 } |
| 326 | |
| 902 | 327 int |
| 328 http_response_append( HTTP_header_t *http_hdr, char *response, int length ) { | |
| 1027 | 329 if( http_hdr==NULL || response==NULL || length<0 ) return -1; |
|
7304
7da2c2a68547
Check if realloc failed on http_hdr->buffer instead of ptr in http_response_append,
bertrand
parents:
7293
diff
changeset
|
330 |
|
18558
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
331 if( (unsigned)length > SIZE_MAX - http_hdr->buffer_size - 1) { |
|
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
332 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Bad size in memory (re)allocation\n"); |
|
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
333 return -1; |
|
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
334 } |
| 30702 | 335 http_hdr->buffer = realloc( http_hdr->buffer, http_hdr->buffer_size+length+1 ); |
|
7304
7da2c2a68547
Check if realloc failed on http_hdr->buffer instead of ptr in http_response_append,
bertrand
parents:
7293
diff
changeset
|
336 if( http_hdr->buffer==NULL ) { |
|
32515
84bdbf9e9c48
Use MSGTR_MemAllocFailed instead of hardcoded string
cboesch
parents:
32511
diff
changeset
|
337 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
| 902 | 338 return -1; |
| 339 } | |
| 7293 | 340 memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length ); |
| 341 http_hdr->buffer_size += length; | |
| 342 http_hdr->buffer[http_hdr->buffer_size]=0; // close the string! | |
| 902 | 343 return http_hdr->buffer_size; |
| 344 } | |
| 345 | |
| 346 int | |
|
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
347 http_is_header_entire( HTTP_header_t *http_hdr ) { |
| 902 | 348 if( http_hdr==NULL ) return -1; |
| 7293 | 349 if( http_hdr->buffer==NULL ) return 0; // empty |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
350 |
|
35894
ae62be3d8de2
Handle the severely broken headers QuickTime Streaming Server sends.
reimar
parents:
35893
diff
changeset
|
351 if(http_hdr->buffer_size > 128*1024) return 1; |
| 3784 | 352 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL && |
| 353 strstr(http_hdr->buffer, "\n\n")==NULL ) return 0; | |
| 354 return 1; | |
| 902 | 355 } |
| 356 | |
| 357 int | |
| 358 http_response_parse( HTTP_header_t *http_hdr ) { | |
| 870 | 359 char *hdr_ptr, *ptr; |
| 360 char *field=NULL; | |
|
18558
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
361 int pos_hdr_sep, hdr_sep_len; |
|
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
362 size_t len; |
| 902 | 363 if( http_hdr==NULL ) return -1; |
| 364 if( http_hdr->is_parsed ) return 0; | |
| 870 | 365 |
| 366 // Get the protocol | |
| 902 | 367 hdr_ptr = strstr( http_hdr->buffer, " " ); |
| 870 | 368 if( hdr_ptr==NULL ) { |
| 5915 | 369 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. No space separator found.\n"); |
| 902 | 370 return -1; |
| 870 | 371 } |
| 902 | 372 len = hdr_ptr-http_hdr->buffer; |
| 18879 | 373 http_hdr->protocol = malloc(len+1); |
| 870 | 374 if( http_hdr->protocol==NULL ) { |
|
32515
84bdbf9e9c48
Use MSGTR_MemAllocFailed instead of hardcoded string
cboesch
parents:
32511
diff
changeset
|
375 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
| 902 | 376 return -1; |
| 870 | 377 } |
| 902 | 378 strncpy( http_hdr->protocol, http_hdr->buffer, len ); |
| 379 http_hdr->protocol[len]='\0'; | |
| 870 | 380 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) { |
| 381 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) { | |
| 5915 | 382 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get HTTP minor version.\n"); |
| 902 | 383 return -1; |
| 870 | 384 } |
| 385 } | |
| 386 | |
| 387 // Get the status code | |
| 388 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) { | |
| 5915 | 389 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get status code.\n"); |
| 902 | 390 return -1; |
| 870 | 391 } |
| 392 hdr_ptr += 4; | |
| 393 | |
| 394 // Get the reason phrase | |
|
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
395 ptr = strstr( hdr_ptr, "\n" ); |
| 35229 | 396 if( ptr==NULL ) { |
| 5915 | 397 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get the reason phrase.\n"); |
| 902 | 398 return -1; |
| 870 | 399 } |
| 400 len = ptr-hdr_ptr; | |
| 18879 | 401 http_hdr->reason_phrase = malloc(len+1); |
| 870 | 402 if( http_hdr->reason_phrase==NULL ) { |
|
32515
84bdbf9e9c48
Use MSGTR_MemAllocFailed instead of hardcoded string
cboesch
parents:
32511
diff
changeset
|
403 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
| 902 | 404 return -1; |
| 870 | 405 } |
| 406 strncpy( http_hdr->reason_phrase, hdr_ptr, len ); | |
| 4311 | 407 if( http_hdr->reason_phrase[len-1]=='\r' ) { |
| 408 len--; | |
| 409 } | |
| 870 | 410 http_hdr->reason_phrase[len]='\0'; |
| 411 | |
| 412 // Set the position of the header separator: \r\n\r\n | |
|
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
413 hdr_sep_len = 4; |
| 902 | 414 ptr = strstr( http_hdr->buffer, "\r\n\r\n" ); |
| 870 | 415 if( ptr==NULL ) { |
|
35894
ae62be3d8de2
Handle the severely broken headers QuickTime Streaming Server sends.
reimar
parents:
35893
diff
changeset
|
416 hdr_sep_len = 2; |
|
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
417 ptr = strstr( http_hdr->buffer, "\n\n" ); |
|
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
418 if( ptr==NULL ) { |
| 5915 | 419 mp_msg(MSGT_NETWORK,MSGL_ERR,"Header may be incomplete. No CRLF CRLF found.\n"); |
|
35894
ae62be3d8de2
Handle the severely broken headers QuickTime Streaming Server sends.
reimar
parents:
35893
diff
changeset
|
420 hdr_sep_len = 0; |
|
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
421 } |
| 870 | 422 } |
| 902 | 423 pos_hdr_sep = ptr-http_hdr->buffer; |
| 870 | 424 |
|
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
425 // Point to the first line after the method line. |
|
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
426 hdr_ptr = strstr( http_hdr->buffer, "\n" )+1; |
| 870 | 427 do { |
|
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
428 ptr = hdr_ptr; |
|
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
429 while( *ptr!='\r' && *ptr!='\n' ) ptr++; |
| 870 | 430 len = ptr-hdr_ptr; |
|
35894
ae62be3d8de2
Handle the severely broken headers QuickTime Streaming Server sends.
reimar
parents:
35893
diff
changeset
|
431 if (len == 0 || !memchr(hdr_ptr, ':', len)) { |
|
ae62be3d8de2
Handle the severely broken headers QuickTime Streaming Server sends.
reimar
parents:
35893
diff
changeset
|
432 mp_msg(MSGT_NETWORK, MSGL_ERR, "Broken response header, missing ':'\n"); |
|
ae62be3d8de2
Handle the severely broken headers QuickTime Streaming Server sends.
reimar
parents:
35893
diff
changeset
|
433 pos_hdr_sep = ptr - http_hdr->buffer; |
|
ae62be3d8de2
Handle the severely broken headers QuickTime Streaming Server sends.
reimar
parents:
35893
diff
changeset
|
434 hdr_sep_len = 0; |
|
ae62be3d8de2
Handle the severely broken headers QuickTime Streaming Server sends.
reimar
parents:
35893
diff
changeset
|
435 break; |
|
ae62be3d8de2
Handle the severely broken headers QuickTime Streaming Server sends.
reimar
parents:
35893
diff
changeset
|
436 } |
|
35893
6065fb95813e
Support broken icy-metaint response from QuickTime Streaming Server.
reimar
parents:
35317
diff
changeset
|
437 if (len > 16 && !strncasecmp(hdr_ptr + 4, "icy-metaint:", 12)) |
|
6065fb95813e
Support broken icy-metaint response from QuickTime Streaming Server.
reimar
parents:
35317
diff
changeset
|
438 { |
|
6065fb95813e
Support broken icy-metaint response from QuickTime Streaming Server.
reimar
parents:
35317
diff
changeset
|
439 mp_msg(MSGT_NETWORK, MSGL_WARN, "Server sent a severely broken icy-metaint HTTP header!\n"); |
|
6065fb95813e
Support broken icy-metaint response from QuickTime Streaming Server.
reimar
parents:
35317
diff
changeset
|
440 hdr_ptr += 4; |
|
6065fb95813e
Support broken icy-metaint response from QuickTime Streaming Server.
reimar
parents:
35317
diff
changeset
|
441 len -= 4; |
|
6065fb95813e
Support broken icy-metaint response from QuickTime Streaming Server.
reimar
parents:
35317
diff
changeset
|
442 } |
| 30702 | 443 field = realloc(field, len+1); |
| 870 | 444 if( field==NULL ) { |
|
32515
84bdbf9e9c48
Use MSGTR_MemAllocFailed instead of hardcoded string
cboesch
parents:
32511
diff
changeset
|
445 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MemAllocFailed); |
| 902 | 446 return -1; |
| 870 | 447 } |
| 448 strncpy( field, hdr_ptr, len ); | |
| 449 field[len]='\0'; | |
| 450 http_set_field( http_hdr, field ); | |
|
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
451 hdr_ptr = ptr+((*ptr=='\r')?2:1); |
| 902 | 452 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) ); |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
453 |
|
32511
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32052
diff
changeset
|
454 free(field); |
| 870 | 455 |
|
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
456 if( pos_hdr_sep+hdr_sep_len<http_hdr->buffer_size ) { |
| 870 | 457 // Response has data! |
|
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
458 http_hdr->body = http_hdr->buffer+pos_hdr_sep+hdr_sep_len; |
|
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
459 http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+hdr_sep_len); |
| 870 | 460 } |
| 461 | |
| 902 | 462 http_hdr->is_parsed = 1; |
| 463 return 0; | |
| 870 | 464 } |
| 465 | |
| 466 char * | |
| 902 | 467 http_build_request( HTTP_header_t *http_hdr ) { |
| 35317 | 468 char *ptr; |
| 902 | 469 int len; |
| 3039 | 470 HTTP_field_t *field; |
| 870 | 471 if( http_hdr==NULL ) return NULL; |
| 472 | |
| 473 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET"); | |
| 474 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/"); | |
| 35317 | 475 if( !http_hdr->uri || !http_hdr->method) |
| 476 return NULL; | |
| 870 | 477 |
| 3497 | 478 //**** Compute the request length |
| 479 // Add the Method line | |
| 35317 | 480 len = strlen(http_hdr->method)+strlen(http_hdr->uri)+12; |
| 3497 | 481 // Add the fields |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
482 field = http_hdr->first_field; |
| 3039 | 483 while( field!=NULL ) { |
| 484 len += strlen(field->field_name)+2; | |
| 485 field = field->next; | |
| 486 } | |
| 3497 | 487 // Add the CRLF |
| 488 len += 2; | |
| 489 // Add the body | |
| 902 | 490 if( http_hdr->body!=NULL ) { |
| 491 len += http_hdr->body_size; | |
| 492 } | |
| 3497 | 493 // Free the buffer if it was previously used |
| 902 | 494 if( http_hdr->buffer!=NULL ) { |
| 495 free( http_hdr->buffer ); | |
| 496 http_hdr->buffer = NULL; | |
| 497 } | |
| 18879 | 498 http_hdr->buffer = malloc(len+1); |
| 902 | 499 if( http_hdr->buffer==NULL ) { |
|
32515
84bdbf9e9c48
Use MSGTR_MemAllocFailed instead of hardcoded string
cboesch
parents:
32511
diff
changeset
|
500 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MemAllocFailed); |
| 902 | 501 return NULL; |
| 502 } | |
| 503 http_hdr->buffer_size = len; | |
| 504 | |
| 3497 | 505 //*** Building the request |
| 902 | 506 ptr = http_hdr->buffer; |
| 3497 | 507 // Add the method line |
| 35317 | 508 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, http_hdr->uri, http_hdr->http_minor_version ); |
| 3039 | 509 field = http_hdr->first_field; |
| 3497 | 510 // Add the field |
| 3039 | 511 while( field!=NULL ) { |
| 512 ptr += sprintf( ptr, "%s\r\n", field->field_name ); | |
| 513 field = field->next; | |
| 514 } | |
| 870 | 515 ptr += sprintf( ptr, "\r\n" ); |
| 3497 | 516 // Add the body |
| 870 | 517 if( http_hdr->body!=NULL ) { |
| 518 memcpy( ptr, http_hdr->body, http_hdr->body_size ); | |
| 519 } | |
| 3497 | 520 |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
521 return http_hdr->buffer; |
| 870 | 522 } |
| 523 | |
| 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; | |
| 3039 | 527 http_hdr->field_search_pos = http_hdr->first_field; |
| 30702 | 528 http_hdr->field_search = realloc( http_hdr->field_search, strlen(field_name)+1 ); |
| 870 | 529 if( http_hdr->field_search==NULL ) { |
|
32515
84bdbf9e9c48
Use MSGTR_MemAllocFailed instead of hardcoded string
cboesch
parents:
32511
diff
changeset
|
530 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
| 870 | 531 return NULL; |
| 532 } | |
| 533 strcpy( http_hdr->field_search, field_name ); | |
| 534 return http_get_next_field( http_hdr ); | |
| 535 } | |
| 536 | |
| 537 char * | |
| 538 http_get_next_field( HTTP_header_t *http_hdr ) { | |
| 539 char *ptr; | |
| 3039 | 540 HTTP_field_t *field; |
| 870 | 541 if( http_hdr==NULL ) return NULL; |
| 542 | |
| 3039 | 543 field = http_hdr->field_search_pos; |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
544 while( field!=NULL ) { |
| 3039 | 545 ptr = strstr( field->field_name, ":" ); |
| 870 | 546 if( ptr==NULL ) return NULL; |
| 3039 | 547 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) { |
| 870 | 548 ptr++; // Skip the column |
| 549 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some | |
| 3039 | 550 http_hdr->field_search_pos = field->next; |
| 870 | 551 return ptr; // return the value without the field name |
| 552 } | |
| 3039 | 553 field = field->next; |
| 870 | 554 } |
| 555 return NULL; | |
| 556 } | |
| 557 | |
| 558 void | |
| 3039 | 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; | |
| 870 | 562 |
| 18879 | 563 new_field = malloc(sizeof(HTTP_field_t)); |
| 3039 | 564 if( new_field==NULL ) { |
|
32515
84bdbf9e9c48
Use MSGTR_MemAllocFailed instead of hardcoded string
cboesch
parents:
32511
diff
changeset
|
565 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
| 870 | 566 return; |
| 567 } | |
| 3039 | 568 new_field->next = NULL; |
| 18879 | 569 new_field->field_name = malloc(strlen(field_name)+1); |
| 3039 | 570 if( new_field->field_name==NULL ) { |
|
32515
84bdbf9e9c48
Use MSGTR_MemAllocFailed instead of hardcoded string
cboesch
parents:
32511
diff
changeset
|
571 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
|
27834
d35bcab9833b
Avoid a memleak if allocation of field_name fails, fixes bug #1319.
reimar
parents:
27473
diff
changeset
|
572 free(new_field); |
| 3039 | 573 return; |
| 574 } | |
| 575 strcpy( new_field->field_name, field_name ); | |
| 576 | |
| 577 if( http_hdr->last_field==NULL ) { | |
| 578 http_hdr->first_field = new_field; | |
| 579 } else { | |
| 580 http_hdr->last_field->next = new_field; | |
| 581 } | |
| 582 http_hdr->last_field = new_field; | |
| 870 | 583 http_hdr->field_nb++; |
| 584 } | |
| 585 | |
| 586 void | |
| 587 http_set_method( HTTP_header_t *http_hdr, const char *method ) { | |
| 588 if( http_hdr==NULL || method==NULL ) return; | |
| 589 | |
| 18879 | 590 http_hdr->method = malloc(strlen(method)+1); |
| 870 | 591 if( http_hdr->method==NULL ) { |
|
32515
84bdbf9e9c48
Use MSGTR_MemAllocFailed instead of hardcoded string
cboesch
parents:
32511
diff
changeset
|
592 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
| 870 | 593 return; |
| 594 } | |
| 595 strcpy( http_hdr->method, method ); | |
| 596 } | |
| 597 | |
| 598 void | |
| 599 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) { | |
| 600 if( http_hdr==NULL || uri==NULL ) return; | |
| 601 | |
| 18879 | 602 http_hdr->uri = malloc(strlen(uri)+1); |
| 870 | 603 if( http_hdr->uri==NULL ) { |
|
32515
84bdbf9e9c48
Use MSGTR_MemAllocFailed instead of hardcoded string
cboesch
parents:
32511
diff
changeset
|
604 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
| 870 | 605 return; |
| 606 } | |
| 607 strcpy( http_hdr->uri, uri ); | |
| 608 } | |
| 609 | |
|
32547
6a629e6fdb09
Add Proxy-Authorization header to authenticate on proxies
cboesch
parents:
32515
diff
changeset
|
610 static int |
|
6a629e6fdb09
Add Proxy-Authorization header to authenticate on proxies
cboesch
parents:
32515
diff
changeset
|
611 http_add_authentication( HTTP_header_t *http_hdr, const char *username, const char *password, const char *auth_str ) { |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
612 char *auth = NULL, *usr_pass = NULL, *b64_usr_pass = NULL; |
| 32585 | 613 int encoded_len, pass_len=0; |
| 614 size_t auth_len, usr_pass_len; | |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
615 int res = -1; |
| 6514 | 616 if( http_hdr==NULL || username==NULL ) return -1; |
| 617 | |
| 618 if( password!=NULL ) { | |
| 619 pass_len = strlen(password); | |
| 620 } | |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
621 |
| 32585 | 622 usr_pass_len = strlen(username) + 1 + pass_len; |
| 623 usr_pass = malloc(usr_pass_len + 1); | |
| 6514 | 624 if( usr_pass==NULL ) { |
|
32515
84bdbf9e9c48
Use MSGTR_MemAllocFailed instead of hardcoded string
cboesch
parents:
32511
diff
changeset
|
625 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
626 goto out; |
| 6514 | 627 } |
| 628 | |
| 629 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password ); | |
| 630 | |
| 32585 | 631 encoded_len = AV_BASE64_SIZE(usr_pass_len); |
| 18879 | 632 b64_usr_pass = malloc(encoded_len); |
| 6514 | 633 if( b64_usr_pass==NULL ) { |
|
32515
84bdbf9e9c48
Use MSGTR_MemAllocFailed instead of hardcoded string
cboesch
parents:
32511
diff
changeset
|
634 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
635 goto out; |
| 6514 | 636 } |
| 32585 | 637 av_base64_encode(b64_usr_pass, encoded_len, usr_pass, usr_pass_len); |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
638 |
|
32547
6a629e6fdb09
Add Proxy-Authorization header to authenticate on proxies
cboesch
parents:
32515
diff
changeset
|
639 auth_len = encoded_len + 100; |
|
6a629e6fdb09
Add Proxy-Authorization header to authenticate on proxies
cboesch
parents:
32515
diff
changeset
|
640 auth = malloc(auth_len); |
| 6514 | 641 if( auth==NULL ) { |
|
32515
84bdbf9e9c48
Use MSGTR_MemAllocFailed instead of hardcoded string
cboesch
parents:
32511
diff
changeset
|
642 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
643 goto out; |
| 6514 | 644 } |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
645 |
|
32547
6a629e6fdb09
Add Proxy-Authorization header to authenticate on proxies
cboesch
parents:
32515
diff
changeset
|
646 snprintf(auth, auth_len, "%s: Basic %s", auth_str, b64_usr_pass); |
| 6514 | 647 http_set_field( http_hdr, auth ); |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
648 res = 0; |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
649 |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
650 out: |
| 6514 | 651 free( usr_pass ); |
| 652 free( b64_usr_pass ); | |
| 653 free( auth ); | |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
654 |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
655 return res; |
| 6514 | 656 } |
| 657 | |
|
32547
6a629e6fdb09
Add Proxy-Authorization header to authenticate on proxies
cboesch
parents:
32515
diff
changeset
|
658 int |
|
6a629e6fdb09
Add Proxy-Authorization header to authenticate on proxies
cboesch
parents:
32515
diff
changeset
|
659 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) { |
|
6a629e6fdb09
Add Proxy-Authorization header to authenticate on proxies
cboesch
parents:
32515
diff
changeset
|
660 return http_add_authentication(http_hdr, username, password, "Authorization"); |
|
6a629e6fdb09
Add Proxy-Authorization header to authenticate on proxies
cboesch
parents:
32515
diff
changeset
|
661 } |
|
6a629e6fdb09
Add Proxy-Authorization header to authenticate on proxies
cboesch
parents:
32515
diff
changeset
|
662 |
|
6a629e6fdb09
Add Proxy-Authorization header to authenticate on proxies
cboesch
parents:
32515
diff
changeset
|
663 int |
|
6a629e6fdb09
Add Proxy-Authorization header to authenticate on proxies
cboesch
parents:
32515
diff
changeset
|
664 http_add_basic_proxy_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) { |
|
6a629e6fdb09
Add Proxy-Authorization header to authenticate on proxies
cboesch
parents:
32515
diff
changeset
|
665 return http_add_authentication(http_hdr, username, password, "Proxy-Authorization"); |
|
6a629e6fdb09
Add Proxy-Authorization header to authenticate on proxies
cboesch
parents:
32515
diff
changeset
|
666 } |
|
6a629e6fdb09
Add Proxy-Authorization header to authenticate on proxies
cboesch
parents:
32515
diff
changeset
|
667 |
| 870 | 668 void |
| 669 http_debug_hdr( HTTP_header_t *http_hdr ) { | |
| 3039 | 670 HTTP_field_t *field; |
| 671 int i = 0; | |
| 902 | 672 if( http_hdr==NULL ) return; |
| 870 | 673 |
| 5915 | 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 ); | |
|
33841
25667edae85c
cleanup: adjust printf format strings to match parameter types
diego
parents:
32720
diff
changeset
|
681 mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%zu]\n", http_hdr->body_size ); |
| 870 | 682 |
| 5915 | 683 mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n"); |
| 3039 | 684 field = http_hdr->first_field; |
| 685 while( field!=NULL ) { | |
| 5915 | 686 mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name ); |
| 3039 | 687 field = field->next; |
| 688 } | |
| 5915 | 689 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n"); |
| 870 | 690 } |
| 6514 | 691 |
|
25970
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
692 static void print_icy_metadata(HTTP_header_t *http_hdr) { |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
693 const char *field_data; |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
694 // note: I skip icy-notice1 and 2, as they contain html <BR> |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
695 // and are IMHO useless info ::atmos |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
696 if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL ) |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
697 mp_msg(MSGT_NETWORK,MSGL_INFO,"Name : %s\n", field_data); |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
698 if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL ) |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
699 mp_msg(MSGT_NETWORK,MSGL_INFO,"Genre : %s\n", field_data); |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
700 if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL ) |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
701 mp_msg(MSGT_NETWORK,MSGL_INFO,"Website: %s\n", field_data); |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
702 // XXX: does this really mean public server? ::atmos |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
703 if( (field_data = http_get_field(http_hdr, "icy-pub")) != NULL ) |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
704 mp_msg(MSGT_NETWORK,MSGL_INFO,"Public : %s\n", atoi(field_data)?"yes":"no"); |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
705 if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL ) |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
706 mp_msg(MSGT_NETWORK,MSGL_INFO,"Bitrate: %skbit/s\n", field_data); |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
707 } |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
708 |
| 21567 | 709 //! If this function succeeds you must closesocket stream->fd |
| 15585 | 710 static int http_streaming_start(stream_t *stream, int* file_format) { |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
711 HTTP_header_t *http_hdr = NULL; |
|
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
712 int fd = stream->fd; |
|
25135
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
713 int res = STREAM_UNSUPPORTED; |
| 15585 | 714 int redirect = 0; |
| 715 int auth_retry=0; | |
| 716 int seekable=0; | |
| 717 char *content_type; | |
|
30357
3fdf04500df2
Handle Content-Length also when Content-Type is not set.
reimar
parents:
30356
diff
changeset
|
718 const char *content_length; |
| 15585 | 719 char *next_url; |
| 720 URL_t *url = stream->streaming_ctrl->url; | |
| 6514 | 721 |
| 15585 | 722 do |
| 723 { | |
|
21541
3b4ed8857b38
Fix potential endless loop in http_streaming_start due
reimar
parents:
21540
diff
changeset
|
724 redirect = 0; |
|
35308
f570e9faa3c7
Fix off-by-one errors in file descriptor validity checks.
reimar
parents:
35301
diff
changeset
|
725 if (fd >= 0) closesocket(fd); |
| 15585 | 726 fd = http_send_request( url, 0 ); |
| 727 if( fd<0 ) { | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
728 goto err_out; |
| 15585 | 729 } |
| 730 | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
731 http_free(http_hdr); |
| 15585 | 732 http_hdr = http_read_response( fd ); |
| 733 if( http_hdr==NULL ) { | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
734 goto err_out; |
| 15585 | 735 } |
| 736 | |
| 17932 | 737 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) { |
| 15585 | 738 http_debug_hdr( http_hdr ); |
| 739 } | |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
740 |
| 15585 | 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 ) { | |
| 30106 | 743 const char *accept_ranges = http_get_field(http_hdr,"Accept-Ranges"); |
|
30105
31b7d4637b50
Fix crash if http reply contains neither "Accept-Ranges" nor "Server" fields.
reimar
parents:
30103
diff
changeset
|
744 const char *server = http_get_field(http_hdr, "Server"); |
| 30106 | 745 if (accept_ranges) |
| 15585 | 746 seekable = strncmp(accept_ranges,"bytes",5)==0; |
|
32720
1afbc8e3ff55
100l, fix hack to check for MakeMKV server being inverted.
reimar
parents:
32698
diff
changeset
|
747 else if (server && (strcmp(server, "gvs 1.0") == 0 || |
|
1afbc8e3ff55
100l, fix hack to check for MakeMKV server being inverted.
reimar
parents:
32698
diff
changeset
|
748 strncmp(server, "MakeMKV", 7) == 0)) { |
|
32698
b171c744cd06
Add MakeMKV to list of webservers always supporting ranges even when they
reimar
parents:
32636
diff
changeset
|
749 // HACK for youtube and MakeMKV incorrectly claiming not to support seeking |
|
b171c744cd06
Add MakeMKV to list of webservers always supporting ranges even when they
reimar
parents:
32636
diff
changeset
|
750 mp_msg(MSGT_NETWORK, MSGL_WARN, "Broken webserver, incorrectly claims to not support Accept-Ranges\n"); |
|
b171c744cd06
Add MakeMKV to list of webservers always supporting ranges even when they
reimar
parents:
32636
diff
changeset
|
751 seekable = 1; |
|
b171c744cd06
Add MakeMKV to list of webservers always supporting ranges even when they
reimar
parents:
32636
diff
changeset
|
752 } |
| 15585 | 753 } |
| 754 | |
|
25971
64b1e4ea04fc
Always display Icy-Metadata if available, whether we recognize an ICY-Server
reimar
parents:
25970
diff
changeset
|
755 print_icy_metadata(http_hdr); |
|
64b1e4ea04fc
Always display Icy-Metadata if available, whether we recognize an ICY-Server
reimar
parents:
25970
diff
changeset
|
756 |
| 15585 | 757 // Check if the response is an ICY status_code reason_phrase |
|
25968
c7f41f9e2eb8
Detect IceCast also by Icy-MetaInt header part in http_streaming_start(),
reimar
parents:
25246
diff
changeset
|
758 if( !strcasecmp(http_hdr->protocol, "ICY") || |
|
c7f41f9e2eb8
Detect IceCast also by Icy-MetaInt header part in http_streaming_start(),
reimar
parents:
25246
diff
changeset
|
759 http_get_field(http_hdr, "Icy-MetaInt") ) { |
| 15585 | 760 switch( http_hdr->status_code ) { |
| 761 case 200: { // OK | |
| 25969 | 762 char *field_data; |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
763 // If content-type == video/nsv we most likely have a winamp video stream |
|
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
764 // otherwise it should be mp3. if there are more types consider adding mime type |
| 15585 | 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; | |
|
16948
9b7925705f5b
Add another content-type for aac audio in shoutcast streams
rtognimp
parents:
16932
diff
changeset
|
768 else if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "audio/aacp") || !strcmp(field_data, "audio/aac"))) |
|
16917
c45409728a9d
Use correct demuxer type for aac in shoutcast streams
rtognimp
parents:
16614
diff
changeset
|
769 *file_format = DEMUXER_TYPE_AAC; |
| 15585 | 770 else |
| 771 *file_format = DEMUXER_TYPE_AUDIO; | |
|
25135
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
772 res = STREAM_ERROR; |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
773 goto out; |
| 15585 | 774 } |
| 775 case 400: // Server Full | |
| 776 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n"); | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
777 goto err_out; |
| 15585 | 778 case 401: // Service Unavailable |
| 779 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n"); | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
780 goto err_out; |
| 15585 | 781 case 403: // Service Forbidden |
| 782 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n"); | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
783 goto err_out; |
| 15585 | 784 case 404: // Resource Not Found |
| 785 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n"); | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
786 goto err_out; |
| 15585 | 787 default: |
| 788 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n"); | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
789 goto err_out; |
| 15585 | 790 } |
| 791 } | |
| 792 | |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
793 // Assume standard http if not ICY |
| 15585 | 794 switch( http_hdr->status_code ) { |
| 795 case 200: // OK | |
|
30357
3fdf04500df2
Handle Content-Length also when Content-Type is not set.
reimar
parents:
30356
diff
changeset
|
796 content_length = http_get_field(http_hdr, "Content-Length"); |
|
3fdf04500df2
Handle Content-Length also when Content-Type is not set.
reimar
parents:
30356
diff
changeset
|
797 if (content_length) { |
|
3fdf04500df2
Handle Content-Length also when Content-Type is not set.
reimar
parents:
30356
diff
changeset
|
798 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", content_length); |
|
3fdf04500df2
Handle Content-Length also when Content-Type is not set.
reimar
parents:
30356
diff
changeset
|
799 stream->end_pos = atoll(content_length); |
|
3fdf04500df2
Handle Content-Length also when Content-Type is not set.
reimar
parents:
30356
diff
changeset
|
800 } |
| 15585 | 801 // Look if we can use the Content-Type |
| 802 content_type = http_get_field( http_hdr, "Content-Type" ); | |
| 803 if( content_type!=NULL ) { | |
| 32557 | 804 unsigned int i; |
| 805 | |
| 15585 | 806 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type ); |
| 807 // Check in the mime type table for a demuxer type | |
| 32557 | 808 for (i = 0; mime_type_table[i].mime_type != NULL; i++) { |
| 15585 | 809 if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) { |
| 810 *file_format = mime_type_table[i].demuxer_type; | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
811 res = seekable; |
|
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
812 goto out; |
| 15585 | 813 } |
| 814 } | |
| 815 } | |
| 816 // Not found in the mime type table, don't fail, | |
| 817 // we should try raw HTTP | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
818 res = seekable; |
|
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
819 goto out; |
| 15585 | 820 // Redirect |
| 821 case 301: // Permanently | |
| 822 case 302: // Temporarily | |
|
19459
6dad4b1efb82
Handle 303 (See Other) redirect, part of a patch by Benjamin Zores (ben at geexbox org)
reimar
parents:
19312
diff
changeset
|
823 case 303: // See Other |
|
32630
2653cb380532
Add support for HTTP 307 (Temporary Redirect) responses.
cboesch
parents:
32585
diff
changeset
|
824 case 307: // Temporarily (since HTTP/1.1) |
| 15585 | 825 // TODO: RFC 2616, recommand to detect infinite redirection loops |
| 826 next_url = http_get_field( http_hdr, "Location" ); | |
| 827 if( next_url!=NULL ) { | |
|
25238
f42b8e689416
Preserve unsv:// protocol specifier over http redirects.
reimar
parents:
25211
diff
changeset
|
828 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0; |
|
20784
720206eef78b
Support URL redirections that do not specify full URL.
reimar
parents:
20185
diff
changeset
|
829 stream->streaming_ctrl->url = url_redirect( &url, next_url ); |
| 35113 | 830 if (url_is_protocol(url, "mms")) { |
|
25135
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
831 res = STREAM_REDIRECTED; |
|
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
832 goto err_out; |
|
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
833 } |
| 35113 | 834 if (!url_is_protocol(url, "http")) { |
|
25135
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
835 mp_msg(MSGT_NETWORK,MSGL_ERR,"Unsupported http %d redirect to %s protocol\n", http_hdr->status_code, url->protocol); |
|
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
836 goto err_out; |
|
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
837 } |
| 35113 | 838 if (is_ultravox) |
| 839 url_set_protocol(url, "unsv"); | |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
840 redirect = 1; |
| 15585 | 841 } |
| 842 break; | |
| 843 case 401: // Authentication required | |
|
21566
79d969f9eec0
STREAM_UNSUPPORTED is -1, so use the former for return value in all places.
reimar
parents:
21565
diff
changeset
|
844 if( http_authenticate(http_hdr, url, &auth_retry)<0 ) |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
845 goto err_out; |
| 15585 | 846 redirect = 1; |
| 847 break; | |
| 848 default: | |
| 849 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
850 goto err_out; |
| 15585 | 851 } |
| 852 } while( redirect ); | |
| 853 | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
854 err_out: |
|
35308
f570e9faa3c7
Fix off-by-one errors in file descriptor validity checks.
reimar
parents:
35301
diff
changeset
|
855 if (fd >= 0) closesocket( fd ); |
|
21565
546cf4a6377a
Make sure stream->fd is set correct (esp. to -1 on error when fd is closed)
reimar
parents:
21542
diff
changeset
|
856 fd = -1; |
| 21542 | 857 http_free( http_hdr ); |
| 21776 | 858 http_hdr = NULL; |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
859 out: |
| 35309 | 860 stream->streaming_ctrl->data = http_hdr; |
|
21565
546cf4a6377a
Make sure stream->fd is set correct (esp. to -1 on error when fd is closed)
reimar
parents:
21542
diff
changeset
|
861 stream->fd = fd; |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
862 return res; |
| 15585 | 863 } |
| 864 | |
| 865 static int fixup_open(stream_t *stream,int seekable) { | |
| 16013 | 866 HTTP_header_t *http_hdr = stream->streaming_ctrl->data; |
|
16078
095e980cf7c0
Some ICY servers (e.g. http://broadcast.spnet.net:8000/darikhigh) do not set
reimar
parents:
16070
diff
changeset
|
867 int is_icy = http_hdr && http_get_field(http_hdr, "Icy-MetaInt"); |
| 16070 | 868 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0; |
| 15585 | 869 |
| 870 stream->type = STREAMTYPE_STREAM; | |
| 16013 | 871 if(!is_icy && !is_ultravox && seekable) |
| 15585 | 872 { |
|
29920
4f740437ed2b
Finally rename the STREAM_SEEK define to MP_STREAM_SEEK, there are just too many
reimar
parents:
29263
diff
changeset
|
873 stream->flags |= MP_STREAM_SEEK; |
| 15585 | 874 stream->seek = http_seek; |
| 875 } | |
| 876 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
| 16013 | 877 if ((!is_icy && !is_ultravox) || scast_streaming_start(stream)) |
| 15585 | 878 if(nop_streaming_start( stream )) { |
| 879 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_start failed\n"); | |
| 21567 | 880 if (stream->fd >= 0) |
| 881 closesocket(stream->fd); | |
| 882 stream->fd = -1; | |
| 15585 | 883 streaming_ctrl_free(stream->streaming_ctrl); |
| 884 stream->streaming_ctrl = NULL; | |
| 24257 | 885 return STREAM_UNSUPPORTED; |
| 15585 | 886 } |
| 887 | |
| 888 fixup_network_stream_cache(stream); | |
| 889 return STREAM_OK; | |
| 890 } | |
| 891 | |
| 892 static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) { | |
| 893 int seekable=0; | |
| 894 | |
| 895 stream->streaming_ctrl = streaming_ctrl_new(); | |
| 896 if( stream->streaming_ctrl==NULL ) { | |
| 897 return STREAM_ERROR; | |
| 898 } | |
| 899 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
|
35301
8f6d3f8ffa61
Add url_new_with_proxy function to reduce code duplication and memleaks.
reimar
parents:
35229
diff
changeset
|
900 stream->streaming_ctrl->url = url_new_with_proxy(stream->url); |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
901 |
| 20185 | 902 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(1), URL: %s\n", stream->url); |
| 15585 | 903 seekable = http_streaming_start(stream, file_format); |
| 904 if((seekable < 0) || (*file_format == DEMUXER_TYPE_ASF)) { | |
| 21567 | 905 if (stream->fd >= 0) |
| 906 closesocket(stream->fd); | |
| 907 stream->fd = -1; | |
|
25135
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
908 if (seekable == STREAM_REDIRECTED) |
|
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
909 return seekable; |
| 15585 | 910 streaming_ctrl_free(stream->streaming_ctrl); |
| 911 stream->streaming_ctrl = NULL; | |
| 24257 | 912 return STREAM_UNSUPPORTED; |
| 15585 | 913 } |
| 914 | |
| 915 return fixup_open(stream, seekable); | |
| 916 } | |
| 917 | |
| 918 static int open_s2(stream_t *stream,int mode, void* opts, int* file_format) { | |
| 919 int seekable=0; | |
| 920 | |
| 921 stream->streaming_ctrl = streaming_ctrl_new(); | |
| 922 if( stream->streaming_ctrl==NULL ) { | |
| 923 return STREAM_ERROR; | |
| 924 } | |
| 925 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
|
35301
8f6d3f8ffa61
Add url_new_with_proxy function to reduce code duplication and memleaks.
reimar
parents:
35229
diff
changeset
|
926 stream->streaming_ctrl->url = url_new_with_proxy(stream->url); |
|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
927 |
| 20185 | 928 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(2), URL: %s\n", stream->url); |
| 15585 | 929 seekable = http_streaming_start(stream, file_format); |
| 930 if(seekable < 0) { | |
| 21567 | 931 if (stream->fd >= 0) |
| 932 closesocket(stream->fd); | |
| 933 stream->fd = -1; | |
| 15585 | 934 streaming_ctrl_free(stream->streaming_ctrl); |
| 935 stream->streaming_ctrl = NULL; | |
| 24257 | 936 return STREAM_UNSUPPORTED; |
| 15585 | 937 } |
| 938 | |
| 939 return fixup_open(stream, seekable); | |
| 940 } | |
| 941 | |
| 942 | |
| 25211 | 943 const stream_info_t stream_info_http1 = { |
| 15585 | 944 "http streaming", |
| 945 "null", | |
| 946 "Bertrand, Albeau, Reimar Doeffinger, Arpi?", | |
| 947 "plain http", | |
| 948 open_s1, | |
|
27847
28deb37052cd
Add a noicyx:// protocol to allow easier testing for misconfigured servers.
reimar
parents:
27834
diff
changeset
|
949 {"http", "http_proxy", "unsv", "icyx", "noicyx", NULL}, |
| 15585 | 950 NULL, |
| 951 0 // Urls are an option string | |
| 952 }; | |
| 953 | |
| 25211 | 954 const stream_info_t stream_info_http2 = { |
| 15585 | 955 "http streaming", |
| 956 "null", | |
| 957 "Bertrand, Albeu, Arpi? who?", | |
|
16932
c30e0970250c
fix typos: aslo->also, falback->fallback (they were just too annoying *g*)
reimar
parents:
16917
diff
changeset
|
958 "plain http, also used as fallback for many other protocols", |
| 15585 | 959 open_s2, |
| 960 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback | |
| 961 NULL, | |
| 962 0 // Urls are an option string | |
| 963 }; |
