source: roaraudio/libroar/vio_proto.c @ 5380:0504bc7766aa

Last change on this file since 5380:0504bc7766aa was 5278:b3e0dd3f3141, checked in by phi, 12 years ago

last parts of merging _nonblock into _ctl and fixed sizeof(cmd) of _ctls

File size: 16.1 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 char                     * tmp;
48
49 if ( def == NULL )
50  return -1;
51
52 switch (proto) {
53  case ROAR_VIO_PROTO_P_HTTP:    port =   80; break;
54  case ROAR_VIO_PROTO_P_GOPHER:  port =   70; break;
55  case ROAR_VIO_PROTO_P_ICY:     port = 8000; break;
56  default:
57    return -1;
58 }
59
60 if ( dstr == NULL )
61  dstr = "//";
62
63 if ( roar_vio_dstr_init_defaults(def, ROAR_VIO_DEF_TYPE_SOCKET, O_RDWR, 0644) == -1 )
64  return -1;
65
66 if ( roar_vio_socket_init_tcp4_def(def, "localhost", port) == -1 )
67  return -1;
68
69 if ( !strncmp(dstr, "//", 2) )
70  dstr += 2;
71
72 if ( (ed = strstr(dstr, "/")) != NULL )
73  *ed = 0;
74
75 if ( (tmp = strstr(dstr, "@")) != NULL )
76  dstr = tmp + 1;
77
78 ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
79
80 ret = roar_vio_socket_init_dstr_def(def, dstr, -1, SOCK_STREAM, def);
81
82 ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
83
84 if ( ed != NULL )
85  *ed = '/';
86
87 ROAR_DBG("roar_vio_proto_init_def(*): dstr='%s'", dstr);
88
89 return ret;
90#else
91 return -1;
92#endif
93}
94
95int roar_vio_open_proto      (struct roar_vio_calls * calls, struct roar_vio_calls * dst,
96                              const char * dstr, int proto, struct roar_vio_defaults * odef) {
97#ifndef ROAR_WITHOUT_VIO_PROTO
98 struct roar_userpass userpass = {.subtype = -1, .user = NULL, .pass = NULL};
99 struct roar_vio_proto * self;
100 const char * host;
101 char * tmp;
102
103 ROAR_DBG("roar_vio_open_proto(calls=%p, dst=%p, dstr='%s', proto=%i, odef=%p) = ?", calls, dst, dstr, proto, odef);
104
105 if ( calls == NULL || dst == NULL || odef == NULL )
106  return -1;
107
108 ROAR_DBG("roar_vio_open_proto(*): odef->o_flags=%i", odef->o_flags);
109 ROAR_DBG("roar_vio_open_proto(*) = ?");
110
111 if ( (self = roar_mm_malloc(sizeof(struct roar_vio_proto))) == NULL )
112  return -1;
113
114 memset(self, 0, sizeof(struct roar_vio_proto));
115
116 self->next      = dst;
117
118 calls->inst     = self;
119
120 calls->read     = roar_vio_proto_read;
121 calls->write    = roar_vio_proto_write;
122 calls->sync     = roar_vio_proto_sync;
123 calls->ctl      = roar_vio_proto_ctl;
124 calls->close    = roar_vio_proto_close;
125
126 ROAR_DBG("roar_vio_open_proto(*) = ?");
127
128 if ( dstr != NULL ) {
129  dstr += 2;
130  host  = dstr;
131
132  if ( (tmp = strstr(dstr, "/")) == NULL )
133   return -1;
134
135  *tmp++ = 0;
136  dstr   = tmp;
137
138  if ( (tmp = strstr(dstr, "#")) != NULL )
139   *tmp = 0;
140 } else {
141  ROAR_DBG("roar_vio_open_proto(*): no dstr!, odef->type=%i", odef->type);
142  if ( odef->type == ROAR_VIO_DEF_TYPE_FILE ) {
143   dstr = odef->d.file;
144   host = "localhost";
145
146   for (; *dstr == '/'; dstr++);
147
148  } else if ( odef->type == ROAR_VIO_DEF_TYPE_SOCKET ) {
149   dstr = ""; // index document
150   host = odef->d.socket.host;
151  } else {
152   return -1;
153  }
154 }
155
156 if ( (tmp = strstr(host, "@")) != NULL ) {
157  userpass.user = (char*)host;
158  *tmp = 0;
159  host = tmp + 1;
160  if ( (tmp = strstr(userpass.user, ":")) != NULL ) {
161   *tmp = 0;
162   userpass.pass = tmp + 1;
163  }
164 }
165
166 ROAR_DBG("roar_vio_open_proto(*) = ?");
167 ROAR_DBG("roar_vio_open_proto(*): proto=%i, host='%s', file='%s', userpass={.user='%s', .pass='%s'}", proto, host, dstr, userpass.user, userpass.pass);
168
169 self->proto = proto;
170
171 switch (proto) {
172  case ROAR_VIO_PROTO_P_HTTP:
173  case ROAR_VIO_PROTO_P_ICY:
174    return roar_vio_open_proto_http(calls, dst, host, dstr, userpass.user != NULL ? &userpass : NULL);
175   break;
176  case ROAR_VIO_PROTO_P_GOPHER:
177    return roar_vio_open_proto_gopher(calls, dst, host, dstr);
178   break;
179 }
180
181 ROAR_DBG("roar_vio_open_proto(*) = -1 // no matching protocol");
182 return -1;
183#else
184 return -1;
185#endif
186}
187
188#ifndef ROAR_WITHOUT_VIO_PROTO
189ssize_t roar_vio_proto_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
190 struct roar_vio_proto * self = vio->inst;
191 ssize_t ret;
192 ssize_t have = 0;
193 size_t  len;
194
195 ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count);
196
197 if ( self->reader.buffer != NULL ) {
198  len = count;
199  if ( roar_buffer_shift_out(&(self->reader.buffer), buf, &len) == -1 ) {
200   // This is very bad.
201   return -1;
202  }
203
204  if ( len ) {
205   have   = len;
206   buf   += len;
207   count -= len;
208  }
209 }
210
211 ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count);
212
213 if ( count == 0 )
214  return have;
215
216 ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count);
217
218 if ( (ret = roar_vio_read(self->next, buf, count)) == -1 )
219  return ret;
220
221 return have + ret;
222}
223
224ssize_t roar_vio_proto_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
225 struct roar_vio_proto * self = vio->inst;
226
227 return roar_vio_write(self->next, buf, count);
228}
229
230int     roar_vio_proto_sync    (struct roar_vio_calls * vio) {
231 struct roar_vio_proto * self = vio->inst;
232
233 return roar_vio_sync(self->next);
234}
235
236int     roar_vio_proto_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
237 struct roar_vio_proto * self = vio->inst;
238
239 if (vio == NULL || cmd == -1)
240  return -1;
241
242 ROAR_DBG("roar_vio_proto_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
243
244 switch (cmd) {
245  case ROAR_VIO_CTL_GET_NAME:
246    if ( data == NULL )
247     return -1;
248
249    switch (self->proto) {
250     case ROAR_VIO_PROTO_P_HTTP:
251       *(char**)data = "http";
252      break;
253     case ROAR_VIO_PROTO_P_GOPHER:
254       *(char**)data = "gopher";
255      break;
256     case ROAR_VIO_PROTO_P_ICY:
257       *(char**)data = "icy";
258      break;
259     default:
260       *(char**)data = "proto";
261      break;
262    }
263    return 0;
264   break;
265  case ROAR_VIO_CTL_GET_NEXT:
266    *(struct roar_vio_calls **)data = self->next;
267    return 0;
268   break;
269  case ROAR_VIO_CTL_SET_NEXT:
270    self->next = *(struct roar_vio_calls **)data;
271    return 0;
272   break;
273  case ROAR_VIO_CTL_GET_MIMETYPE:
274    if ( data == NULL )
275     return -1;
276
277    if ( self->content_type == NULL )
278     return -1;
279
280    *(char**)data = self->content_type;
281    return 0;
282   break;
283  case ROAR_VIO_CTL_GET_FH:
284  case ROAR_VIO_CTL_GET_SELECT_FH:
285    if ( self->reader.buffer == NULL && self->writer.buffer == NULL )
286     return roar_vio_ctl(self->next, cmd, data);
287    return -1;
288   break;
289  case ROAR_VIO_CTL_GET_READ_FH:
290  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
291    if ( self->reader.buffer == NULL )
292     return roar_vio_ctl(self->next, cmd, data);
293    return -1;
294   break;
295  case ROAR_VIO_CTL_GET_WRITE_FH:
296  case ROAR_VIO_CTL_GET_SELECT_WRITE_FH:
297    if ( self->writer.buffer == NULL )
298     return roar_vio_ctl(self->next, cmd, data);
299    return -1;
300   break;
301 }
302
303 return roar_vio_ctl(self->next, cmd, data);
304}
305
306int     roar_vio_proto_close   (struct roar_vio_calls * vio) {
307 struct roar_vio_proto * self = vio->inst;
308
309 if ( roar_vio_close(self->next) == -1 )
310  return -1;
311
312 if ( self->content_type != NULL )
313  roar_mm_free(self->content_type);
314
315 roar_mm_free(self);
316
317 return 0;
318}
319
320static int _parse_header(struct roar_keyval * kv, char ** buf, int * aligned, char * endofheader) {
321 char * p = *buf;
322 char   c = 0;
323
324 if ( !(*aligned) ) {
325  for (; *p != 0 && *p != '\r' && *p != '\n'; p++);
326  p++;
327  if ( *p == '\n' )
328   p++;
329 }
330
331 if ( p >= endofheader )
332  return -1;
333
334/*
335 if ( *p == '\r' || *p == '\n' )
336  return 0;
337 */
338
339 kv->key = p;
340
341 for (; *p != 0 && *p != '\r' && *p != '\n' && *p != ':'; p++);
342
343 if ( *p == 0 )
344  return -1;
345
346 if ( p >= endofheader )
347  return -1;
348
349 c = *p;
350 *p = 0;
351
352 if ( c == '\r' && *(p+1) == '\n' )
353  p++;
354
355 p++;
356
357 if ( c == '\r' || c == '\n' ) {
358  if ( *(kv->key) == '\r' || *(kv->key) == '\n' )
359   return 0;
360//  printf("Key-only\n");
361  kv->value = kv->key;
362  kv->key   = NULL;
363  *buf = p;
364  return 1;
365 }
366
367 for (; *p == ' '; p++);
368
369 if ( *p == 0 )
370  return -1;
371
372 kv->value = p;
373
374 for (; *p != 0 && *p != '\r' && *p != '\n'; p++);
375
376 if ( *p == 0 )
377  return -1;
378
379 if ( p >= endofheader )
380  return -1;
381
382 c = *p;
383 *p = 0;
384
385 if ( c == '\r' && *(p+1) == '\n' )
386  p++;
387
388 p++;
389
390 *buf = p;
391
392 if ( c == '\r' || c == '\n' ) {
393//  printf("aligned\n");
394  *aligned = 1;
395  p++;
396 } else {
397//  printf("non-aligned(c=0x%x)\n", (int)c);
398  *aligned = 0;
399 }
400
401 if ( *(kv->key) != 0 )
402  return 1;
403
404 return 0;
405}
406
407static void _handle_header (struct roar_vio_proto * self, struct roar_keyval * kv) {
408 ROAR_DBG("_handle_header(*): Header: key='%s', value='%s'", kv->key, kv->value);
409
410 if ( kv->key == NULL || kv->value == NULL )
411  return;
412
413 if ( !strcasecmp(kv->key, "Content-Type") ) {
414  if ( self->content_type != NULL )
415   roar_mm_free(self->content_type);
416
417  self->content_type = roar_mm_strdup(kv->value);
418 }
419}
420
421static inline char * _up2http_auth (struct roar_userpass * up) {
422 char * inbuf, * outbuf;
423 size_t inlen, outlen;
424 ssize_t ret;
425 //Authorization: Basic dXNlcjpwdw==
426
427 if ( up == NULL )
428  return NULL;
429
430 if ( up->subtype != -1 )
431  return NULL;
432
433 if ( up->user == NULL || up->pass == NULL )
434  return NULL;
435
436 inlen = roar_mm_strlen(up->user) + roar_mm_strlen(up->pass) + 2;
437 inbuf = roar_mm_malloc(inlen);
438 if ( inbuf == NULL )
439  return NULL;
440
441 inbuf[0] = 0;
442 roar_mm_strlcat(inbuf, up->user, inlen);
443 roar_mm_strlcat(inbuf, ":", inlen);
444 roar_mm_strlcat(inbuf, up->pass, inlen);
445
446 outlen = ((inlen * 3) / 2) + 3 /* padding... */ + 6 /* 'Basic ' */;
447 outbuf = roar_mm_malloc(outlen);
448 if ( outbuf == NULL ) {
449  roar_mm_free(inbuf);
450  return NULL;
451 }
452
453 strncpy(outbuf, "Basic ", 7);
454
455 ROAR_DBG("_up2http_auth(up=%p{.subtype=%i, .user='%s', .pass='%s'): inbuf='%s', outbuf='%s'", up, up->subtype, up->user, up->pass, inbuf, outbuf);
456 ret = roar_base64_encode(NULL, outbuf + 6, outlen - 6, inbuf, inlen - 1, NULL, 1);
457 ROAR_DBG("_up2http_auth(up=%p{.subtype=%i, .user='%s', .pass='%s'): inbuf='%s', outbuf='%s'", up, up->subtype, up->user, up->pass, inbuf, outbuf);
458
459 roar_mm_free(inbuf);
460
461 if ( ret == -1 ) {
462  roar_mm_free(outbuf);
463  return NULL;
464 }
465
466 return outbuf;
467}
468
469int roar_vio_open_proto_http   (struct roar_vio_calls * calls, struct roar_vio_calls * dst, const char * host, const char * file, struct roar_userpass * up) {
470 struct roar_keyval kv;
471 struct roar_vio_proto * self;
472 struct roar_buffer * bufbuf;
473 void * vpbuf;
474 char * authbuf;
475 char * buf;
476 char * endofheader = NULL;
477 char * p;
478 char b0[80], b1[80];
479 int  status;
480 int  len;
481 int  oeflen = 4;
482 int  aligned = 1;
483
484 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
485
486 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
487  return -1;
488
489 self         = calls->inst;
490 calls->write = NULL; // Disable write as we do not support this
491
492 if ( roar_buffer_new_data(&bufbuf, 1024, &vpbuf) == -1 )
493  return -1;
494
495 buf = vpbuf;
496
497 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
498
499 roar_vio_printf(dst, "GET /%s HTTP/1.1\r\n", file);
500 roar_vio_printf(dst, "Host: %s\r\n", host);
501 roar_vio_printf(dst, "User-Agent: roar_vio_open_proto_http() $Revision$\r\n");
502 roar_vio_printf(dst, "Connection: close\r\n");
503 if ( up != NULL ) {
504  if ( (authbuf = _up2http_auth(up)) != NULL ) {
505   roar_vio_printf(dst, "Authorization: %s\r\n", authbuf);
506   roar_mm_free(authbuf);
507  }
508 }
509 roar_vio_printf(dst, "\r\n");
510
511 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
512
513 roar_vio_sync(dst);
514
515 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
516
517 if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) {
518  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
519  roar_buffer_free(bufbuf);
520  return -1;
521 }
522
523 ROAR_DBG("roar_vio_open_proto_http(*): got %i bytes from server.", len);
524
525 buf[len] = 0;
526
527 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
528
529 if ( sscanf(buf, "%79s %i %79s\n", b0, &status, b1) != 3 ) {
530  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
531  roar_buffer_free(bufbuf);
532  return -1;
533 }
534
535 ROAR_DBG("roar_vio_open_proto_http(*): b0='%s'", b0);
536
537 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
538
539 if ( status != 200 ) {
540  ROAR_DBG("roar_vio_open_proto_http(*) = -1 // status=%i", status);
541  roar_buffer_free(bufbuf);
542  return -1;
543 }
544
545 ROAR_DBG("roar_vio_open_proto_http(*): status=%i", status);
546// ROAR_WARN("roar_vio_open_proto_http(*): buf='%s'", buf);
547
548 endofheader = strstr(buf, "\r\n\r\n");
549 if ( endofheader == NULL ) {
550  endofheader = strstr(buf, "\n\n");
551  oeflen = 2;
552 }
553
554 ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
555
556 p = buf;
557 while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 )
558  if ( aligned )
559   _handle_header(self, &kv);
560
561 while ( endofheader == NULL ) {
562  if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
563   return -1;
564
565  buf[len] = 0;
566  endofheader = strstr(buf, "\r\n\r\n");
567  if ( endofheader == NULL ) {
568   endofheader = strstr(buf, "\n\n");
569   oeflen = 2;
570  }
571
572/* Doesn't work good.
573  while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 )
574   if ( aligned )
575    _handle_header(self, &kv);
576*/
577
578  p = buf;
579  while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 )
580   if ( aligned )
581    _handle_header(self, &kv);
582
583  ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
584 }
585
586 ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
587 ROAR_DBG("roar_vio_open_proto_http(*): buf=%p\n", buf);
588
589 if ( (endofheader - buf) == (len - oeflen) ) {
590  roar_buffer_free(bufbuf);
591  bufbuf = NULL;
592 }
593
594 if ( bufbuf != NULL ) {
595  if ( roar_buffer_set_offset(bufbuf, endofheader - buf + oeflen) == -1 ||
596       roar_buffer_set_len(bufbuf,    len - (endofheader - buf + oeflen) - 0 /* ??? */) == -1 ) {
597   // TODO: FIXME: handle this in a better way.
598   ROAR_ERR("roar_vio_open_proto_http(*): Can not set data area of buffer %p, VERY BAD.", bufbuf);
599  }
600 }
601 self->reader.buffer = bufbuf;
602
603/*
604 if ( !strcmp((buf+len)-4, "\r\n\r\n") )
605  return 0;
606
607 while (*buf != '\r' && *buf != '\n') {
608  if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
609   return -1;
610 }
611*/
612
613 return 0;
614}
615
616int roar_vio_open_proto_gopher (struct roar_vio_calls * calls, struct roar_vio_calls * dst, const char * host, const char * file) {
617 struct roar_vio_proto * self;
618 char type;
619 const char * mime = NULL;
620
621 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
622  return -1;
623
624 self         = calls->inst;
625 calls->write = NULL; // Disable write as we do not support this
626
627 ROAR_DBG("roar_vio_open_proto_gopher(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
628
629 type = file[0];
630
631 file++;
632
633 ROAR_DBG("roar_vio_open_proto_gopher(*): type='%c'", type);
634
635 switch (type) {
636  case ROAR_GOPHER_TYPE_FILE:
637    mime = "text/plain";
638   break;
639  case ROAR_GOPHER_TYPE_DIR:
640    mime = "inode/directory";
641   break;
642  case ROAR_GOPHER_TYPE_BIN:
643    mime = "application/octet-stream";
644   break;
645  case ROAR_GOPHER_TYPE_GIF:
646    mime = "image/gif";
647   break;
648  case ROAR_GOPHER_TYPE_HTML:
649    mime = "text/html";
650   break;
651 }
652
653 if ( mime != NULL ) {
654  self->content_type = roar_mm_strdup(mime);
655 }
656
657 roar_vio_printf(dst, "%s\r\n", file);
658
659 roar_vio_sync(dst); // for encryption/compression layers
660
661 return 0;
662}
663#endif
664
665//ll
Note: See TracBrowser for help on using the repository browser.