source: roaraudio/libroar/vio_proto.c @ 1352:32e2b93fd9b5

Last change on this file since 1352:32e2b93fd9b5 was 1352:32e2b93fd9b5, checked in by phi, 15 years ago

corrected gopher URL schema and added basic HTTP code, not working on broken httpds

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