source: roaraudio/libroar/vio_proto.c @ 1439:9c285e158bc5

Last change on this file since 1439:9c285e158bc5 was 1439:9c285e158bc5, checked in by phi, 15 years ago

make vio proto optional

File size: 5.5 KB
Line 
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
37int roar_vio_proto_init_def  (struct roar_vio_defaults * def, char * dstr, int proto, struct roar_vio_defaults * odef) {
38#ifndef ROAR_WITHOUT_VIO_PROTO
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
53 if ( dstr == NULL )
54  dstr = "//";
55
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
68 ROAR_WARN("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
69
70 ret = roar_vio_socket_init_dstr_def(def, dstr, -1, SOCK_STREAM, def);
71
72 ROAR_WARN("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
73
74 if ( ed != NULL )
75  *ed = '/';
76
77 return ret;
78#else
79 return -1;
80#endif
81}
82
83int roar_vio_open_proto      (struct roar_vio_calls * calls, struct roar_vio_calls * dst,
84                              char * dstr, int proto, struct roar_vio_defaults * odef) {
85#ifndef ROAR_WITHOUT_VIO_PROTO
86 char * host;
87 char * tmp;
88
89 ROAR_WARN("roar_vio_open_proto(calls=%p, dst=%p, dstr='%s', proto=%i, odef=%p) = ?", calls, dst, dstr, proto, odef);
90
91 if ( calls == NULL || dst == NULL || odef == NULL )
92  return -1;
93
94 ROAR_WARN("roar_vio_open_proto(*): odef->o_flags=%i", odef->o_flags);
95 ROAR_DBG("roar_vio_open_proto(*) = ?");
96
97 if ( roar_vio_open_pass(calls, dst) == -1 )
98  return -1;
99
100 ROAR_DBG("roar_vio_open_proto(*) = ?");
101
102 if ( dstr != NULL ) {
103  dstr += 2;
104  host  = dstr;
105
106  if ( (tmp = strstr(dstr, "/")) == NULL )
107   return -1;
108
109  *tmp++ = 0;
110  dstr   = tmp;
111
112  if ( (tmp = strstr(dstr, "#")) != NULL )
113   *tmp = 0;
114 } else {
115  ROAR_DBG("roar_vio_open_proto(*): no dstr!, odef->type=%i", odef->type);
116  if ( odef->type == ROAR_VIO_DEF_TYPE_FILE ) {
117   dstr = odef->d.file;
118   host = "localhost";
119
120   for (; *dstr == '/'; dstr++);
121
122  } else if ( odef->type == ROAR_VIO_DEF_TYPE_SOCKET ) {
123   dstr = ""; // index document
124   host = odef->d.socket.host;
125  } else {
126   return -1;
127  }
128 }
129
130 ROAR_DBG("roar_vio_open_proto(*) = ?");
131 ROAR_WARN("roar_vio_open_proto(*): proto=%i, host='%s', file='%s'", proto, host, dstr);
132
133 switch (proto) {
134  case ROAR_VIO_PROTO_P_HTTP:
135    return roar_vio_open_proto_http(calls, dst, host, dstr);
136   break;
137  case ROAR_VIO_PROTO_P_GOPHER:
138    return roar_vio_open_proto_gopher(calls, dst, host, dstr);
139   break;
140 }
141
142 return -1;
143#else
144 return -1;
145#endif
146}
147
148#ifndef ROAR_WITHOUT_VIO_PROTO
149int roar_vio_open_proto_http   (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) {
150 char buf[1024];
151 char b0[80], b1[80];
152 int  status;
153 int  len;
154
155 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
156  return -1;
157
158 roar_vio_printf(dst, "GET /%s HTTP/1.1\r\n", file);
159 roar_vio_printf(dst, "Host: %s\r\n", host);
160 roar_vio_printf(dst, "User-Agent: roar_vio_open_proto_http() $Revision$\r\n");
161 roar_vio_printf(dst, "Connection: close\r\n");
162 roar_vio_printf(dst, "\r\n");
163
164 roar_vio_sync(dst);
165
166 if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
167  return -1;
168
169 buf[len] = 0;
170
171 if ( sscanf(buf, "%79s %i %79s\n", b0, &status, b1) != 3 ) {
172  return -1;
173 }
174
175 if ( status != 200 )
176  return -1;
177
178 ROAR_WARN("roar_vio_open_proto_http(*): status=%i", status);
179// ROAR_WARN("roar_vio_open_proto_http(*): buf='%s'", buf);
180
181 if ( !strcmp((buf+len)-4, "\r\n\r\n") )
182  return 0;
183
184 while (*buf != '\r' && *buf != '\n') {
185  if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
186   return -1;
187 }
188
189 return 0;
190}
191
192int roar_vio_open_proto_gopher (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) {
193 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
194  return -1;
195
196 ROAR_DBG("roar_vio_open_proto_gopher(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
197
198 if ( file[1] == '/' )
199  file += 2;
200
201 roar_vio_printf(dst, "/%s\r\n", file);
202
203 roar_vio_sync(dst); // for encryption/compression layers
204
205 return 0;
206}
207#endif
208
209//ll
Note: See TracBrowser for help on using the repository browser.