Home | History | Annotate | Line # | Download | only in hpcboot
file_http.cpp revision 1.5.2.2
      1  1.5.2.2  jdolecek /*	$NetBSD: file_http.cpp,v 1.5.2.2 2002/03/16 15:57:50 jdolecek Exp $	*/
      2      1.1       uch 
      3      1.1       uch /*-
      4  1.5.2.1  jdolecek  * Copyright (c) 2001, 2002 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.2       uch 
     43  1.5.2.2  jdolecek // for WCE210 or earlier
     44  1.5.2.2  jdolecek _CRTIMP int __cdecl tolower(int);
     45      1.2       uch #define wcsicmp		_wcsicmp
     46  1.5.2.2  jdolecek static int WCE210_WSAStartup(WORD, LPWSADATA);
     47  1.5.2.2  jdolecek static int WCE210_WSACleanup(void);
     48      1.1       uch static int __stricmp(const char *, const char *);
     49      1.1       uch 
     50      1.1       uch HttpFile::HttpFile(Console *&cons)
     51      1.1       uch 	: File(cons),
     52      1.1       uch 	  _req_get("GET "),
     53      1.1       uch 	  _req_head("HEAD "),
     54      1.1       uch 	  _req_host(" HTTP/1.0\r\nHOST: "),
     55      1.1       uch 	  _req_ua("\r\nUser-Agent: HPCBOOT/ZERO(1st impact; Windows CE; "
     56      1.1       uch #if defined MIPS
     57      1.5       uch 	      "MIPS"
     58      1.1       uch #elif defined ARM
     59      1.5       uch 	      "ARM"
     60      1.1       uch #elif defined SH3
     61      1.5       uch 	      "SH3"
     62      1.1       uch #elif defined SH4
     63      1.5       uch 	      "SH4"
     64      1.1       uch #else
     65      1.5       uch 	      "Unknown"
     66      1.1       uch #endif
     67      1.5       uch 	      ")\r\n\r\n")
     68      1.1       uch {
     69  1.5.2.2  jdolecek 
     70      1.1       uch 	_server_name[0] = '\0';
     71  1.5.2.2  jdolecek 	_debug = 1;
     72      1.1       uch 	_memory_cache = TRUE;
     73      1.1       uch 	//    _memory_cache = FALSE; // not recomended.
     74      1.1       uch 	_buffer = 0;
     75      1.1       uch 	_reset_state();
     76  1.5.2.1  jdolecek 	DPRINTF((TEXT("FileManager: HTTP\n")));
     77  1.5.2.2  jdolecek 
     78  1.5.2.2  jdolecek 	if (WinCEVersion.dwMajorVersion > 3 ||
     79  1.5.2.2  jdolecek 	    (WinCEVersion.dwMajorVersion > 2) &&
     80  1.5.2.2  jdolecek 	    (WinCEVersion.dwMinorVersion >= 11)) {
     81  1.5.2.2  jdolecek 		_wsa_startup = WSAStartup;
     82  1.5.2.2  jdolecek 		_wsa_cleanup = WSACleanup;
     83  1.5.2.2  jdolecek 	} else {
     84  1.5.2.2  jdolecek 		_wsa_startup = WCE210_WSAStartup;
     85  1.5.2.2  jdolecek 		_wsa_cleanup = WCE210_WSACleanup;
     86  1.5.2.2  jdolecek 	}
     87  1.5.2.2  jdolecek }
     88  1.5.2.2  jdolecek 
     89  1.5.2.2  jdolecek int
     90  1.5.2.2  jdolecek WCE210_WSAStartup(WORD ver, LPWSADATA data)
     91  1.5.2.2  jdolecek {
     92  1.5.2.2  jdolecek 
     93  1.5.2.2  jdolecek 	data->wVersion		= ver;
     94  1.5.2.2  jdolecek 	data->wHighVersion	= ver;
     95  1.5.2.2  jdolecek 	data->szDescription[0]	= '\0';
     96  1.5.2.2  jdolecek 	data->szSystemStatus[0]	= '\0';
     97  1.5.2.2  jdolecek 	data->iMaxSockets	= 10;
     98  1.5.2.2  jdolecek 	data->iMaxUdpDg		= 0;
     99  1.5.2.2  jdolecek 	data->lpVendorInfo	= NULL;
    100  1.5.2.2  jdolecek 
    101  1.5.2.2  jdolecek 	return (0);
    102  1.5.2.2  jdolecek }
    103  1.5.2.2  jdolecek 
    104  1.5.2.2  jdolecek int
    105  1.5.2.2  jdolecek WCE210_WSACleanup()
    106  1.5.2.2  jdolecek {
    107  1.5.2.2  jdolecek 
    108  1.5.2.2  jdolecek 	return (0);
    109      1.1       uch }
    110      1.1       uch 
    111      1.1       uch HttpFile::~HttpFile(void)
    112      1.1       uch {
    113      1.1       uch 	if (_buffer)
    114      1.1       uch 		free(_buffer);
    115  1.5.2.2  jdolecek 	_wsa_cleanup();
    116      1.1       uch }
    117      1.1       uch 
    118      1.1       uch void
    119      1.1       uch HttpFile::_reset_state(void)
    120      1.1       uch {
    121      1.1       uch 	_ascii_filename[0] = '\0';
    122      1.1       uch 	_cached = FALSE;
    123      1.1       uch 	if (_buffer)
    124      1.1       uch 		free(_buffer);
    125      1.1       uch 	_buffer = 0;
    126      1.1       uch 	_header_size = 0;
    127      1.1       uch 	_cur_pos = 0;
    128      1.1       uch }
    129      1.1       uch 
    130      1.1       uch BOOL
    131      1.1       uch HttpFile::setRoot(TCHAR *server)
    132      1.1       uch {
    133      1.1       uch 	SOCKET h;
    134      1.1       uch 	int ret, port;
    135      1.1       uch 
    136      1.1       uch 	// parse server name and its port #
    137      1.1       uch 	TCHAR sep[] = TEXT(":/");
    138  1.5.2.2  jdolecek 
    139      1.1       uch 	TCHAR *token = wcstok(server, sep);
    140      1.1       uch 	for (int i = 0; i < 3 && token; i++, token = wcstok(0, sep)) {
    141      1.1       uch 		switch(i) {
    142      1.1       uch 		case 0:
    143      1.1       uch 			if (wcsicmp(token, TEXT("http"))) {
    144      1.1       uch 				return FALSE;
    145      1.1       uch 			}
    146      1.1       uch 			break;
    147      1.1       uch 		case 1:
    148      1.1       uch 			if (!_to_ascii(_server_name, token, MAX_PATH))
    149      1.1       uch 				return FALSE;
    150      1.1       uch 			port = 80;
    151      1.1       uch 			break;
    152      1.1       uch 		case 2:
    153      1.1       uch 			port = _wtoi(token);
    154      1.1       uch 			break;
    155      1.1       uch 		}
    156      1.1       uch 	}
    157      1.1       uch 
    158  1.5.2.2  jdolecek 	WORD version = MAKEWORD(1, 1);
    159  1.5.2.2  jdolecek 	ret = _wsa_startup(version, &_winsock);
    160      1.1       uch 	if (ret != 0) {
    161      1.1       uch 		DPRINTF((TEXT("WinSock initialize failed.\n")));
    162      1.1       uch 		return FALSE;
    163      1.1       uch 	}
    164      1.1       uch 	if (LOBYTE(_winsock.wVersion) != 1 ||
    165      1.1       uch 	    HIBYTE(_winsock.wVersion) != 1) {
    166      1.1       uch 		DPRINTF((TEXT("can't use WinSock DLL.\n")));
    167      1.1       uch 		return FALSE;
    168      1.1       uch 	}
    169      1.1       uch 
    170      1.1       uch 	h = socket(AF_INET, SOCK_STREAM, 0);
    171      1.1       uch 	if (h == INVALID_SOCKET) {
    172      1.1       uch 		DPRINTF((TEXT("can't open socket. cause=%d\n"),
    173      1.5       uch 		    WSAGetLastError()));
    174      1.1       uch 		return FALSE;
    175      1.1       uch 	}
    176      1.1       uch 
    177      1.1       uch 	memset(&_sockaddr, 0, sizeof(sockaddr_in));
    178      1.1       uch 	_sockaddr.sin_family = AF_INET;
    179      1.1       uch 	_sockaddr.sin_port = htons(port);
    180      1.4       uch 
    181      1.4       uch 	struct hostent *entry = gethostbyname(_server_name);
    182      1.4       uch 	if (entry == 0) {
    183      1.4       uch 		_sockaddr.sin_addr.S_un.S_addr = inet_addr(_server_name);
    184      1.4       uch 		if (_sockaddr.sin_addr.S_un.S_addr == INADDR_NONE) {
    185      1.4       uch 			DPRINTF((TEXT("can't get host by name.\n")));
    186      1.4       uch 			return FALSE;
    187      1.4       uch 		}
    188      1.1       uch 		u_int8_t *b = &_sockaddr.sin_addr.S_un.S_un_b.s_b1;
    189      1.1       uch 		DPRINTF((TEXT("%d.%d.%d.%d "), b[0], b[1], b[2], b[3]));
    190      1.1       uch 		if (connect(h,(const struct sockaddr *)&_sockaddr,
    191      1.5       uch 		    sizeof(struct sockaddr_in)) == 0)
    192      1.1       uch 			goto connected;
    193      1.4       uch 	} else {
    194      1.4       uch 		for (u_int8_t **addr_list =(u_int8_t **)entry->h_addr_list;
    195      1.5       uch 		    *addr_list; addr_list++) {
    196      1.4       uch 			u_int8_t *b = &_sockaddr.sin_addr.S_un.S_un_b.s_b1;
    197      1.4       uch 			for (int i = 0; i < 4; i++)
    198      1.4       uch 				b[i] = addr_list[0][i];
    199      1.4       uch 
    200      1.4       uch 			DPRINTF((TEXT("%d.%d.%d.%d "), b[0], b[1], b[2],b[3]));
    201      1.4       uch 			if (connect(h,(const struct sockaddr *)&_sockaddr,
    202      1.5       uch 			    sizeof(struct sockaddr_in)) == 0)
    203      1.4       uch 				goto connected;
    204      1.4       uch 		}
    205      1.1       uch 	}
    206      1.1       uch 	DPRINTF((TEXT("can't connect server.\n")));
    207      1.1       uch 	return FALSE;
    208      1.1       uch 
    209      1.1       uch  connected:
    210  1.5.2.1  jdolecek 	DPRINTF((TEXT("(%S) connected.\n"), _server_name));
    211      1.1       uch 	closesocket(h);
    212      1.1       uch 
    213      1.1       uch 	return TRUE;
    214      1.1       uch }
    215      1.1       uch 
    216      1.1       uch BOOL
    217      1.1       uch HttpFile::open(const TCHAR *name, u_int32_t flag)
    218      1.1       uch {
    219  1.5.2.1  jdolecek 
    220      1.1       uch 	_reset_state();
    221  1.5.2.1  jdolecek 
    222      1.1       uch 	return _to_ascii(_ascii_filename, name, MAX_PATH);
    223      1.1       uch }
    224      1.1       uch 
    225      1.1       uch size_t
    226      1.1       uch HttpFile::_read_from_cache(void *buf, size_t bytes, off_t ofs)
    227      1.1       uch {
    228      1.3       uch 	size_t transfer;
    229      1.3       uch 
    230      1.3       uch 	if (ofs >= _buffer_size)
    231      1.3       uch 		return 0;
    232      1.3       uch 
    233      1.3       uch 	transfer = ofs + bytes > _buffer_size ? _buffer_size - ofs : bytes;
    234      1.3       uch 
    235      1.1       uch 	memcpy(buf, &_buffer[ofs], transfer);
    236      1.3       uch 
    237      1.1       uch 	return transfer;
    238      1.1       uch }
    239      1.1       uch 
    240      1.1       uch BOOL
    241      1.1       uch HttpFile::seek(off_t offset)
    242      1.1       uch {
    243      1.1       uch 	_cur_pos = offset;
    244      1.1       uch 
    245      1.1       uch 	return TRUE;
    246      1.1       uch }
    247      1.1       uch 
    248      1.1       uch size_t
    249      1.1       uch HttpFile::read(void *buf, size_t bytes, off_t offset)
    250      1.1       uch {
    251      1.1       uch 	char *b;
    252      1.1       uch 	off_t ofs;
    253      1.1       uch 
    254      1.1       uch 	if (offset != -1) {
    255      1.1       uch 		ofs = offset;
    256      1.1       uch 	} else {
    257      1.1       uch 		ofs = _cur_pos;
    258      1.1       uch 		_cur_pos += bytes;
    259      1.1       uch 	}
    260      1.1       uch 
    261      1.1       uch 	if (_memory_cache && _cached)
    262      1.1       uch 		return _read_from_cache(buf, bytes, ofs);
    263      1.1       uch 
    264      1.1       uch 	// HEAD request(get header size).
    265      1.1       uch 	if (_header_size == 0)
    266      1.1       uch 		_buffer_size = _parse_header(_header_size);
    267      1.1       uch 
    268      1.1       uch 	// reconnect
    269      1.1       uch 	Socket sock(_sockaddr);
    270      1.1       uch 	SOCKET h;
    271      1.1       uch 	if ((h = sock) == INVALID_SOCKET)
    272      1.1       uch 		return 0;
    273      1.1       uch 
    274      1.1       uch 	// GET request
    275      1.1       uch 	strcpy(_request, _req_get);
    276      1.1       uch 	_set_request();
    277      1.1       uch 	send(h, _request, strlen(_request), 0);
    278      1.1       uch 
    279      1.1       uch 	// skip header.
    280      1.1       uch 	b = static_cast <char *>(malloc(_header_size));
    281      1.1       uch 	_recv_buffer(h, b, _header_size);
    282      1.1       uch 	free(b);
    283      1.1       uch 
    284      1.1       uch 	// read contents.
    285      1.1       uch 	size_t readed;
    286      1.1       uch 	if (_memory_cache) {
    287      1.1       uch 		_buffer = static_cast <char *>(malloc(_buffer_size));
    288      1.1       uch 		_recv_buffer(h, _buffer, _buffer_size);
    289      1.1       uch 		_cached = TRUE;
    290      1.1       uch 		return _read_from_cache(buf, bytes, ofs);
    291      1.1       uch 	} else {
    292      1.1       uch 		int i, n = ofs / bytes;
    293      1.1       uch 		b = static_cast <char *>(buf);
    294      1.1       uch 
    295      1.1       uch 		for (readed = 0, i = 0; i < n; i++)
    296      1.1       uch 			readed += _recv_buffer(h, b, bytes);
    297      1.1       uch 		if ((n =(ofs % bytes)))
    298      1.1       uch 			readed += _recv_buffer(h, b, n);
    299      1.1       uch 		DPRINTF((TEXT("skip contents %d byte.\n"), readed));
    300      1.1       uch 
    301      1.1       uch 		readed = _recv_buffer(h, b, bytes);
    302      1.1       uch 	}
    303      1.1       uch 	return readed;
    304      1.1       uch }
    305      1.1       uch 
    306      1.1       uch size_t
    307      1.1       uch HttpFile::_parse_header(size_t &header_size)
    308      1.1       uch {
    309      1.1       uch 	size_t sz = 0;
    310      1.1       uch 	// reconnect.
    311      1.1       uch 	Socket sock(_sockaddr);
    312      1.1       uch 	SOCKET h;
    313      1.1       uch 	if ((h = sock) == INVALID_SOCKET)
    314      1.1       uch 		return 0;
    315      1.1       uch 
    316      1.1       uch 	// HEAD request
    317      1.1       uch 	strcpy(_request, _req_head);
    318      1.1       uch 	_set_request();
    319      1.1       uch 	send(h, _request, strlen(_request), 0);
    320      1.1       uch 
    321      1.1       uch 	// receive.
    322      1.1       uch 	char __buf[TMP_BUFFER_SIZE];
    323      1.1       uch 	int cnt, ret;
    324      1.1       uch 
    325      1.1       uch 	for (cnt = 0;
    326      1.5       uch 	    ret = _recv_buffer(h, __buf, TMP_BUFFER_SIZE); cnt += ret) {
    327      1.1       uch 		char sep[] = " :\r\n";
    328      1.1       uch 		char *token = strtok(__buf, sep);
    329      1.1       uch 		while (token) {
    330      1.1       uch 			if (__stricmp(token, "content-length") == 0) {
    331  1.5.2.2  jdolecek 				DPRINTFN(1, (TEXT("*token: %S\n"), token));
    332      1.1       uch 				token = strtok(0, sep);
    333      1.1       uch 				sz = atoi(token);
    334  1.5.2.2  jdolecek 				DPRINTFN(1, (TEXT("*content-length=%d\n"), sz));
    335      1.1       uch 			} else
    336      1.1       uch 				token = strtok(0, sep);
    337      1.1       uch 		}
    338      1.1       uch 	}
    339      1.1       uch 	header_size = cnt;
    340  1.5.2.1  jdolecek 
    341  1.5.2.1  jdolecek 	DPRINTF((TEXT
    342  1.5.2.1  jdolecek 	    ("open file http://%S%S - header %d byte contents %d byte\n"),
    343  1.5.2.1  jdolecek 	    _server_name, _ascii_filename, header_size, sz));
    344      1.1       uch 
    345      1.1       uch 	return sz;
    346      1.1       uch }
    347      1.1       uch 
    348      1.1       uch size_t
    349      1.1       uch HttpFile::_recv_buffer(SOCKET h, char *buf, size_t size)
    350      1.1       uch {
    351      1.1       uch 	size_t cnt, total = 0;
    352      1.1       uch 
    353      1.1       uch 	do {
    354      1.1       uch 		cnt = recv(h, buf + total, size - total, 0);
    355      1.1       uch 		total += cnt;
    356      1.1       uch 		DPRINTFN(2,(TEXT("size %d readed %d byte(+%d)\n"),
    357      1.5       uch 		    size, total, cnt));
    358      1.1       uch 	} while (total < size && cnt > 0);
    359      1.1       uch 
    360      1.1       uch 
    361      1.1       uch 	DPRINTFN(1,(TEXT("total read %d byte\n"), total));
    362      1.1       uch 	return total;
    363      1.1       uch }
    364      1.1       uch 
    365      1.1       uch void
    366      1.1       uch HttpFile::_set_request(void)
    367      1.1       uch {
    368  1.5.2.1  jdolecek 
    369      1.1       uch 	strcat(_request, _ascii_filename);
    370      1.1       uch 	strcat(_request, _req_host);
    371      1.1       uch 	strcat(_request, _server_name);
    372      1.1       uch 	strcat(_request, _req_ua);
    373      1.1       uch }
    374      1.1       uch 
    375      1.1       uch static int
    376      1.1       uch __stricmp(const char *s1, const char *s2)
    377      1.1       uch {
    378  1.5.2.2  jdolecek 
    379  1.5.2.2  jdolecek 	while (tolower(*s1) == tolower(*s2++))
    380  1.5.2.2  jdolecek 		if (*s1++ == '\0')
    381  1.5.2.2  jdolecek 			return (0);
    382  1.5.2.2  jdolecek 	return (tolower(*s1) - tolower(*--s2));
    383      1.1       uch }
    384      1.1       uch 
    385      1.1       uch Socket::Socket(struct sockaddr_in &sock)
    386      1.1       uch 	: _sockaddr(sock)
    387      1.1       uch {
    388  1.5.2.1  jdolecek 
    389      1.1       uch 	_socket = socket(AF_INET, SOCK_STREAM, 0);
    390      1.1       uch 	if (_socket != INVALID_SOCKET)
    391      1.1       uch 		connect(_socket,
    392      1.5       uch 		    reinterpret_cast <const struct sockaddr *>(&_sockaddr),
    393      1.5       uch 		    sizeof(struct sockaddr_in));
    394      1.1       uch }
    395      1.1       uch 
    396      1.1       uch Socket::~Socket(void)
    397      1.1       uch {
    398  1.5.2.1  jdolecek 
    399      1.1       uch 	if (_socket != INVALID_SOCKET)
    400      1.1       uch 		closesocket(_socket);
    401      1.1       uch }
    402