source: roaraudio/libroar/vio_proto.c @ 5276:0eb24ca6810e

Last change on this file since 5276:0eb24ca6810e was 5276:0eb24ca6810e, checked in by phi, 12 years ago

merged VIO's _nonblock() into _ctl() (Closes: #135)

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