source: roaraudio/libroar/vio_proto.c @ 3067:b395e36e014a

Last change on this file since 3067:b395e36e014a was 3067:b395e36e014a, checked in by phi, 14 years ago

rewrote most of the HTTP code for new API, still does not work with shoutcast

File size: 10.0 KB
RevLine 
[1347]1//vio_proto.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
[1349]37int roar_vio_proto_init_def  (struct roar_vio_defaults * def, char * dstr, int proto, struct roar_vio_defaults * odef) {
[1439]38#ifndef ROAR_WITHOUT_VIO_PROTO
[1349]39 int                        port = 0;
40 int                        ret;
41 char                     * ed;
42
43 if ( def == NULL )
44  return -1;
45
46 switch (proto) {
47  case ROAR_VIO_PROTO_P_HTTP:    port = 80; break;
48  case ROAR_VIO_PROTO_P_GOPHER:  port = 70; break;
49  default:
50    return -1;
51 }
52
[1353]53 if ( dstr == NULL )
54  dstr = "//";
55
[1349]56 if ( roar_vio_dstr_init_defaults(def, ROAR_VIO_DEF_TYPE_SOCKET, O_RDWR, 0644) == -1 )
57  return -1;
58
59 if ( roar_vio_socket_init_tcp4_def(def, "localhost", port) == -1 )
60  return -1;
61
62 if ( !strncmp(dstr, "//", 2) )
63  dstr += 2;
64
65 if ( (ed = strstr(dstr, "/")) != NULL )
66  *ed = 0;
67
[1616]68 ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
[1353]69
[1349]70 ret = roar_vio_socket_init_dstr_def(def, dstr, -1, SOCK_STREAM, def);
71
[1616]72 ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
[1353]73
[1349]74 if ( ed != NULL )
75  *ed = '/';
76
[1650]77 ROAR_DBG("roar_vio_proto_init_def(*): dstr='%s'", dstr);
78
[1349]79 return ret;
[1439]80#else
81 return -1;
82#endif
[1349]83}
84
85int roar_vio_open_proto      (struct roar_vio_calls * calls, struct roar_vio_calls * dst,
86                              char * dstr, int proto, struct roar_vio_defaults * odef) {
[1439]87#ifndef ROAR_WITHOUT_VIO_PROTO
[3066]88 struct roar_vio_proto * self;
[1349]89 char * host;
90 char * tmp;
91
[1616]92 ROAR_DBG("roar_vio_open_proto(calls=%p, dst=%p, dstr='%s', proto=%i, odef=%p) = ?", calls, dst, dstr, proto, odef);
[1349]93
[1353]94 if ( calls == NULL || dst == NULL || odef == NULL )
[1349]95  return -1;
96
[1616]97 ROAR_DBG("roar_vio_open_proto(*): odef->o_flags=%i", odef->o_flags);
[1349]98 ROAR_DBG("roar_vio_open_proto(*) = ?");
99
[3066]100 if ( (self = roar_mm_malloc(sizeof(struct roar_vio_proto))) == NULL )
[1349]101  return -1;
102
[3066]103 memset(self, 0, sizeof(struct roar_vio_proto));
104
105 self->next      = dst;
106
107 calls->inst     = self;
108
109 calls->read     = roar_vio_proto_read;
110 calls->write    = roar_vio_proto_write;
111// calls->lseek    = roar_vio_proto_lseek; // TODO: this is currently not supported
112 calls->nonblock = roar_vio_proto_nonblock;
113 calls->sync     = roar_vio_proto_sync;
114 calls->ctl      = roar_vio_proto_ctl;
115 calls->close    = roar_vio_proto_close;
116
[1353]117 ROAR_DBG("roar_vio_open_proto(*) = ?");
118
119 if ( dstr != NULL ) {
120  dstr += 2;
121  host  = dstr;
[1349]122
[1353]123  if ( (tmp = strstr(dstr, "/")) == NULL )
124   return -1;
125
126  *tmp++ = 0;
127  dstr   = tmp;
[1349]128
[1353]129  if ( (tmp = strstr(dstr, "#")) != NULL )
130   *tmp = 0;
131 } else {
132  ROAR_DBG("roar_vio_open_proto(*): no dstr!, odef->type=%i", odef->type);
133  if ( odef->type == ROAR_VIO_DEF_TYPE_FILE ) {
134   dstr = odef->d.file;
135   host = "localhost";
[1349]136
[1353]137   for (; *dstr == '/'; dstr++);
138
139  } else if ( odef->type == ROAR_VIO_DEF_TYPE_SOCKET ) {
140   dstr = ""; // index document
141   host = odef->d.socket.host;
142  } else {
143   return -1;
144  }
145 }
[1349]146
147 ROAR_DBG("roar_vio_open_proto(*) = ?");
[1616]148 ROAR_DBG("roar_vio_open_proto(*): proto=%i, host='%s', file='%s'", proto, host, dstr);
[1349]149
150 switch (proto) {
151  case ROAR_VIO_PROTO_P_HTTP:
152    return roar_vio_open_proto_http(calls, dst, host, dstr);
153   break;
154  case ROAR_VIO_PROTO_P_GOPHER:
155    return roar_vio_open_proto_gopher(calls, dst, host, dstr);
156   break;
157 }
158
[1650]159 ROAR_DBG("roar_vio_open_proto(*) = -1 // no matching protocol");
[1349]160 return -1;
[1439]161#else
162 return -1;
163#endif
[1349]164}
165
[1439]166#ifndef ROAR_WITHOUT_VIO_PROTO
[3065]167ssize_t roar_vio_proto_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
168 struct roar_vio_proto * self = vio->inst;
169 ssize_t ret;
170 ssize_t have = 0;
171 size_t  len;
172
173 if ( self->reader.buffer != NULL ) {
174  len = count;
175  if ( roar_buffer_shift_out(&(self->reader.buffer), buf, &len) == -1 ) {
176   // This is very bad.
177   return -1;
178  }
179
180  if ( len ) {
181   have   = len;
182   buf   += len;
183   count -= len;
184  }
185 }
186
187 if ( count == 0 )
188  return have;
189
[3067]190 ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count);
191
[3065]192 if ( (ret = roar_vio_read(self->next, buf, count)) == -1 )
193  return ret;
194
195 return have + ret;
196}
197
198ssize_t roar_vio_proto_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
199 struct roar_vio_proto * self = vio->inst;
200
201 return roar_vio_write(self->next, buf, count);
202}
203
204// TODO: this is currently not implemented as this is hard to implement with buffers:
205off_t   roar_vio_proto_lseek   (struct roar_vio_calls * vio, off_t offset, int whence);
206
207int     roar_vio_proto_nonblock(struct roar_vio_calls * vio, int state) {
208 struct roar_vio_proto * self = vio->inst;
209
210 /* we can simply use the next layer's nonblock as all we do in addtion *
211  * to call there functions are our buffers which do not block normaly  */
212
213 return roar_vio_nonblock(self->next, state);
214}
215
216int     roar_vio_proto_sync    (struct roar_vio_calls * vio) {
217 struct roar_vio_proto * self = vio->inst;
218
219 return roar_vio_sync(self->next);
220}
221
222int     roar_vio_proto_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
223 struct roar_vio_proto * self = vio->inst;
224
225 if (vio == NULL || cmd == -1)
226  return -1;
227
228 ROAR_DBG("roar_vio_proto_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
229
230 switch (cmd) {
231  case ROAR_VIO_CTL_GET_NEXT:
232    *(struct roar_vio_calls **)data = self->next;
233    return 0;
234   break;
235  case ROAR_VIO_CTL_SET_NEXT:
236    self->next = *(struct roar_vio_calls **)data;
237    return 0;
238   break;
239 }
240
241 return roar_vio_ctl(self->next, cmd, data);
242}
243
244int     roar_vio_proto_close   (struct roar_vio_calls * vio) {
245 struct roar_vio_proto * self = vio->inst;
246
247 if ( roar_vio_close(self->next) == -1 )
248  return -1;
249
250 roar_mm_free(self);
251
252 return 0;
253}
254
255
[1349]256int roar_vio_open_proto_http   (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) {
[3067]257 struct roar_vio_proto * self;
258 struct roar_buffer * bufbuf;
259 char * buf;
260 char * endofheader = NULL;
[1352]261 char b0[80], b1[80];
262 int  status;
263 int  len;
264
[1650]265 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
266
[1352]267 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
268  return -1;
269
[3067]270 self         = calls->inst;
[3066]271 calls->write = NULL; // Disable write as we do not support this
272
[3067]273 if ( roar_buffer_new(&bufbuf, 1024) == -1 )
274  return -1;
275
276 if ( roar_buffer_get_data(bufbuf, &buf) == -1 ) {
277  roar_buffer_free(bufbuf);
278  return -1;
279 }
280
[1650]281 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
282
[1352]283 roar_vio_printf(dst, "GET /%s HTTP/1.1\r\n", file);
284 roar_vio_printf(dst, "Host: %s\r\n", host);
285 roar_vio_printf(dst, "User-Agent: roar_vio_open_proto_http() $Revision$\r\n");
286 roar_vio_printf(dst, "Connection: close\r\n");
287 roar_vio_printf(dst, "\r\n");
288
[1650]289 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
290
[1352]291 roar_vio_sync(dst);
292
[1650]293 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
294
295 if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) {
296  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
[3067]297  roar_buffer_free(bufbuf);
[1352]298  return -1;
[1650]299 }
[1352]300
301 buf[len] = 0;
302
[1650]303 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
304
[1352]305 if ( sscanf(buf, "%79s %i %79s\n", b0, &status, b1) != 3 ) {
[1650]306  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
[3067]307  roar_buffer_free(bufbuf);
[1352]308  return -1;
309 }
310
[1650]311 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
312
313 if ( status != 200 ) {
314  ROAR_DBG("roar_vio_open_proto_http(*) = -1 // status=%i", status);
[3067]315  roar_buffer_free(bufbuf);
[1352]316  return -1;
[1650]317 }
[1352]318
[1616]319 ROAR_DBG("roar_vio_open_proto_http(*): status=%i", status);
[1352]320// ROAR_WARN("roar_vio_open_proto_http(*): buf='%s'", buf);
321
[3067]322 endofheader = strstr(buf, "\r\n\r\n");
323
324 ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
325
326 while ( endofheader == NULL ) {
327  if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
328   return -1;
329
330  buf[len] = 0;
331  endofheader = strstr(buf, "\r\n\r\n");
332  ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
333 }
334
335 ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
336 ROAR_DBG("roar_vio_open_proto_http(*): buf=%p\n", buf);
337
338 if ( (endofheader - buf) == (len - 4) ) {
339  roar_buffer_free(bufbuf);
340  bufbuf = NULL;
341 }
342
343 if ( bufbuf != NULL ) {
344  roar_buffer_set_offset(bufbuf, endofheader - buf + 4);
345  roar_buffer_set_len(bufbuf,    1024 - (endofheader - buf + 4) - 1);
346 }
347 self->reader.buffer = bufbuf;
348
349/*
[1352]350 if ( !strcmp((buf+len)-4, "\r\n\r\n") )
351  return 0;
352
353 while (*buf != '\r' && *buf != '\n') {
354  if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
355   return -1;
356 }
[3067]357*/
[1352]358
359 return 0;
[1349]360}
361
362int roar_vio_open_proto_gopher (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) {
363 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
364  return -1;
365
[3066]366 calls->write = NULL; // Disable write as we do not support this
367
[1349]368 ROAR_DBG("roar_vio_open_proto_gopher(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
369
[1352]370 if ( file[1] == '/' )
371  file += 2;
[1349]372
[1352]373 roar_vio_printf(dst, "/%s\r\n", file);
[1349]374
375 roar_vio_sync(dst); // for encryption/compression layers
376
377 return 0;
378}
[1439]379#endif
[1349]380
[1347]381//ll
Note: See TracBrowser for help on using the repository browser.