source: roaraudio/libroar/vio_proto.c @ 1353:d3fff5536a2e

Last change on this file since 1353:d3fff5536a2e was 1353:d3fff5536a2e, checked in by phi, 15 years ago

we need to set access modes and init next->def in case of sockets, got tcp:...##gopher:##file:... working :)

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