annotate stream/http.c @ 36125:c4075d9e329d

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