source: roaraudio/libroar/vio_proto.c @ 4839:5944793ffc1d

Last change on this file since 4839:5944793ffc1d was 4839:5944793ffc1d, checked in by phi, 13 years ago

do not blindly pass fh

File size: 14.3 KB
Line 
1//vio_proto.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38#ifndef ROAR_WITHOUT_VIO_PROTO
39#include <roaraudio/proto_gopher.h>
40#endif
41
42int roar_vio_proto_init_def  (struct roar_vio_defaults * def, char * dstr, int proto, struct roar_vio_defaults * odef) {
43#ifndef ROAR_WITHOUT_VIO_PROTO
44 int                        port = 0;
45 int                        ret;
46 char                     * ed;
47
48 if ( def == NULL )
49  return -1;
50
51 switch (proto) {
52  case ROAR_VIO_PROTO_P_HTTP:    port =   80; break;
53  case ROAR_VIO_PROTO_P_GOPHER:  port =   70; break;
54  case ROAR_VIO_PROTO_P_ICY:     port = 8000; break;
55  default:
56    return -1;
57 }
58
59 if ( dstr == NULL )
60  dstr = "//";
61
62 if ( roar_vio_dstr_init_defaults(def, ROAR_VIO_DEF_TYPE_SOCKET, O_RDWR, 0644) == -1 )
63  return -1;
64
65 if ( roar_vio_socket_init_tcp4_def(def, "localhost", port) == -1 )
66  return -1;
67
68 if ( !strncmp(dstr, "//", 2) )
69  dstr += 2;
70
71 if ( (ed = strstr(dstr, "/")) != NULL )
72  *ed = 0;
73
74 ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
75
76 ret = roar_vio_socket_init_dstr_def(def, dstr, -1, SOCK_STREAM, def);
77
78 ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
79
80 if ( ed != NULL )
81  *ed = '/';
82
83 ROAR_DBG("roar_vio_proto_init_def(*): dstr='%s'", dstr);
84
85 return ret;
86#else
87 return -1;
88#endif
89}
90
91int roar_vio_open_proto      (struct roar_vio_calls * calls, struct roar_vio_calls * dst,
92                              char * dstr, int proto, struct roar_vio_defaults * odef) {
93#ifndef ROAR_WITHOUT_VIO_PROTO
94 struct roar_vio_proto * self;
95 char * host;
96 char * tmp;
97
98 ROAR_DBG("roar_vio_open_proto(calls=%p, dst=%p, dstr='%s', proto=%i, odef=%p) = ?", calls, dst, dstr, proto, odef);
99
100 if ( calls == NULL || dst == NULL || odef == NULL )
101  return -1;
102
103 ROAR_DBG("roar_vio_open_proto(*): odef->o_flags=%i", odef->o_flags);
104 ROAR_DBG("roar_vio_open_proto(*) = ?");
105
106 if ( (self = roar_mm_malloc(sizeof(struct roar_vio_proto))) == NULL )
107  return -1;
108
109 memset(self, 0, sizeof(struct roar_vio_proto));
110
111 self->next      = dst;
112
113 calls->inst     = self;
114
115 calls->read     = roar_vio_proto_read;
116 calls->write    = roar_vio_proto_write;
117// calls->lseek    = roar_vio_proto_lseek; // TODO: this is currently not supported
118 calls->nonblock = roar_vio_proto_nonblock;
119 calls->sync     = roar_vio_proto_sync;
120 calls->ctl      = roar_vio_proto_ctl;
121 calls->close    = roar_vio_proto_close;
122
123 ROAR_DBG("roar_vio_open_proto(*) = ?");
124
125 if ( dstr != NULL ) {
126  dstr += 2;
127  host  = dstr;
128
129  if ( (tmp = strstr(dstr, "/")) == NULL )
130   return -1;
131
132  *tmp++ = 0;
133  dstr   = tmp;
134
135  if ( (tmp = strstr(dstr, "#")) != NULL )
136   *tmp = 0;
137 } else {
138  ROAR_DBG("roar_vio_open_proto(*): no dstr!, odef->type=%i", odef->type);
139  if ( odef->type == ROAR_VIO_DEF_TYPE_FILE ) {
140   dstr = odef->d.file;
141   host = "localhost";
142
143   for (; *dstr == '/'; dstr++);
144
145  } else if ( odef->type == ROAR_VIO_DEF_TYPE_SOCKET ) {
146   dstr = ""; // index document
147   host = odef->d.socket.host;
148  } else {
149   return -1;
150  }
151 }
152
153 ROAR_DBG("roar_vio_open_proto(*) = ?");
154 ROAR_DBG("roar_vio_open_proto(*): proto=%i, host='%s', file='%s'", proto, host, dstr);
155
156 self->proto = proto;
157
158 switch (proto) {
159  case ROAR_VIO_PROTO_P_HTTP:
160  case ROAR_VIO_PROTO_P_ICY:
161    return roar_vio_open_proto_http(calls, dst, host, dstr);
162   break;
163  case ROAR_VIO_PROTO_P_GOPHER:
164    return roar_vio_open_proto_gopher(calls, dst, host, dstr);
165   break;
166 }
167
168 ROAR_DBG("roar_vio_open_proto(*) = -1 // no matching protocol");
169 return -1;
170#else
171 return -1;
172#endif
173}
174
175#ifndef ROAR_WITHOUT_VIO_PROTO
176ssize_t roar_vio_proto_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
177 struct roar_vio_proto * self = vio->inst;
178 ssize_t ret;
179 ssize_t have = 0;
180 size_t  len;
181
182 if ( self->reader.buffer != NULL ) {
183  len = count;
184  if ( roar_buffer_shift_out(&(self->reader.buffer), buf, &len) == -1 ) {
185   // This is very bad.
186   return -1;
187  }
188
189  if ( len ) {
190   have   = len;
191   buf   += len;
192   count -= len;
193  }
194 }
195
196 if ( count == 0 )
197  return have;
198
199 ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count);
200
201 if ( (ret = roar_vio_read(self->next, buf, count)) == -1 )
202  return ret;
203
204 return have + ret;
205}
206
207ssize_t roar_vio_proto_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
208 struct roar_vio_proto * self = vio->inst;
209
210 return roar_vio_write(self->next, buf, count);
211}
212
213// TODO: this is currently not implemented as this is hard to implement with buffers:
214off_t   roar_vio_proto_lseek   (struct roar_vio_calls * vio, off_t offset, int whence);
215
216int     roar_vio_proto_nonblock(struct roar_vio_calls * vio, int state) {
217 struct roar_vio_proto * self = vio->inst;
218
219 /* we can simply use the next layer's nonblock as all we do in addtion *
220  * to call there functions are our buffers which do not block normaly  */
221
222 return roar_vio_nonblock(self->next, state);
223}
224
225int     roar_vio_proto_sync    (struct roar_vio_calls * vio) {
226 struct roar_vio_proto * self = vio->inst;
227
228 return roar_vio_sync(self->next);
229}
230
231int     roar_vio_proto_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
232 struct roar_vio_proto * self = vio->inst;
233
234 if (vio == NULL || cmd == -1)
235  return -1;
236
237 ROAR_DBG("roar_vio_proto_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
238
239 switch (cmd) {
240  case ROAR_VIO_CTL_GET_NAME:
241    if ( data == NULL )
242     return -1;
243
244    switch (self->proto) {
245     case ROAR_VIO_PROTO_P_HTTP:
246       *(char**)data = "http";
247      break;
248     case ROAR_VIO_PROTO_P_GOPHER:
249       *(char**)data = "gopher";
250      break;
251     case ROAR_VIO_PROTO_P_ICY:
252       *(char**)data = "icy";
253      break;
254     default:
255       *(char**)data = "proto";
256      break;
257    }
258    return 0;
259   break;
260  case ROAR_VIO_CTL_GET_NEXT:
261    *(struct roar_vio_calls **)data = self->next;
262    return 0;
263   break;
264  case ROAR_VIO_CTL_SET_NEXT:
265    self->next = *(struct roar_vio_calls **)data;
266    return 0;
267   break;
268  case ROAR_VIO_CTL_GET_MIMETYPE:
269    if ( data == NULL )
270     return -1;
271
272    if ( self->content_type == NULL )
273     return -1;
274
275    *(char**)data = self->content_type;
276    return 0;
277   break;
278  case ROAR_VIO_CTL_GET_FH:
279  case ROAR_VIO_CTL_GET_SELECT_FH:
280    if ( self->reader.buffer == NULL && self->writer.buffer == NULL )
281     return roar_vio_ctl(self->next, cmd, data);
282    return -1;
283   break;
284  case ROAR_VIO_CTL_GET_READ_FH:
285  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
286    if ( self->reader.buffer == NULL )
287     return roar_vio_ctl(self->next, cmd, data);
288    return -1;
289   break;
290  case ROAR_VIO_CTL_GET_WRITE_FH:
291  case ROAR_VIO_CTL_GET_SELECT_WRITE_FH:
292    if ( self->writer.buffer == NULL )
293     return roar_vio_ctl(self->next, cmd, data);
294    return -1;
295   break;
296 }
297
298 return roar_vio_ctl(self->next, cmd, data);
299}
300
301int     roar_vio_proto_close   (struct roar_vio_calls * vio) {
302 struct roar_vio_proto * self = vio->inst;
303
304 if ( roar_vio_close(self->next) == -1 )
305  return -1;
306
307 if ( self->content_type != NULL )
308  roar_mm_free(self->content_type);
309
310 roar_mm_free(self);
311
312 return 0;
313}
314
315static int _parse_header(struct roar_keyval * kv, char ** buf, int * aligned, char * endofheader) {
316 char * p = *buf;
317 char   c = 0;
318
319 if ( !(*aligned) ) {
320  for (; *p != 0 && *p != '\r' && *p != '\n'; p++);
321  p++;
322  if ( *p == '\n' )
323   p++;
324 }
325
326 if ( p >= endofheader )
327  return -1;
328
329 kv->key = p;
330
331 for (; *p != 0 && *p != '\r' && *p != '\n' && *p != ':'; p++);
332
333 if ( *p == 0 )
334  return -1;
335
336 if ( p >= endofheader )
337  return -1;
338
339 c = *p;
340 *p = 0;
341
342 if ( c == '\r' && *(p+1) == '\n' )
343  p++;
344
345 p++;
346
347 if ( c == '\r' || c == '\n' ) {
348  if ( *(kv->key) == '\r' || *(kv->key) == '\n' )
349   return 0;
350//  printf("Key-only\n");
351  kv->value = kv->key;
352  kv->key   = NULL;
353  *buf = p;
354  return 1;
355 }
356
357 for (; *p == ' '; p++);
358
359 if ( *p == 0 )
360  return -1;
361
362 kv->value = p;
363
364 for (; *p != 0 && *p != '\r' && *p != '\n'; p++);
365
366 if ( *p == 0 )
367  return -1;
368
369 if ( p >= endofheader )
370  return -1;
371
372 c = *p;
373 *p = 0;
374
375 if ( c == '\r' && *(p+1) == '\n' )
376  p++;
377
378 p++;
379
380 *buf = p;
381
382 if ( c == '\r' || c == '\n' ) {
383//  printf("aligned\n");
384  *aligned = 1;
385  p++;
386 } else {
387//  printf("non-aligned(c=0x%x)\n", (int)c);
388  *aligned = 0;
389 }
390
391 if ( *(kv->key) != 0 )
392  return 1;
393
394 return 0;
395}
396
397static void _handle_header (struct roar_vio_proto * self, struct roar_keyval * kv) {
398 ROAR_DBG("_handle_header(*): Header: key='%s', value='%s'", kv->key, kv->value);
399
400 if ( kv->key == NULL || kv->value == NULL )
401  return;
402
403 if ( !strcasecmp(kv->key, "Content-Type") ) {
404  if ( self->content_type != NULL )
405   roar_mm_free(self->content_type);
406
407  self->content_type = roar_mm_strdup(kv->value);
408 }
409}
410
411int roar_vio_open_proto_http   (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) {
412 struct roar_keyval kv;
413 struct roar_vio_proto * self;
414 struct roar_buffer * bufbuf;
415 void * vpbuf;
416 char * buf;
417 char * endofheader = NULL;
418 char * p;
419 char b0[80], b1[80];
420 int  status;
421 int  len;
422 int  oeflen = 4;
423 int  aligned = 1;
424
425 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
426
427 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
428  return -1;
429
430 self         = calls->inst;
431 calls->write = NULL; // Disable write as we do not support this
432
433 if ( roar_buffer_new_data(&bufbuf, 1024, &vpbuf) == -1 )
434  return -1;
435
436 buf = vpbuf;
437
438 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
439
440 roar_vio_printf(dst, "GET /%s HTTP/1.1\r\n", file);
441 roar_vio_printf(dst, "Host: %s\r\n", host);
442 roar_vio_printf(dst, "User-Agent: roar_vio_open_proto_http() $Revision$\r\n");
443 roar_vio_printf(dst, "Connection: close\r\n");
444 roar_vio_printf(dst, "\r\n");
445
446 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
447
448 roar_vio_sync(dst);
449
450 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
451
452 if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) {
453  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
454  roar_buffer_free(bufbuf);
455  return -1;
456 }
457
458 ROAR_DBG("roar_vio_open_proto_http(*): got %i bytes from server.", len);
459
460 buf[len] = 0;
461
462 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
463
464 if ( sscanf(buf, "%79s %i %79s\n", b0, &status, b1) != 3 ) {
465  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
466  roar_buffer_free(bufbuf);
467  return -1;
468 }
469
470 ROAR_DBG("roar_vio_open_proto_http(*): b0='%s'", b0);
471
472 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
473
474 if ( status != 200 ) {
475  ROAR_DBG("roar_vio_open_proto_http(*) = -1 // status=%i", status);
476  roar_buffer_free(bufbuf);
477  return -1;
478 }
479
480 ROAR_DBG("roar_vio_open_proto_http(*): status=%i", status);
481// ROAR_WARN("roar_vio_open_proto_http(*): buf='%s'", buf);
482
483 endofheader = strstr(buf, "\r\n\r\n");
484 if ( endofheader == NULL ) {
485  endofheader = strstr(buf, "\n\n");
486  oeflen = 2;
487 }
488
489 ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
490
491 p = buf;
492 while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 )
493  if ( aligned )
494   _handle_header(self, &kv);
495
496 while ( endofheader == NULL ) {
497  if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
498   return -1;
499
500  buf[len] = 0;
501  endofheader = strstr(buf, "\r\n\r\n");
502  if ( endofheader == NULL ) {
503   endofheader = strstr(buf, "\n\n");
504   oeflen = 2;
505  }
506
507/* Doesn't work good.
508  while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 )
509   if ( aligned )
510    _handle_header(self, &kv);
511*/
512
513  p = buf;
514  while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 )
515   if ( aligned )
516    _handle_header(self, &kv);
517
518  ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
519 }
520
521 ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
522 ROAR_DBG("roar_vio_open_proto_http(*): buf=%p\n", buf);
523
524 if ( (endofheader - buf) == (len - oeflen) ) {
525  roar_buffer_free(bufbuf);
526  bufbuf = NULL;
527 }
528
529 if ( bufbuf != NULL ) {
530  roar_buffer_set_offset(bufbuf, endofheader - buf + oeflen);
531  roar_buffer_set_len(bufbuf,    len - (endofheader - buf + oeflen) - 1);
532 }
533 self->reader.buffer = bufbuf;
534
535/*
536 if ( !strcmp((buf+len)-4, "\r\n\r\n") )
537  return 0;
538
539 while (*buf != '\r' && *buf != '\n') {
540  if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
541   return -1;
542 }
543*/
544
545 return 0;
546}
547
548int roar_vio_open_proto_gopher (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) {
549 struct roar_vio_proto * self;
550 char type;
551 const char * mime = NULL;
552
553 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
554  return -1;
555
556 self         = calls->inst;
557 calls->write = NULL; // Disable write as we do not support this
558
559 ROAR_DBG("roar_vio_open_proto_gopher(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
560
561 type = file[0];
562
563 file++;
564
565 ROAR_DBG("roar_vio_open_proto_gopher(*): type='%c'", type);
566
567 switch (type) {
568  case ROAR_GOPHER_TYPE_FILE:
569    mime = "text/plain";
570   break;
571  case ROAR_GOPHER_TYPE_DIR:
572    mime = "inode/directory";
573   break;
574  case ROAR_GOPHER_TYPE_BIN:
575    mime = "application/octet-stream";
576   break;
577  case ROAR_GOPHER_TYPE_GIF:
578    mime = "image/gif";
579   break;
580  case ROAR_GOPHER_TYPE_HTML:
581    mime = "text/html";
582   break;
583 }
584
585 if ( mime != NULL ) {
586  self->content_type = roar_mm_strdup(mime);
587 }
588
589 roar_vio_printf(dst, "%s\r\n", file);
590
591 roar_vio_sync(dst); // for encryption/compression layers
592
593 return 0;
594}
595#endif
596
597//ll
Note: See TracBrowser for help on using the repository browser.