source: roaraudio/libroar/vio_proto.c @ 4832:085f96f6555c

Last change on this file since 4832:085f96f6555c was 4832:085f96f6555c, checked in by phi, 13 years ago

support gopher URLs not starting with /. report mime types for gopher objects if a 1:1 mapping is possible

File size: 13.7 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 }
279
280 return roar_vio_ctl(self->next, cmd, data);
281}
282
283int     roar_vio_proto_close   (struct roar_vio_calls * vio) {
284 struct roar_vio_proto * self = vio->inst;
285
286 if ( roar_vio_close(self->next) == -1 )
287  return -1;
288
289 if ( self->content_type != NULL )
290  roar_mm_free(self->content_type);
291
292 roar_mm_free(self);
293
294 return 0;
295}
296
297static int _parse_header(struct roar_keyval * kv, char ** buf, int * aligned, char * endofheader) {
298 char * p = *buf;
299 char   c = 0;
300
301 if ( !(*aligned) ) {
302  for (; *p != 0 && *p != '\r' && *p != '\n'; p++);
303  p++;
304  if ( *p == '\n' )
305   p++;
306 }
307
308 if ( p >= endofheader )
309  return -1;
310
311 kv->key = p;
312
313 for (; *p != 0 && *p != '\r' && *p != '\n' && *p != ':'; p++);
314
315 if ( *p == 0 )
316  return -1;
317
318 if ( p >= endofheader )
319  return -1;
320
321 c = *p;
322 *p = 0;
323
324 if ( c == '\r' && *(p+1) == '\n' )
325  p++;
326
327 p++;
328
329 if ( c == '\r' || c == '\n' ) {
330  if ( *(kv->key) == '\r' || *(kv->key) == '\n' )
331   return 0;
332//  printf("Key-only\n");
333  kv->value = kv->key;
334  kv->key   = NULL;
335  *buf = p;
336  return 1;
337 }
338
339 for (; *p == ' '; p++);
340
341 if ( *p == 0 )
342  return -1;
343
344 kv->value = p;
345
346 for (; *p != 0 && *p != '\r' && *p != '\n'; p++);
347
348 if ( *p == 0 )
349  return -1;
350
351 if ( p >= endofheader )
352  return -1;
353
354 c = *p;
355 *p = 0;
356
357 if ( c == '\r' && *(p+1) == '\n' )
358  p++;
359
360 p++;
361
362 *buf = p;
363
364 if ( c == '\r' || c == '\n' ) {
365//  printf("aligned\n");
366  *aligned = 1;
367  p++;
368 } else {
369//  printf("non-aligned(c=0x%x)\n", (int)c);
370  *aligned = 0;
371 }
372
373 if ( *(kv->key) != 0 )
374  return 1;
375
376 return 0;
377}
378
379static void _handle_header (struct roar_vio_proto * self, struct roar_keyval * kv) {
380 ROAR_DBG("_handle_header(*): Header: key='%s', value='%s'", kv->key, kv->value);
381
382 if ( kv->key == NULL || kv->value == NULL )
383  return;
384
385 if ( !strcasecmp(kv->key, "Content-Type") ) {
386  if ( self->content_type != NULL )
387   roar_mm_free(self->content_type);
388
389  self->content_type = roar_mm_strdup(kv->value);
390 }
391}
392
393int roar_vio_open_proto_http   (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) {
394 struct roar_keyval kv;
395 struct roar_vio_proto * self;
396 struct roar_buffer * bufbuf;
397 void * vpbuf;
398 char * buf;
399 char * endofheader = NULL;
400 char * p;
401 char b0[80], b1[80];
402 int  status;
403 int  len;
404 int  oeflen = 4;
405 int  aligned = 1;
406
407 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
408
409 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
410  return -1;
411
412 self         = calls->inst;
413 calls->write = NULL; // Disable write as we do not support this
414
415 if ( roar_buffer_new_data(&bufbuf, 1024, &vpbuf) == -1 )
416  return -1;
417
418 buf = vpbuf;
419
420 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
421
422 roar_vio_printf(dst, "GET /%s HTTP/1.1\r\n", file);
423 roar_vio_printf(dst, "Host: %s\r\n", host);
424 roar_vio_printf(dst, "User-Agent: roar_vio_open_proto_http() $Revision$\r\n");
425 roar_vio_printf(dst, "Connection: close\r\n");
426 roar_vio_printf(dst, "\r\n");
427
428 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
429
430 roar_vio_sync(dst);
431
432 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
433
434 if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) {
435  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
436  roar_buffer_free(bufbuf);
437  return -1;
438 }
439
440 ROAR_DBG("roar_vio_open_proto_http(*): got %i bytes from server.", len);
441
442 buf[len] = 0;
443
444 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
445
446 if ( sscanf(buf, "%79s %i %79s\n", b0, &status, b1) != 3 ) {
447  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
448  roar_buffer_free(bufbuf);
449  return -1;
450 }
451
452 ROAR_DBG("roar_vio_open_proto_http(*): b0='%s'", b0);
453
454 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
455
456 if ( status != 200 ) {
457  ROAR_DBG("roar_vio_open_proto_http(*) = -1 // status=%i", status);
458  roar_buffer_free(bufbuf);
459  return -1;
460 }
461
462 ROAR_DBG("roar_vio_open_proto_http(*): status=%i", status);
463// ROAR_WARN("roar_vio_open_proto_http(*): buf='%s'", buf);
464
465 endofheader = strstr(buf, "\r\n\r\n");
466 if ( endofheader == NULL ) {
467  endofheader = strstr(buf, "\n\n");
468  oeflen = 2;
469 }
470
471 ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
472
473 p = buf;
474 while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 )
475  if ( aligned )
476   _handle_header(self, &kv);
477
478 while ( endofheader == NULL ) {
479  if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
480   return -1;
481
482  buf[len] = 0;
483  endofheader = strstr(buf, "\r\n\r\n");
484  if ( endofheader == NULL ) {
485   endofheader = strstr(buf, "\n\n");
486   oeflen = 2;
487  }
488
489/* Doesn't work good.
490  while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 )
491   if ( aligned )
492    _handle_header(self, &kv);
493*/
494
495  p = buf;
496  while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 )
497   if ( aligned )
498    _handle_header(self, &kv);
499
500  ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
501 }
502
503 ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
504 ROAR_DBG("roar_vio_open_proto_http(*): buf=%p\n", buf);
505
506 if ( (endofheader - buf) == (len - oeflen) ) {
507  roar_buffer_free(bufbuf);
508  bufbuf = NULL;
509 }
510
511 if ( bufbuf != NULL ) {
512  roar_buffer_set_offset(bufbuf, endofheader - buf + oeflen);
513  roar_buffer_set_len(bufbuf,    len - (endofheader - buf + oeflen) - 1);
514 }
515 self->reader.buffer = bufbuf;
516
517/*
518 if ( !strcmp((buf+len)-4, "\r\n\r\n") )
519  return 0;
520
521 while (*buf != '\r' && *buf != '\n') {
522  if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
523   return -1;
524 }
525*/
526
527 return 0;
528}
529
530int roar_vio_open_proto_gopher (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) {
531 struct roar_vio_proto * self;
532 char type;
533 const char * mime = NULL;
534
535 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
536  return -1;
537
538 self         = calls->inst;
539 calls->write = NULL; // Disable write as we do not support this
540
541 ROAR_DBG("roar_vio_open_proto_gopher(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
542
543 type = file[0];
544
545 file++;
546
547 ROAR_DBG("roar_vio_open_proto_gopher(*): type='%c'", type);
548
549 switch (type) {
550  case ROAR_GOPHER_TYPE_FILE:
551    mime = "text/plain";
552   break;
553  case ROAR_GOPHER_TYPE_DIR:
554    mime = "inode/directory";
555   break;
556  case ROAR_GOPHER_TYPE_BIN:
557    mime = "application/octet-stream";
558   break;
559  case ROAR_GOPHER_TYPE_GIF:
560    mime = "image/gif";
561   break;
562  case ROAR_GOPHER_TYPE_HTML:
563    mime = "text/html";
564   break;
565 }
566
567 if ( mime != NULL ) {
568  self->content_type = roar_mm_strdup(mime);
569 }
570
571 roar_vio_printf(dst, "%s\r\n", file);
572
573 roar_vio_sync(dst); // for encryption/compression layers
574
575 return 0;
576}
577#endif
578
579//ll
Note: See TracBrowser for help on using the repository browser.