00001
00002
00003
00004
00005
00006
00007
00008
00009
00014 #ifdef ENABLE_NETWORK
00015
00016 #include "../../stdafx.h"
00017 #include "../../debug.h"
00018 #include "../../rev.h"
00019 #include "../network_func.h"
00020
00021 #include "tcp_http.h"
00022
00024 static SmallVector<NetworkHTTPSocketHandler *, 1> _http_connections;
00025
00026 NetworkHTTPSocketHandler::NetworkHTTPSocketHandler(SOCKET s,
00027 HTTPCallback *callback, const char *host, const char *url,
00028 const char *data, int depth) :
00029 NetworkSocketHandler(),
00030 recv_pos(0),
00031 recv_length(0),
00032 callback(callback),
00033 data(data),
00034 redirect_depth(depth),
00035 sock(s)
00036 {
00037 size_t bufferSize = strlen(url) + strlen(host) + strlen(_openttd_revision) + (data == NULL ? 0 : strlen(data)) + 128;
00038 char *buffer = AllocaM(char, bufferSize);
00039
00040 DEBUG(net, 7, "[tcp/http] requesting %s%s", host, url);
00041 if (data != NULL) {
00042 seprintf(buffer, buffer + bufferSize - 1, "POST %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: OpenTTD/%s\r\nContent-Type: text/plain\r\nContent-Length: %d\r\n\r\n%s\r\n", url, host, _openttd_revision, (int)strlen(data), data);
00043 } else {
00044 seprintf(buffer, buffer + bufferSize - 1, "GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: OpenTTD/%s\r\n\r\n", url, host, _openttd_revision);
00045 }
00046
00047 ssize_t size = strlen(buffer);
00048 ssize_t res = send(this->sock, (const char*)buffer, size, 0);
00049 if (res != size) {
00050
00051
00052 this->callback->OnFailure();
00053 delete this;
00054 return;
00055 }
00056
00057 *_http_connections.Append() = this;
00058 }
00059
00060 NetworkHTTPSocketHandler::~NetworkHTTPSocketHandler()
00061 {
00062 this->CloseConnection();
00063
00064 if (this->sock != INVALID_SOCKET) closesocket(this->sock);
00065 this->sock = INVALID_SOCKET;
00066 free((void*)this->data);
00067 }
00068
00069 NetworkRecvStatus NetworkHTTPSocketHandler::CloseConnection(bool error)
00070 {
00071 NetworkSocketHandler::CloseConnection(error);
00072 return NETWORK_RECV_STATUS_OKAY;
00073 }
00074
00079 #define return_error(msg) { DEBUG(net, 0, msg); return -1; }
00080
00081 static const char * const NEWLINE = "\r\n";
00082 static const char * const END_OF_HEADER = "\r\n\r\n";
00083 static const char * const HTTP_1_0 = "HTTP/1.0 ";
00084 static const char * const HTTP_1_1 = "HTTP/1.1 ";
00085 static const char * const CONTENT_LENGTH = "Content-Length: ";
00086 static const char * const LOCATION = "Location: ";
00087
00098 int NetworkHTTPSocketHandler::HandleHeader()
00099 {
00100 assert(strlen(HTTP_1_0) == strlen(HTTP_1_1));
00101 assert(strstr(this->recv_buffer, END_OF_HEADER) != NULL);
00102
00103
00104 if (strncmp(this->recv_buffer, HTTP_1_0, strlen(HTTP_1_0)) != 0 &&
00105 strncmp(this->recv_buffer, HTTP_1_1, strlen(HTTP_1_1)) != 0) {
00106 return_error("[tcp/http] received invalid HTTP reply");
00107 }
00108
00109 char *status = this->recv_buffer + strlen(HTTP_1_0);
00110 if (strncmp(status, "200", 3) == 0) {
00111
00112
00113
00114 char *length = strcasestr(this->recv_buffer, CONTENT_LENGTH);
00115 if (length == NULL) return_error("[tcp/http] missing 'content-length' header");
00116
00117
00118 length += strlen(CONTENT_LENGTH);
00119
00120
00121
00122 char *end_of_line = strstr(length, NEWLINE);
00123
00124
00125 *end_of_line = '\0';
00126 int len = atoi(length);
00127
00128 *end_of_line = '\r';
00129
00130
00131
00132
00133 if (len == 0) return_error("[tcp/http] refusing to download 0 bytes");
00134
00135 DEBUG(net, 7, "[tcp/http] downloading %i bytes", len);
00136 return len;
00137 }
00138
00139 if (strncmp(status, "301", 3) != 0 &&
00140 strncmp(status, "302", 3) != 0 &&
00141 strncmp(status, "303", 3) != 0 &&
00142 strncmp(status, "307", 3) != 0) {
00143
00144
00145
00146
00147 *strstr(status, NEWLINE) = '\0';
00148 DEBUG(net, 0, "[tcp/http] unhandled status reply %s", status);
00149 return -1;
00150 }
00151
00152 if (this->redirect_depth == 5) return_error("[tcp/http] too many redirects, looping redirects?");
00153
00154
00155 char *uri = strcasestr(this->recv_buffer, LOCATION);
00156 if (uri == NULL) return_error("[tcp/http] missing 'location' header for redirect");
00157
00158 uri += strlen(LOCATION);
00159
00160
00161
00162 char *end_of_line = strstr(uri, NEWLINE);
00163 *end_of_line = '\0';
00164
00165 DEBUG(net, 6, "[tcp/http] redirecting to %s", uri);
00166
00167 int ret = NetworkHTTPSocketHandler::Connect(uri, this->callback, this->data, this->redirect_depth + 1);
00168 if (ret != 0) return ret;
00169
00170
00171 this->data = NULL;
00172
00173
00174 *end_of_line = '\r';
00175 return 0;
00176 }
00177
00178 int NetworkHTTPSocketHandler::Connect(char *uri, HTTPCallback *callback, const char *data, int depth)
00179 {
00180 char *hname = strstr(uri, "://");
00181 if (hname == NULL) return_error("[tcp/http] invalid location");
00182
00183 hname += 3;
00184
00185 char *url = strchr(hname, '/');
00186 if (url == NULL) return_error("[tcp/http] invalid location");
00187
00188 *url = '\0';
00189
00190
00191 const char *company = NULL;
00192 const char *port = NULL;
00193 ParseConnectionString(&company, &port, hname);
00194 if (company != NULL) return_error("[tcp/http] invalid hostname");
00195
00196 NetworkAddress address(hname, port == NULL ? 80 : atoi(port));
00197
00198
00199 *url = '/';
00200 new NetworkHTTPContentConnecter(address, callback, url, data, depth);
00201 return 0;
00202 }
00203
00204 #undef return_error
00205
00213 int NetworkHTTPSocketHandler::Receive()
00214 {
00215 for (;;) {
00216 ssize_t res = recv(this->sock, (char *)this->recv_buffer + this->recv_pos, lengthof(this->recv_buffer) - this->recv_pos, 0);
00217 if (res == -1) {
00218 int err = GET_LAST_ERROR();
00219 if (err != EWOULDBLOCK) {
00220
00221 if (err != 104) DEBUG(net, 0, "recv failed with error %d", err);
00222 return -1;
00223 }
00224
00225 return 1;
00226 }
00227
00228
00229 if (res == 0) {
00230 if (this->recv_length != 0) return -1;
00231
00232 this->callback->OnReceiveData(NULL, 0);
00233 return 0;
00234 }
00235
00236
00237 if (this->recv_length == 0) {
00238 int read = this->recv_pos + res;
00239 int end = min(read, lengthof(this->recv_buffer) - 1);
00240
00241
00242 char prev = this->recv_buffer[end];
00243 this->recv_buffer[end] = '\0';
00244 char *end_of_header = strstr(this->recv_buffer, END_OF_HEADER);
00245 this->recv_buffer[end] = prev;
00246
00247 if (end_of_header == NULL) {
00248 if (read == lengthof(this->recv_buffer)) {
00249 DEBUG(net, 0, "[tcp/http] header too big");
00250 return -1;
00251 }
00252 this->recv_pos = read;
00253 } else {
00254 int ret = this->HandleHeader();
00255 if (ret <= 0) return ret;
00256
00257 this->recv_length = ret;
00258
00259 end_of_header += strlen(END_OF_HEADER);
00260 int len = min(read - (end_of_header - this->recv_buffer), res);
00261 if (len != 0) {
00262 this->callback->OnReceiveData(end_of_header, len);
00263 this->recv_length -= len;
00264 }
00265
00266 this->recv_pos = 0;
00267 }
00268 } else {
00269 res = min(this->recv_length, res);
00270
00271 this->callback->OnReceiveData(this->recv_buffer, res);
00272 this->recv_length -= res;
00273 }
00274 }
00275 }
00276
00277 void NetworkHTTPSocketHandler::HTTPReceive()
00278 {
00279
00280 if (_http_connections.Length() == 0) return;
00281
00282 fd_set read_fd;
00283 struct timeval tv;
00284
00285 FD_ZERO(&read_fd);
00286 for (NetworkHTTPSocketHandler **iter = _http_connections.Begin(); iter < _http_connections.End(); iter++) {
00287 FD_SET((*iter)->sock, &read_fd);
00288 }
00289
00290 tv.tv_sec = tv.tv_usec = 0;
00291 #if !defined(__MORPHOS__) && !defined(__AMIGA__)
00292 int n = select(FD_SETSIZE, &read_fd, NULL, NULL, &tv);
00293 #else
00294 int n = WaitSelect(FD_SETSIZE, &read_fd, NULL, NULL, &tv, NULL);
00295 #endif
00296 if (n == -1) return;
00297
00298 for (NetworkHTTPSocketHandler **iter = _http_connections.Begin(); iter < _http_connections.End(); ) {
00299 NetworkHTTPSocketHandler *cur = *iter;
00300
00301 if (FD_ISSET(cur->sock, &read_fd)) {
00302 int ret = cur->Receive();
00303
00304 if (ret < 0) cur->callback->OnFailure();
00305 if (ret <= 0) {
00306
00307 cur->CloseConnection();
00308 _http_connections.Erase(iter);
00309 delete cur;
00310 continue;
00311 }
00312 }
00313 iter++;
00314 }
00315 }
00316
00317 #endif