file_http.cpp revision 1.1 1 1.1 uch /* $NetBSD: file_http.cpp,v 1.1 2001/02/09 18:34:38 uch Exp $ */
2 1.1 uch
3 1.1 uch /*-
4 1.1 uch * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * This code is derived from software contributed to The NetBSD Foundation
8 1.1 uch * by UCHIYAMA Yasushi.
9 1.1 uch *
10 1.1 uch * Redistribution and use in source and binary forms, with or without
11 1.1 uch * modification, are permitted provided that the following conditions
12 1.1 uch * are met:
13 1.1 uch * 1. Redistributions of source code must retain the above copyright
14 1.1 uch * notice, this list of conditions and the following disclaimer.
15 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 uch * notice, this list of conditions and the following disclaimer in the
17 1.1 uch * documentation and/or other materials provided with the distribution.
18 1.1 uch * 3. All advertising materials mentioning features or use of this software
19 1.1 uch * must display the following acknowledgement:
20 1.1 uch * This product includes software developed by the NetBSD
21 1.1 uch * Foundation, Inc. and its contributors.
22 1.1 uch * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 uch * contributors may be used to endorse or promote products derived
24 1.1 uch * from this software without specific prior written permission.
25 1.1 uch *
26 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 uch * POSSIBILITY OF SUCH DAMAGE.
37 1.1 uch */
38 1.1 uch
39 1.1 uch #include <file.h>
40 1.1 uch #include <file_http.h>
41 1.1 uch #include <console.h>
42 1.1 uch
43 1.1 uch static int __stricmp(const char *, const char *);
44 1.1 uch
45 1.1 uch HttpFile::HttpFile(Console *&cons)
46 1.1 uch : File(cons),
47 1.1 uch _req_get("GET "),
48 1.1 uch _req_head("HEAD "),
49 1.1 uch _req_host(" HTTP/1.0\r\nHOST: "),
50 1.1 uch _req_ua("\r\nUser-Agent: HPCBOOT/ZERO(1st impact; Windows CE; "
51 1.1 uch #if defined MIPS
52 1.1 uch "MIPS"
53 1.1 uch #elif defined ARM
54 1.1 uch "ARM"
55 1.1 uch #elif defined SH3
56 1.1 uch "SH3"
57 1.1 uch #elif defined SH4
58 1.1 uch "SH4"
59 1.1 uch #else
60 1.1 uch "Unknown"
61 1.1 uch #endif
62 1.1 uch ")\r\n\r\n")
63 1.1 uch {
64 1.1 uch _server_name[0] = '\0';
65 1.1 uch _debug = TRUE;
66 1.1 uch _memory_cache = TRUE;
67 1.1 uch // _memory_cache = FALSE; // not recomended.
68 1.1 uch _buffer = 0;
69 1.1 uch _reset_state();
70 1.1 uch DPRINTF((TEXT("File: HTTP\n")));
71 1.1 uch }
72 1.1 uch
73 1.1 uch HttpFile::~HttpFile(void)
74 1.1 uch {
75 1.1 uch if (_buffer)
76 1.1 uch free(_buffer);
77 1.1 uch WSACleanup();
78 1.1 uch }
79 1.1 uch
80 1.1 uch void
81 1.1 uch HttpFile::_reset_state(void)
82 1.1 uch {
83 1.1 uch _ascii_filename[0] = '\0';
84 1.1 uch _cached = FALSE;
85 1.1 uch if (_buffer)
86 1.1 uch free(_buffer);
87 1.1 uch _buffer = 0;
88 1.1 uch _header_size = 0;
89 1.1 uch _cur_pos = 0;
90 1.1 uch }
91 1.1 uch
92 1.1 uch BOOL
93 1.1 uch HttpFile::setRoot(TCHAR *server)
94 1.1 uch {
95 1.1 uch SOCKET h;
96 1.1 uch int ret, port;
97 1.1 uch
98 1.1 uch // parse server name and its port #
99 1.1 uch TCHAR sep[] = TEXT(":/");
100 1.1 uch TCHAR *token = wcstok(server, sep);
101 1.1 uch for (int i = 0; i < 3 && token; i++, token = wcstok(0, sep)) {
102 1.1 uch switch(i) {
103 1.1 uch case 0:
104 1.1 uch if (wcsicmp(token, TEXT("http"))) {
105 1.1 uch return FALSE;
106 1.1 uch }
107 1.1 uch break;
108 1.1 uch case 1:
109 1.1 uch if (!_to_ascii(_server_name, token, MAX_PATH))
110 1.1 uch return FALSE;
111 1.1 uch port = 80;
112 1.1 uch break;
113 1.1 uch case 2:
114 1.1 uch port = _wtoi(token);
115 1.1 uch break;
116 1.1 uch }
117 1.1 uch }
118 1.1 uch
119 1.1 uch ret = WSAStartup(MAKEWORD(1, 1), &_winsock);
120 1.1 uch if (ret != 0) {
121 1.1 uch DPRINTF((TEXT("WinSock initialize failed.\n")));
122 1.1 uch return FALSE;
123 1.1 uch }
124 1.1 uch if (LOBYTE(_winsock.wVersion) != 1 ||
125 1.1 uch HIBYTE(_winsock.wVersion) != 1) {
126 1.1 uch DPRINTF((TEXT("can't use WinSock DLL.\n")));
127 1.1 uch return FALSE;
128 1.1 uch }
129 1.1 uch
130 1.1 uch h = socket(AF_INET, SOCK_STREAM, 0);
131 1.1 uch if (h == INVALID_SOCKET) {
132 1.1 uch DPRINTF((TEXT("can't open socket. cause=%d\n"),
133 1.1 uch WSAGetLastError()));
134 1.1 uch return FALSE;
135 1.1 uch }
136 1.1 uch
137 1.1 uch struct hostent *entry = gethostbyname(_server_name);
138 1.1 uch if (entry == 0) {
139 1.1 uch DPRINTF((TEXT("can't get host by name.\n")));
140 1.1 uch return FALSE;
141 1.1 uch }
142 1.1 uch
143 1.1 uch memset(&_sockaddr, 0, sizeof(sockaddr_in));
144 1.1 uch _sockaddr.sin_family = AF_INET;
145 1.1 uch _sockaddr.sin_port = htons(port);
146 1.1 uch for (u_int8_t **addr_list =(u_int8_t **)entry->h_addr_list;
147 1.1 uch *addr_list; addr_list++) {
148 1.1 uch u_int8_t *b = &_sockaddr.sin_addr.S_un.S_un_b.s_b1;
149 1.1 uch for (int i = 0; i < 4; i++)
150 1.1 uch b[i] = addr_list[0][i];
151 1.1 uch
152 1.1 uch DPRINTF((TEXT("%d.%d.%d.%d "), b[0], b[1], b[2], b[3]));
153 1.1 uch if (connect(h,(const struct sockaddr *)&_sockaddr,
154 1.1 uch sizeof(struct sockaddr_in)) == 0)
155 1.1 uch goto connected;
156 1.1 uch }
157 1.1 uch DPRINTF((TEXT("can't connect server.\n")));
158 1.1 uch return FALSE;
159 1.1 uch
160 1.1 uch connected:
161 1.1 uch DPRINTF((TEXT(" \"%S\"\n"), _server_name));
162 1.1 uch closesocket(h);
163 1.1 uch
164 1.1 uch return TRUE;
165 1.1 uch }
166 1.1 uch
167 1.1 uch BOOL
168 1.1 uch HttpFile::open(const TCHAR *name, u_int32_t flag)
169 1.1 uch {
170 1.1 uch _reset_state();
171 1.1 uch return _to_ascii(_ascii_filename, name, MAX_PATH);
172 1.1 uch }
173 1.1 uch
174 1.1 uch size_t
175 1.1 uch HttpFile::_read_from_cache(void *buf, size_t bytes, off_t ofs)
176 1.1 uch {
177 1.1 uch size_t transfer = ofs + bytes > _buffer_size
178 1.1 uch ? _buffer_size - ofs : bytes;
179 1.1 uch memcpy(buf, &_buffer[ofs], transfer);
180 1.1 uch return transfer;
181 1.1 uch }
182 1.1 uch
183 1.1 uch BOOL
184 1.1 uch HttpFile::seek(off_t offset)
185 1.1 uch {
186 1.1 uch _cur_pos = offset;
187 1.1 uch
188 1.1 uch return TRUE;
189 1.1 uch }
190 1.1 uch
191 1.1 uch size_t
192 1.1 uch HttpFile::read(void *buf, size_t bytes, off_t offset)
193 1.1 uch {
194 1.1 uch char *b;
195 1.1 uch off_t ofs;
196 1.1 uch
197 1.1 uch if (offset != -1) {
198 1.1 uch ofs = offset;
199 1.1 uch } else {
200 1.1 uch ofs = _cur_pos;
201 1.1 uch _cur_pos += bytes;
202 1.1 uch }
203 1.1 uch
204 1.1 uch if (_memory_cache && _cached)
205 1.1 uch return _read_from_cache(buf, bytes, ofs);
206 1.1 uch
207 1.1 uch // HEAD request(get header size).
208 1.1 uch if (_header_size == 0)
209 1.1 uch _buffer_size = _parse_header(_header_size);
210 1.1 uch
211 1.1 uch // reconnect
212 1.1 uch Socket sock(_sockaddr);
213 1.1 uch SOCKET h;
214 1.1 uch if ((h = sock) == INVALID_SOCKET)
215 1.1 uch return 0;
216 1.1 uch
217 1.1 uch // GET request
218 1.1 uch strcpy(_request, _req_get);
219 1.1 uch _set_request();
220 1.1 uch send(h, _request, strlen(_request), 0);
221 1.1 uch
222 1.1 uch // skip header.
223 1.1 uch b = static_cast <char *>(malloc(_header_size));
224 1.1 uch _recv_buffer(h, b, _header_size);
225 1.1 uch free(b);
226 1.1 uch
227 1.1 uch // read contents.
228 1.1 uch size_t readed;
229 1.1 uch if (_memory_cache) {
230 1.1 uch _buffer = static_cast <char *>(malloc(_buffer_size));
231 1.1 uch _recv_buffer(h, _buffer, _buffer_size);
232 1.1 uch _cached = TRUE;
233 1.1 uch return _read_from_cache(buf, bytes, ofs);
234 1.1 uch } else {
235 1.1 uch int i, n = ofs / bytes;
236 1.1 uch b = static_cast <char *>(buf);
237 1.1 uch
238 1.1 uch for (readed = 0, i = 0; i < n; i++)
239 1.1 uch readed += _recv_buffer(h, b, bytes);
240 1.1 uch if ((n =(ofs % bytes)))
241 1.1 uch readed += _recv_buffer(h, b, n);
242 1.1 uch DPRINTF((TEXT("skip contents %d byte.\n"), readed));
243 1.1 uch
244 1.1 uch readed = _recv_buffer(h, b, bytes);
245 1.1 uch }
246 1.1 uch return readed;
247 1.1 uch }
248 1.1 uch
249 1.1 uch size_t
250 1.1 uch HttpFile::_parse_header(size_t &header_size)
251 1.1 uch {
252 1.1 uch size_t sz = 0;
253 1.1 uch // reconnect.
254 1.1 uch Socket sock(_sockaddr);
255 1.1 uch SOCKET h;
256 1.1 uch if ((h = sock) == INVALID_SOCKET)
257 1.1 uch return 0;
258 1.1 uch
259 1.1 uch // HEAD request
260 1.1 uch strcpy(_request, _req_head);
261 1.1 uch _set_request();
262 1.1 uch send(h, _request, strlen(_request), 0);
263 1.1 uch
264 1.1 uch // receive.
265 1.1 uch char __buf[TMP_BUFFER_SIZE];
266 1.1 uch int cnt, ret;
267 1.1 uch
268 1.1 uch for (cnt = 0;
269 1.1 uch ret = _recv_buffer(h, __buf, TMP_BUFFER_SIZE); cnt += ret) {
270 1.1 uch char sep[] = " :\r\n";
271 1.1 uch char *token = strtok(__buf, sep);
272 1.1 uch while (token) {
273 1.1 uch if (__stricmp(token, "content-length") == 0) {
274 1.1 uch token = strtok(0, sep);
275 1.1 uch sz = atoi(token);
276 1.1 uch DPRINTF((TEXT("content-length=%d\n"), sz));
277 1.1 uch } else
278 1.1 uch token = strtok(0, sep);
279 1.1 uch }
280 1.1 uch }
281 1.1 uch header_size = cnt;
282 1.1 uch DPRINTF((TEXT("header %d byte contents %d byte\n"), header_size, sz));
283 1.1 uch
284 1.1 uch return sz;
285 1.1 uch }
286 1.1 uch
287 1.1 uch size_t
288 1.1 uch HttpFile::_recv_buffer(SOCKET h, char *buf, size_t size)
289 1.1 uch {
290 1.1 uch size_t cnt, total = 0;
291 1.1 uch
292 1.1 uch do {
293 1.1 uch cnt = recv(h, buf + total, size - total, 0);
294 1.1 uch total += cnt;
295 1.1 uch DPRINTFN(2,(TEXT("size %d readed %d byte(+%d)\n"),
296 1.1 uch size, total, cnt));
297 1.1 uch } while (total < size && cnt > 0);
298 1.1 uch
299 1.1 uch
300 1.1 uch DPRINTFN(1,(TEXT("total read %d byte\n"), total));
301 1.1 uch return total;
302 1.1 uch }
303 1.1 uch
304 1.1 uch void
305 1.1 uch HttpFile::_set_request(void)
306 1.1 uch {
307 1.1 uch strcat(_request, _ascii_filename);
308 1.1 uch strcat(_request, _req_host);
309 1.1 uch strcat(_request, _server_name);
310 1.1 uch strcat(_request, _req_ua);
311 1.1 uch }
312 1.1 uch
313 1.1 uch static int
314 1.1 uch __stricmp(const char *s1, const char *s2)
315 1.1 uch {
316 1.1 uch const unsigned char *us1 =
317 1.1 uch reinterpret_cast <const unsigned char *>(s1);
318 1.1 uch const unsigned char *us2 =
319 1.1 uch reinterpret_cast <const unsigned char *>(s2);
320 1.1 uch
321 1.1 uch while (tolower(*us1) == tolower(*us2++))
322 1.1 uch if (*us1++ == '\0')
323 1.1 uch return 0;
324 1.1 uch return tolower(*us1) - tolower(*--us2);
325 1.1 uch }
326 1.1 uch
327 1.1 uch Socket::Socket(struct sockaddr_in &sock)
328 1.1 uch : _sockaddr(sock)
329 1.1 uch {
330 1.1 uch _socket = socket(AF_INET, SOCK_STREAM, 0);
331 1.1 uch if (_socket != INVALID_SOCKET)
332 1.1 uch connect(_socket,
333 1.1 uch reinterpret_cast <const struct sockaddr *>(&_sockaddr),
334 1.1 uch sizeof(struct sockaddr_in));
335 1.1 uch }
336 1.1 uch
337 1.1 uch Socket::~Socket(void)
338 1.1 uch {
339 1.1 uch if (_socket != INVALID_SOCKET)
340 1.1 uch closesocket(_socket);
341 1.1 uch }
342