2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2003-07-02 02:02:18 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
2003-07-02 02:02:18 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-10-16 19:03:49 +00:00
|
|
|
#include "cpp.h"
|
2002-06-06 18:50:12 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2005-03-07 06:09:04 +00:00
|
|
|
#else
|
2004-03-04 11:41:43 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <sstream>
|
|
|
|
#include <algorithm>
|
2002-07-22 23:07:14 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/socket.h>
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cerrno>
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <unistd.h>
|
2002-06-06 18:50:12 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
#include "error_numbers.h"
|
|
|
|
#include "filesys.h"
|
2003-03-08 23:48:05 +00:00
|
|
|
#include "util.h"
|
2005-03-07 06:09:04 +00:00
|
|
|
#include "network.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2005-03-07 06:09:04 +00:00
|
|
|
#include "client_msgs.h"
|
2003-07-03 05:01:29 +00:00
|
|
|
#include "http.h"
|
2004-06-13 23:17:33 +00:00
|
|
|
#include "base64.h"
|
2003-07-03 05:01:29 +00:00
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
#define HTTP_BLOCKSIZE 16384
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2004-06-30 18:17:21 +00:00
|
|
|
using std::string;
|
|
|
|
using std::istringstream;
|
|
|
|
using std::vector;
|
2004-08-20 19:50:13 +00:00
|
|
|
using std::getline;
|
2004-06-13 04:47:20 +00:00
|
|
|
|
2005-02-23 06:19:20 +00:00
|
|
|
// Breaks a HTTP URL down into its server, port and file components
|
|
|
|
// format of url:
|
|
|
|
// [http://]host.dom.dom[:port][/dir/file]
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2003-08-22 00:15:51 +00:00
|
|
|
void parse_url(const char* url, char* host, int &port, char* file) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char* p;
|
|
|
|
char buf[256];
|
2002-08-15 22:03:41 +00:00
|
|
|
|
2005-02-23 06:19:20 +00:00
|
|
|
// strip off http:// if present
|
|
|
|
//
|
2003-03-19 01:15:46 +00:00
|
|
|
if (strncmp(url, "http://", 7) == 0) {
|
2003-04-03 18:35:40 +00:00
|
|
|
safe_strcpy(buf, url+7);
|
2003-03-19 01:15:46 +00:00
|
|
|
} else {
|
2003-04-03 18:35:40 +00:00
|
|
|
safe_strcpy(buf, url);
|
2003-03-19 01:15:46 +00:00
|
|
|
}
|
2005-02-23 06:19:20 +00:00
|
|
|
|
|
|
|
// parse and strip off file part if present
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
p = strchr(buf, '/');
|
|
|
|
if (p) {
|
|
|
|
strcpy(file, p+1);
|
|
|
|
*p = 0;
|
|
|
|
} else {
|
2003-03-19 01:15:46 +00:00
|
|
|
strcpy(file, "");
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2005-02-23 06:19:20 +00:00
|
|
|
|
|
|
|
// parse and strip off port if present
|
|
|
|
//
|
|
|
|
p = strchr(buf,':');
|
2002-12-09 03:07:36 +00:00
|
|
|
if (p) {
|
|
|
|
port = atol(p+1);
|
|
|
|
*p = 0;
|
|
|
|
} else {
|
|
|
|
port=80;
|
|
|
|
}
|
2005-02-23 06:19:20 +00:00
|
|
|
|
|
|
|
// what remains is the host
|
|
|
|
//
|
2003-03-19 01:15:46 +00:00
|
|
|
strcpy(host, buf);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2005-02-03 18:49:38 +00:00
|
|
|
void get_user_agent_string(char* p) {
|
|
|
|
sprintf(p, "BOINC client (%s %d.%02d)",
|
|
|
|
HOSTTYPE, BOINC_MAJOR_VERSION, BOINC_MINOR_VERSION
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2005-03-09 20:51:15 +00:00
|
|
|
// create an HTTP 1.0 GET request header
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2002-07-05 05:33:40 +00:00
|
|
|
static void http_get_request_header(
|
2002-12-09 03:07:36 +00:00
|
|
|
char* buf, char* host, int port, char* file, double offset
|
2002-07-05 05:33:40 +00:00
|
|
|
) {
|
2002-12-09 03:07:36 +00:00
|
|
|
char offset_info[256];
|
2005-02-03 18:49:38 +00:00
|
|
|
char user_agent_string[256];
|
|
|
|
get_user_agent_string(user_agent_string);
|
2002-12-09 03:07:36 +00:00
|
|
|
if (offset) sprintf( offset_info, "Range: bytes=%.0f-\015\012", offset );
|
|
|
|
sprintf(buf,
|
2003-05-10 00:52:36 +00:00
|
|
|
"GET %s HTTP/1.0\015\012"
|
2005-02-03 18:49:38 +00:00
|
|
|
"User-Agent: %s\015\012"
|
2002-12-09 03:07:36 +00:00
|
|
|
"Host: %s:%d\015\012"
|
|
|
|
"%s"
|
|
|
|
"Connection: close\015\012"
|
|
|
|
"Accept: */*\015\012"
|
|
|
|
"\015\012",
|
2005-02-02 23:55:00 +00:00
|
|
|
file,
|
2005-02-03 18:49:38 +00:00
|
|
|
user_agent_string,
|
2005-02-02 23:55:00 +00:00
|
|
|
host, port, offset?offset_info:""
|
2002-12-09 03:07:36 +00:00
|
|
|
);
|
2002-06-10 06:14:18 +00:00
|
|
|
}
|
|
|
|
|
2004-06-13 04:47:20 +00:00
|
|
|
static void http_get_request_header_proxy(
|
2005-02-16 23:17:43 +00:00
|
|
|
char* buf, char* host, int port, char* file, double offset, const char* encstr
|
2004-06-13 04:47:20 +00:00
|
|
|
) {
|
|
|
|
char offset_info[256];
|
2005-02-03 18:49:38 +00:00
|
|
|
char user_agent_string[256];
|
|
|
|
get_user_agent_string(user_agent_string);
|
2004-06-13 04:47:20 +00:00
|
|
|
if (offset) sprintf( offset_info, "Range: bytes=%.0f-\015\012", offset );
|
|
|
|
sprintf(buf,
|
|
|
|
"GET %s HTTP/1.0\015\012"
|
2005-02-03 18:49:38 +00:00
|
|
|
"User-Agent: %s\015\012"
|
2004-06-13 04:47:20 +00:00
|
|
|
"Host: %s:%d\015\012"
|
|
|
|
"%s"
|
|
|
|
"Connection: close\015\012"
|
|
|
|
"Accept: */*\015\012"
|
2004-12-10 19:17:32 +00:00
|
|
|
"Proxy-Authorization: Basic %s\015\012"
|
2004-06-13 04:47:20 +00:00
|
|
|
"\015\012",
|
2005-02-03 14:58:33 +00:00
|
|
|
file,
|
2005-02-03 18:49:38 +00:00
|
|
|
user_agent_string,
|
2005-02-03 14:58:33 +00:00
|
|
|
host, port, offset?offset_info:"", encstr
|
2004-06-13 04:47:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2005-03-09 20:51:15 +00:00
|
|
|
// create an HTTP 1.0 HEAD request header
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2005-02-03 18:49:38 +00:00
|
|
|
static void http_head_request_header(
|
|
|
|
char* buf, char* host, int port, char* file
|
|
|
|
) {
|
|
|
|
char user_agent_string[256];
|
|
|
|
get_user_agent_string(user_agent_string);
|
2002-04-30 22:22:54 +00:00
|
|
|
sprintf(buf,
|
2003-05-10 00:52:36 +00:00
|
|
|
"HEAD %s HTTP/1.0\015\012"
|
2005-02-03 18:49:38 +00:00
|
|
|
"User-Agent: %s\015\012"
|
2002-12-09 03:07:36 +00:00
|
|
|
"Host: %s:%d\015\012"
|
2002-08-12 21:54:19 +00:00
|
|
|
"Connection: close\015\012"
|
2002-04-30 22:22:54 +00:00
|
|
|
"Accept: */*\015\012"
|
|
|
|
"\015\012",
|
2005-02-03 18:49:38 +00:00
|
|
|
file, user_agent_string, host, port
|
2002-04-30 22:22:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2005-02-03 18:49:38 +00:00
|
|
|
static void http_head_request_header_proxy(
|
2005-02-16 23:17:43 +00:00
|
|
|
char* buf, char* host, int port, char* file, const char* encstr
|
2004-06-13 04:47:20 +00:00
|
|
|
) {
|
2005-02-03 18:49:38 +00:00
|
|
|
char user_agent_string[256];
|
|
|
|
get_user_agent_string(user_agent_string);
|
2004-06-13 04:47:20 +00:00
|
|
|
sprintf(buf,
|
|
|
|
"HEAD %s HTTP/1.0\015\012"
|
2005-02-03 18:49:38 +00:00
|
|
|
"User-Agent: %s\015\012"
|
2004-06-13 04:47:20 +00:00
|
|
|
"Host: %s:%d\015\012"
|
|
|
|
"Connection: close\015\012"
|
|
|
|
"Accept: */*\015\012"
|
2004-12-10 19:17:32 +00:00
|
|
|
"Proxy-Authorization: Basic %s\015\012"
|
2004-06-13 04:47:20 +00:00
|
|
|
"\015\012",
|
2005-02-03 18:49:38 +00:00
|
|
|
file, user_agent_string, host, port, encstr
|
2004-06-13 04:47:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2005-03-09 20:51:15 +00:00
|
|
|
// create an HTTP 1.0 POST request header
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2002-07-05 05:33:40 +00:00
|
|
|
static void http_post_request_header(
|
2002-12-09 03:07:36 +00:00
|
|
|
char* buf, char* host, int port, char* file, int size
|
2002-07-05 05:33:40 +00:00
|
|
|
) {
|
2005-03-03 23:49:49 +00:00
|
|
|
char user_agent_string[256];
|
|
|
|
get_user_agent_string(user_agent_string);
|
2002-04-30 22:22:54 +00:00
|
|
|
sprintf(buf,
|
2002-12-09 09:36:40 +00:00
|
|
|
"POST %s HTTP/1.0\015\012"
|
2002-04-30 22:22:54 +00:00
|
|
|
"Pragma: no-cache\015\012"
|
|
|
|
"Cache-Control: no-cache\015\012"
|
2005-03-03 23:49:49 +00:00
|
|
|
"User-Agent: %s\015\012"
|
2002-12-09 03:07:36 +00:00
|
|
|
"Host: %s:%d\015\012"
|
2003-05-10 00:52:36 +00:00
|
|
|
"Connection: close\015\012"
|
2002-04-30 22:22:54 +00:00
|
|
|
"Content-Type: application/octet-stream\015\012"
|
|
|
|
"Content-Length: %d\015\012"
|
|
|
|
"\015\012",
|
2005-03-03 23:49:49 +00:00
|
|
|
file, user_agent_string, host, port, size
|
2002-04-30 22:22:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2004-06-13 04:47:20 +00:00
|
|
|
static void http_post_request_header_proxy(
|
2005-02-16 23:17:43 +00:00
|
|
|
char* buf, char* host, int port, char* file, int size, const char* encstr
|
2004-06-13 04:47:20 +00:00
|
|
|
) {
|
2005-03-03 23:49:49 +00:00
|
|
|
char user_agent_string[256];
|
|
|
|
get_user_agent_string(user_agent_string);
|
2004-06-13 04:47:20 +00:00
|
|
|
sprintf(buf,
|
|
|
|
"POST %s HTTP/1.0\015\012"
|
|
|
|
"Pragma: no-cache\015\012"
|
|
|
|
"Cache-Control: no-cache\015\012"
|
2005-03-03 23:49:49 +00:00
|
|
|
"User-Agent: %s\015\012"
|
2004-06-13 04:47:20 +00:00
|
|
|
"Host: %s:%d\015\012"
|
|
|
|
"Connection: close\015\012"
|
|
|
|
"Content-Type: application/octet-stream\015\012"
|
|
|
|
"Content-Length: %d\015\012"
|
2004-12-10 19:17:32 +00:00
|
|
|
"Proxy-Authorization: Basic %s\015\012"
|
2004-06-13 04:47:20 +00:00
|
|
|
"\015\012",
|
2005-03-03 23:49:49 +00:00
|
|
|
file, user_agent_string, host, port, size, encstr
|
2004-06-13 04:47:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2003-09-04 20:11:47 +00:00
|
|
|
void HTTP_REPLY_HEADER::init() {
|
2003-09-30 18:09:58 +00:00
|
|
|
http_status = 500;
|
2003-08-22 00:15:51 +00:00
|
|
|
content_length = 0;
|
|
|
|
redirect_location.erase();
|
|
|
|
recv_buf.erase();
|
|
|
|
}
|
|
|
|
|
2003-09-04 20:11:47 +00:00
|
|
|
void HTTP_REPLY_HEADER::parse() {
|
2004-04-08 08:15:23 +00:00
|
|
|
SCOPE_MSG_LOG scope_messages(log_messages, CLIENT_MSG_LOG::DEBUG_HTTP);
|
2003-08-22 00:15:51 +00:00
|
|
|
|
|
|
|
istringstream h(recv_buf);
|
|
|
|
string line, w;
|
|
|
|
|
2004-12-10 19:17:32 +00:00
|
|
|
if (getline(h, line)) {
|
2003-08-22 00:15:51 +00:00
|
|
|
istringstream iline(line);
|
|
|
|
|
|
|
|
iline >> w;
|
|
|
|
if (!starts_with(w,"HTTP/")) {
|
|
|
|
scope_messages.printf("HTTP_REPLY_HEADER::parse(): not HTTP\n");
|
|
|
|
return;
|
|
|
|
}
|
2003-09-30 18:09:58 +00:00
|
|
|
iline >> http_status;
|
|
|
|
scope_messages.printf("HTTP_REPLY_HEADER::parse(): status=%d\n", http_status);
|
2003-08-22 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (getline(h, line)) {
|
|
|
|
istringstream iline(line);
|
|
|
|
|
|
|
|
iline >> w;
|
|
|
|
downcase_string(w);
|
|
|
|
if (w == "content-length:") {
|
|
|
|
iline >> content_length;
|
|
|
|
scope_messages.printf("HTTP_REPLY_HEADER::parse(): content_length=%d\n", content_length);
|
|
|
|
} else if (w == "location:") {
|
|
|
|
iline >> redirect_location;
|
|
|
|
scope_messages.printf("HTTP_REPLY_HEADER::parse(): redirect_location=%s\n", redirect_location.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-11 23:34:09 +00:00
|
|
|
// return true if, although the last recv() returned -1,
|
|
|
|
// there might be more data later on the socket
|
|
|
|
//
|
|
|
|
inline bool maybe_more_data() {
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (WSAGetLastError() == WSAEWOULDBLOCK) return true;
|
|
|
|
#else
|
|
|
|
if (errno == EAGAIN) return true;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-08-22 00:15:51 +00:00
|
|
|
const unsigned int MAX_HEADER_SIZE = 1024;
|
|
|
|
|
2004-12-10 00:57:20 +00:00
|
|
|
// Read an HTTP reply header into the header struct
|
|
|
|
// Read one character at a time so we don't read anything beyond the header.
|
|
|
|
// Parse the reply header if it's complete.
|
|
|
|
// Returns:
|
|
|
|
// 1 if not done yet
|
|
|
|
// 0 if done (header.http_status indicates status)
|
2003-09-04 20:11:47 +00:00
|
|
|
//
|
|
|
|
int HTTP_REPLY_HEADER::read_reply(int socket) {
|
2004-04-08 08:15:23 +00:00
|
|
|
SCOPE_MSG_LOG scope_messages(log_messages, CLIENT_MSG_LOG::DEBUG_HTTP);
|
2003-07-02 02:02:18 +00:00
|
|
|
|
2003-08-22 00:15:51 +00:00
|
|
|
while (recv_buf.size() < MAX_HEADER_SIZE) {
|
|
|
|
char c;
|
2003-09-04 20:11:47 +00:00
|
|
|
errno = 0;
|
2003-08-22 00:15:51 +00:00
|
|
|
int n = recv(socket, &c, 1, 0);
|
2003-09-07 02:40:57 +00:00
|
|
|
if (n != 1) {
|
|
|
|
scope_messages.printf(
|
|
|
|
"HTTP_REPLY_HEADER::read_reply(): recv() on socket %d returned %d errno %d sockerr %d\n",
|
|
|
|
socket, n, errno, get_socket_error(socket)
|
2003-09-09 04:38:44 +00:00
|
|
|
);
|
2003-09-07 02:40:57 +00:00
|
|
|
}
|
2005-02-11 23:34:09 +00:00
|
|
|
if (n == -1 && maybe_more_data()) {
|
2003-08-22 00:15:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2003-09-04 20:11:47 +00:00
|
|
|
|
|
|
|
// if n is zero, we've reached EOF (and that's an error)
|
|
|
|
//
|
2003-08-22 00:15:51 +00:00
|
|
|
if (n != 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '\r') continue;
|
|
|
|
recv_buf += c;
|
|
|
|
|
|
|
|
if (ends_with(recv_buf, "\n\n")) {
|
|
|
|
scope_messages.printf_multiline(recv_buf.c_str(),
|
2003-09-04 20:11:47 +00:00
|
|
|
"HTTP_REPLY_HEADER::read_reply(): header: "
|
|
|
|
);
|
2003-08-22 00:15:51 +00:00
|
|
|
parse();
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2003-08-22 00:15:51 +00:00
|
|
|
|
2003-09-04 20:11:47 +00:00
|
|
|
// error occurred; status will be 500 (from constructor)
|
|
|
|
//
|
|
|
|
scope_messages.printf(
|
|
|
|
"HTTP_REPLY_HEADER::read_reply(): returning error (recv_buf=%s)\n",
|
|
|
|
recv_buf.c_str()
|
|
|
|
);
|
2003-08-22 00:15:51 +00:00
|
|
|
return 0;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Read the contents of the socket into buf
|
2004-12-10 04:18:40 +00:00
|
|
|
// Read until EOF from socket, so only call this if you're
|
|
|
|
// sure the message is small
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2002-07-05 05:33:40 +00:00
|
|
|
static int read_reply(int socket, char* buf, int len) {
|
|
|
|
int i, n;
|
2005-02-11 23:34:09 +00:00
|
|
|
|
|
|
|
i=0;
|
|
|
|
while (i<len-1) {
|
2002-07-05 05:33:40 +00:00
|
|
|
n = recv(socket, buf+i, 1, 0);
|
2005-02-12 21:57:50 +00:00
|
|
|
if (n==-1 && maybe_more_data()) {
|
2005-02-11 23:34:09 +00:00
|
|
|
boinc_sleep(0.1);
|
|
|
|
continue;
|
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
if (n != 1) break;
|
2005-02-11 23:34:09 +00:00
|
|
|
i++;
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
|
|
|
buf[i] = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
HTTP_OP::HTTP_OP() {
|
2004-02-05 22:36:55 +00:00
|
|
|
strcpy(url_hostname, "");
|
2003-03-19 01:15:46 +00:00
|
|
|
strcpy(filename, "");
|
2002-07-15 23:21:20 +00:00
|
|
|
req1 = NULL;
|
2003-03-19 01:15:46 +00:00
|
|
|
strcpy(infile, "");
|
|
|
|
strcpy(outfile, "");
|
2002-07-15 23:21:20 +00:00
|
|
|
content_length = 0;
|
|
|
|
file_offset = 0;
|
2003-03-19 01:15:46 +00:00
|
|
|
strcpy(request_header, "");
|
2002-07-15 23:21:20 +00:00
|
|
|
http_op_state = HTTP_STATE_IDLE;
|
|
|
|
http_op_type = HTTP_OP_NONE;
|
|
|
|
http_op_retval = 0;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HTTP_OP::~HTTP_OP() {
|
|
|
|
}
|
|
|
|
|
2003-03-02 19:24:09 +00:00
|
|
|
// Initialize HTTP HEAD operation
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2003-08-22 00:15:51 +00:00
|
|
|
int HTTP_OP::init_head(const char* url) {
|
2002-12-09 09:36:40 +00:00
|
|
|
char proxy_buf[256];
|
2004-02-05 22:36:55 +00:00
|
|
|
parse_url(url, url_hostname, port, filename);
|
2004-03-27 00:45:27 +00:00
|
|
|
PROXY::init(url_hostname, port);
|
|
|
|
NET_XFER::init(get_proxy_server_name(url_hostname),get_proxy_port(port), HTTP_BLOCKSIZE);
|
2002-06-10 06:14:18 +00:00
|
|
|
http_op_type = HTTP_OP_HEAD;
|
|
|
|
http_op_state = HTTP_STATE_CONNECTING;
|
2004-03-27 00:45:27 +00:00
|
|
|
if (pi.use_http_proxy) {
|
2004-02-05 22:36:55 +00:00
|
|
|
sprintf(proxy_buf, "http://%s:%d/%s", url_hostname, port, filename);
|
2002-12-09 09:36:40 +00:00
|
|
|
} else {
|
2004-02-05 22:36:55 +00:00
|
|
|
sprintf(proxy_buf, "/%s", filename);
|
2002-12-09 09:36:40 +00:00
|
|
|
}
|
2004-08-12 10:13:01 +00:00
|
|
|
if (!pi.use_http_auth){
|
2004-06-13 04:47:20 +00:00
|
|
|
http_head_request_header(
|
|
|
|
request_header, url_hostname, port, proxy_buf
|
|
|
|
);
|
2004-12-10 19:17:32 +00:00
|
|
|
} else {
|
|
|
|
char id_passwd[512];
|
|
|
|
string encstr = "";
|
|
|
|
memset(id_passwd,0,sizeof(id_passwd));
|
|
|
|
strcpy(id_passwd,pi.http_user_name);
|
|
|
|
strcat(id_passwd,":");
|
|
|
|
strcat(id_passwd,pi.http_user_passwd);
|
|
|
|
encstr = r_base64_encode(id_passwd,strlen(id_passwd));
|
2004-06-13 04:47:20 +00:00
|
|
|
http_head_request_header_proxy(
|
2005-02-16 23:17:43 +00:00
|
|
|
request_header, url_hostname, port, proxy_buf, encstr.c_str()
|
2004-06-13 04:47:20 +00:00
|
|
|
);
|
2004-12-10 19:17:32 +00:00
|
|
|
}
|
2002-06-10 06:14:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-02 19:24:09 +00:00
|
|
|
// Initialize HTTP GET operation
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2005-02-18 16:52:46 +00:00
|
|
|
int HTTP_OP::init_get(
|
|
|
|
const char* url, const char* out, bool del_old_file, double off
|
|
|
|
) {
|
2002-12-09 09:36:40 +00:00
|
|
|
char proxy_buf[256];
|
2003-06-24 00:33:59 +00:00
|
|
|
|
2002-08-07 22:52:10 +00:00
|
|
|
if (del_old_file) {
|
|
|
|
unlink(out);
|
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
file_offset = off;
|
2004-02-05 22:36:55 +00:00
|
|
|
parse_url(url, url_hostname, port, filename);
|
2004-03-27 00:45:27 +00:00
|
|
|
PROXY::init(url_hostname, port);
|
|
|
|
NET_XFER::init(get_proxy_server_name(url_hostname),get_proxy_port(port), HTTP_BLOCKSIZE);
|
2003-04-03 18:35:40 +00:00
|
|
|
safe_strcpy(outfile, out);
|
2004-12-10 19:17:32 +00:00
|
|
|
if (off != 0){
|
|
|
|
bytes_xferred = off;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
http_op_type = HTTP_OP_GET;
|
|
|
|
http_op_state = HTTP_STATE_CONNECTING;
|
2004-03-27 00:45:27 +00:00
|
|
|
if (pi.use_http_proxy) {
|
2004-02-05 22:36:55 +00:00
|
|
|
sprintf(proxy_buf, "http://%s:%d/%s", url_hostname, port, filename);
|
2002-12-09 09:36:40 +00:00
|
|
|
} else {
|
2004-02-05 22:36:55 +00:00
|
|
|
sprintf(proxy_buf, "/%s", filename);
|
2002-12-09 09:36:40 +00:00
|
|
|
}
|
2004-12-10 19:17:32 +00:00
|
|
|
if (!pi.use_http_auth) {
|
2005-02-18 16:52:46 +00:00
|
|
|
http_get_request_header(
|
|
|
|
request_header, url_hostname, port, proxy_buf, (int)file_offset
|
|
|
|
);
|
2004-12-10 19:17:32 +00:00
|
|
|
} else {
|
|
|
|
char id_passwd[512];
|
|
|
|
string encstr = "";
|
|
|
|
memset(id_passwd,0,sizeof(id_passwd));
|
|
|
|
strcpy(id_passwd,pi.http_user_name);
|
|
|
|
strcat(id_passwd,":");
|
|
|
|
strcat(id_passwd,pi.http_user_passwd);
|
|
|
|
encstr = r_base64_encode(id_passwd,strlen(id_passwd));
|
|
|
|
http_get_request_header_proxy(
|
|
|
|
request_header, url_hostname,
|
2005-02-16 23:17:43 +00:00
|
|
|
port, proxy_buf, (int)file_offset, encstr.c_str()
|
2004-12-10 19:17:32 +00:00
|
|
|
);
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-02 19:24:09 +00:00
|
|
|
// Initialize HTTP POST operation
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2005-02-16 23:17:43 +00:00
|
|
|
int HTTP_OP::init_post(
|
|
|
|
const char* url, const char* in, const char* out
|
|
|
|
) {
|
2002-04-30 22:22:54 +00:00
|
|
|
int retval;
|
2002-12-06 07:33:45 +00:00
|
|
|
double size;
|
2002-12-09 09:36:40 +00:00
|
|
|
char proxy_buf[256];
|
2002-08-15 22:03:41 +00:00
|
|
|
|
2004-04-08 08:15:23 +00:00
|
|
|
SCOPE_MSG_LOG scope_messages(log_messages, CLIENT_MSG_LOG::DEBUG_HTTP);
|
2003-07-02 02:02:18 +00:00
|
|
|
|
2004-02-05 22:36:55 +00:00
|
|
|
parse_url(url, url_hostname, port, filename);
|
2004-06-17 00:34:27 +00:00
|
|
|
|
2004-03-27 00:45:27 +00:00
|
|
|
PROXY::init(url_hostname, port);
|
|
|
|
NET_XFER::init(get_proxy_server_name(url_hostname),get_proxy_port(port), HTTP_BLOCKSIZE);
|
2003-04-03 18:35:40 +00:00
|
|
|
safe_strcpy(infile, in);
|
|
|
|
safe_strcpy(outfile, out);
|
2002-12-06 07:33:45 +00:00
|
|
|
retval = file_size(infile, size);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (retval) return retval;
|
2002-12-06 07:33:45 +00:00
|
|
|
content_length = (int)size;
|
2002-04-30 22:22:54 +00:00
|
|
|
http_op_type = HTTP_OP_POST;
|
|
|
|
http_op_state = HTTP_STATE_CONNECTING;
|
2004-03-27 00:45:27 +00:00
|
|
|
if (pi.use_http_proxy) {
|
2004-02-05 22:36:55 +00:00
|
|
|
sprintf(proxy_buf, "http://%s:%d/%s", url_hostname, port, filename);
|
2002-12-09 09:36:40 +00:00
|
|
|
} else {
|
2004-02-05 22:36:55 +00:00
|
|
|
sprintf(proxy_buf, "/%s", filename);
|
2002-12-09 09:36:40 +00:00
|
|
|
}
|
2004-06-17 00:34:27 +00:00
|
|
|
|
2004-12-10 19:17:32 +00:00
|
|
|
if (!pi.use_http_auth) {
|
2004-06-13 04:47:20 +00:00
|
|
|
http_post_request_header(
|
|
|
|
request_header, url_hostname, port, proxy_buf, content_length
|
|
|
|
);
|
2004-12-10 19:17:32 +00:00
|
|
|
} else {
|
|
|
|
char id_passwd[512];
|
|
|
|
string encstr = "";
|
|
|
|
memset(id_passwd,0,sizeof(id_passwd));
|
|
|
|
strcpy(id_passwd,pi.http_user_name);
|
|
|
|
strcat(id_passwd,":");
|
|
|
|
strcat(id_passwd,pi.http_user_passwd);
|
|
|
|
encstr = r_base64_encode(id_passwd,strlen(id_passwd));
|
2004-06-13 04:47:20 +00:00
|
|
|
http_post_request_header_proxy(
|
|
|
|
request_header, url_hostname, port, proxy_buf, content_length,
|
2005-02-16 23:17:43 +00:00
|
|
|
encstr.c_str()
|
2004-06-13 04:47:20 +00:00
|
|
|
);
|
2004-12-10 19:17:32 +00:00
|
|
|
}
|
2003-07-10 18:49:52 +00:00
|
|
|
scope_messages.printf("HTTP_OP::init_post(): %p io_done %d\n", this, io_done);
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-02 19:24:09 +00:00
|
|
|
// Initialize HTTP POST operation
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2002-07-05 05:33:40 +00:00
|
|
|
int HTTP_OP::init_post2(
|
2005-02-16 23:17:43 +00:00
|
|
|
const char* url, char* r1, const char* in, double offset
|
2002-07-05 05:33:40 +00:00
|
|
|
) {
|
|
|
|
int retval;
|
2002-12-06 07:33:45 +00:00
|
|
|
double size;
|
2002-12-09 09:36:40 +00:00
|
|
|
char proxy_buf[256];
|
2002-08-15 22:03:41 +00:00
|
|
|
|
2004-02-05 22:36:55 +00:00
|
|
|
parse_url(url, url_hostname, port, filename);
|
2004-03-27 00:45:27 +00:00
|
|
|
PROXY::init(url_hostname, port);
|
|
|
|
NET_XFER::init(get_proxy_server_name(url_hostname),get_proxy_port(port), HTTP_BLOCKSIZE);
|
2002-07-05 05:33:40 +00:00
|
|
|
req1 = r1;
|
2002-08-12 21:54:19 +00:00
|
|
|
if (in) {
|
2003-04-03 18:35:40 +00:00
|
|
|
safe_strcpy(infile, in);
|
2002-08-12 21:54:19 +00:00
|
|
|
file_offset = offset;
|
2002-12-06 07:33:45 +00:00
|
|
|
retval = file_size(infile, size);
|
2002-08-23 00:53:00 +00:00
|
|
|
if (retval) {
|
2002-08-29 05:15:54 +00:00
|
|
|
printf("HTTP::init_post2: couldn't get file size\n");
|
|
|
|
return retval;
|
|
|
|
}
|
2002-12-06 07:33:45 +00:00
|
|
|
content_length = (int)size - (int)offset;
|
2002-08-12 21:54:19 +00:00
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
content_length += strlen(req1);
|
|
|
|
http_op_type = HTTP_OP_POST2;
|
|
|
|
http_op_state = HTTP_STATE_CONNECTING;
|
2004-03-27 00:45:27 +00:00
|
|
|
if (pi.use_http_proxy) {
|
2004-02-05 22:36:55 +00:00
|
|
|
sprintf(proxy_buf, "http://%s:%d/%s", url_hostname, port, filename);
|
2002-12-09 09:36:40 +00:00
|
|
|
} else {
|
2004-02-05 22:36:55 +00:00
|
|
|
sprintf(proxy_buf, "/%s", filename);
|
2002-12-09 09:36:40 +00:00
|
|
|
}
|
2004-12-10 19:17:32 +00:00
|
|
|
if (!pi.use_http_auth) {
|
2004-06-13 04:47:20 +00:00
|
|
|
http_post_request_header(
|
|
|
|
request_header, url_hostname, port, proxy_buf, content_length
|
|
|
|
);
|
2004-12-10 19:17:32 +00:00
|
|
|
} else {
|
|
|
|
char id_passwd[512];
|
|
|
|
string encstr = "";
|
|
|
|
memset(id_passwd,0,sizeof(id_passwd));
|
|
|
|
strcpy(id_passwd,pi.http_user_name);
|
|
|
|
strcat(id_passwd,":");
|
|
|
|
strcat(id_passwd,pi.http_user_passwd);
|
|
|
|
encstr = r_base64_encode(id_passwd,strlen(id_passwd));
|
2004-06-13 04:47:20 +00:00
|
|
|
http_post_request_header_proxy(
|
|
|
|
request_header, url_hostname, port, proxy_buf, content_length,
|
2005-02-16 23:17:43 +00:00
|
|
|
encstr.c_str()
|
2004-06-13 04:47:20 +00:00
|
|
|
);
|
2004-12-10 19:17:32 +00:00
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Returns true if the HTTP operation is complete
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
bool HTTP_OP::http_op_done() {
|
|
|
|
return (http_op_state == HTTP_STATE_DONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
HTTP_OP_SET::HTTP_OP_SET(NET_XFER_SET* p) {
|
|
|
|
net_xfers = p;
|
|
|
|
}
|
|
|
|
|
2003-03-02 19:24:09 +00:00
|
|
|
// Adds an HTTP_OP to the set
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int HTTP_OP_SET::insert(HTTP_OP* ho) {
|
|
|
|
int retval;
|
2002-08-15 22:03:41 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
retval = net_xfers->insert(ho);
|
|
|
|
if (retval) return retval;
|
|
|
|
http_ops.push_back(ho);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-14 22:01:05 +00:00
|
|
|
bool HTTP_OP_SET::poll(double) {
|
2002-04-30 22:22:54 +00:00
|
|
|
unsigned int i;
|
|
|
|
HTTP_OP* htp;
|
2002-08-29 04:19:51 +00:00
|
|
|
int n, retval;
|
2002-04-30 22:22:54 +00:00
|
|
|
bool action = false;
|
|
|
|
|
2004-04-08 08:15:23 +00:00
|
|
|
SCOPE_MSG_LOG scope_messages(log_messages, CLIENT_MSG_LOG::DEBUG_HTTP);
|
2003-07-02 02:02:18 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
for (i=0; i<http_ops.size(); i++) {
|
|
|
|
htp = http_ops[i];
|
2004-06-17 00:34:27 +00:00
|
|
|
if (htp->error) {
|
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
|
|
|
htp->http_op_retval = htp->error;
|
|
|
|
action = true;
|
|
|
|
continue;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
switch(htp->http_op_state) {
|
|
|
|
case HTTP_STATE_CONNECTING:
|
|
|
|
if (htp->is_connected) {
|
2004-03-27 00:45:27 +00:00
|
|
|
htp->http_op_state = HTTP_STATE_SOCKS_CONNECT;
|
2002-04-30 22:22:54 +00:00
|
|
|
htp->want_upload = true;
|
|
|
|
action = true;
|
|
|
|
}
|
|
|
|
break;
|
2004-03-27 00:45:27 +00:00
|
|
|
case HTTP_STATE_SOCKS_CONNECT:
|
|
|
|
// Since the HTTP layer is synchronous with the proxy layer, we
|
|
|
|
// call proxy_poll() here instead of in do_something()
|
|
|
|
htp->proxy_poll();
|
|
|
|
|
|
|
|
// After negotiation with the proxy is complete, advance to
|
|
|
|
// the next step of the HTTP layer
|
|
|
|
if (htp->proxy_negotiated()) {
|
|
|
|
if (htp->proxy_retval) {
|
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
|
|
|
htp->http_op_retval = htp->proxy_retval;
|
|
|
|
switch (htp->proxy_retval) {
|
2004-12-10 19:17:32 +00:00
|
|
|
case ERR_SOCKS_UNKNOWN_FAILURE:
|
|
|
|
msg_printf(NULL, MSG_ERROR, "An unknown SOCKS server error occurred\n");
|
|
|
|
break;
|
|
|
|
case ERR_SOCKS_REQUEST_FAILED:
|
|
|
|
msg_printf(NULL, MSG_ERROR, "The SOCKS server denied access for this computer\n");
|
|
|
|
break;
|
|
|
|
case ERR_SOCKS_BAD_USER_PASS:
|
|
|
|
msg_printf(NULL, MSG_ERROR, "Incorrect SOCKS user name and/or password\n");
|
|
|
|
break;
|
|
|
|
case ERR_SOCKS_UNKNOWN_SERVER_VERSION:
|
|
|
|
msg_printf(NULL, MSG_ERROR, "The SOCKS server is using an unknown version\n");
|
|
|
|
break;
|
|
|
|
case ERR_SOCKS_UNSUPPORTED:
|
|
|
|
msg_printf(NULL, MSG_ERROR, "The SOCKS server is using unsupported features unknown to BOINC\n");
|
|
|
|
break;
|
|
|
|
case ERR_SOCKS_CANT_REACH_HOST:
|
|
|
|
msg_printf(NULL, MSG_ERROR, "The SOCKS server is unable to contact the host\n");
|
|
|
|
break;
|
|
|
|
case ERR_SOCKS_CONN_REFUSED:
|
|
|
|
msg_printf(NULL, MSG_ERROR, "The connection from the SOCKS server to the host was refused\n");
|
|
|
|
break;
|
2004-03-27 00:45:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
htp->http_op_state = HTTP_STATE_REQUEST_HEADER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2002-04-30 22:22:54 +00:00
|
|
|
case HTTP_STATE_REQUEST_HEADER:
|
|
|
|
if (htp->io_ready) {
|
|
|
|
action = true;
|
2003-03-02 19:24:09 +00:00
|
|
|
n = send(
|
|
|
|
htp->socket, htp->request_header,
|
|
|
|
strlen(htp->request_header), 0
|
2003-02-12 19:21:44 +00:00
|
|
|
);
|
2003-07-29 23:26:32 +00:00
|
|
|
scope_messages.printf(
|
|
|
|
"HTTP_OP_SET::poll(): wrote HTTP header to socket %d: %d bytes\n",
|
|
|
|
htp->socket, n
|
|
|
|
);
|
2003-07-02 02:02:18 +00:00
|
|
|
scope_messages.printf_multiline(htp->request_header, "HTTP_OP_SET::poll(): request header: ");
|
2002-04-30 22:22:54 +00:00
|
|
|
htp->io_ready = false;
|
|
|
|
switch(htp->http_op_type) {
|
|
|
|
case HTTP_OP_POST:
|
|
|
|
htp->http_op_state = HTTP_STATE_REQUEST_BODY;
|
2005-03-10 22:05:42 +00:00
|
|
|
htp->file = boinc_fopen(htp->infile, "rb");
|
2002-04-30 22:22:54 +00:00
|
|
|
if (!htp->file) {
|
2003-07-03 05:01:29 +00:00
|
|
|
msg_printf(NULL, MSG_ERROR, "HTTP_OP_SET::poll(): no input file %s\n", htp->infile);
|
2002-04-30 22:22:54 +00:00
|
|
|
htp->io_done = true;
|
|
|
|
htp->http_op_retval = ERR_FOPEN;
|
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
htp->do_file_io = true;
|
|
|
|
break;
|
|
|
|
case HTTP_OP_GET:
|
2002-06-10 06:14:18 +00:00
|
|
|
case HTTP_OP_HEAD:
|
2003-08-22 00:15:51 +00:00
|
|
|
htp->hrh.init();
|
2002-04-30 22:22:54 +00:00
|
|
|
htp->http_op_state = HTTP_STATE_REPLY_HEADER;
|
|
|
|
htp->want_upload = false;
|
|
|
|
htp->want_download = true;
|
2004-02-19 16:43:28 +00:00
|
|
|
|
|
|
|
// We don't need to write to the socket anymore.
|
|
|
|
// Close the read fd on the receiving side.
|
|
|
|
// This is needed by the scheduler "use_file" mechanism
|
|
|
|
// NOTE: this is commented out because
|
|
|
|
// - it seems to cause problems on all platforms
|
|
|
|
// in Windows it exercises a problem with Norton Firewall
|
2003-12-06 01:42:46 +00:00
|
|
|
#ifndef _WIN32
|
2004-02-19 16:43:28 +00:00
|
|
|
//shutdown(htp->socket, SHUT_WR);
|
2003-12-06 01:42:46 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
break;
|
2002-07-05 05:33:40 +00:00
|
|
|
case HTTP_OP_POST2:
|
|
|
|
htp->http_op_state = HTTP_STATE_REQUEST_BODY1;
|
|
|
|
break;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2002-07-05 05:33:40 +00:00
|
|
|
case HTTP_STATE_REQUEST_BODY1:
|
|
|
|
if (htp->io_ready) {
|
|
|
|
action = true;
|
|
|
|
n = send(htp->socket, htp->req1, strlen(htp->req1), 0);
|
|
|
|
htp->http_op_state = HTTP_STATE_REQUEST_BODY;
|
2003-03-02 19:24:09 +00:00
|
|
|
|
2002-08-12 21:54:19 +00:00
|
|
|
if (htp->infile && strlen(htp->infile) > 0) {
|
2005-03-10 22:05:42 +00:00
|
|
|
htp->file = boinc_fopen(htp->infile, "rb");
|
2002-08-12 21:54:19 +00:00
|
|
|
if (!htp->file) {
|
2003-07-03 05:01:29 +00:00
|
|
|
msg_printf(NULL, MSG_ERROR, "HTTP_OP_SET::poll(): no input2 file %s\n", htp->infile);
|
2002-08-12 21:54:19 +00:00
|
|
|
htp->io_done = true;
|
|
|
|
htp->http_op_retval = ERR_FOPEN;
|
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fseek(htp->file, (long)htp->file_offset, SEEK_SET);
|
|
|
|
htp->do_file_io = true;
|
|
|
|
} else {
|
2002-07-05 05:33:40 +00:00
|
|
|
htp->io_done = true;
|
2002-08-12 21:54:19 +00:00
|
|
|
htp->do_file_io = false;
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
|
|
|
htp->io_ready = false;
|
|
|
|
}
|
|
|
|
break;
|
2002-04-30 22:22:54 +00:00
|
|
|
case HTTP_STATE_REQUEST_BODY:
|
|
|
|
if (htp->io_done) {
|
|
|
|
action = true;
|
2003-07-02 02:02:18 +00:00
|
|
|
scope_messages.printf("HTTP_OP_SET::poll(): finished sending request body\n");
|
2003-08-22 00:15:51 +00:00
|
|
|
htp->hrh.init();
|
2002-04-30 22:22:54 +00:00
|
|
|
htp->http_op_state = HTTP_STATE_REPLY_HEADER;
|
2002-08-26 22:50:36 +00:00
|
|
|
if (htp->file) {
|
2002-08-22 23:54:16 +00:00
|
|
|
fclose(htp->file);
|
2002-08-29 05:15:54 +00:00
|
|
|
htp->file = 0;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
htp->do_file_io = false;
|
|
|
|
htp->want_upload = false;
|
|
|
|
htp->want_download = true;
|
2002-07-05 05:33:40 +00:00
|
|
|
htp->io_ready = false;
|
2003-02-12 19:21:44 +00:00
|
|
|
htp->io_done = false;
|
2003-11-28 23:54:00 +00:00
|
|
|
|
2004-02-19 16:43:28 +00:00
|
|
|
// We don't need to write to the socket anymore.
|
|
|
|
// Close the read fd on the receiving side.
|
|
|
|
// This is needed by the scheduler "use_file" mechanism
|
|
|
|
// NOTE: this is commented out because
|
|
|
|
// - it seems to cause problems on all platforms
|
|
|
|
// in Windows it exercises a problem with Norton Firewall
|
2003-12-06 01:42:46 +00:00
|
|
|
#ifndef _WIN32
|
2004-02-19 16:43:28 +00:00
|
|
|
//shutdown(htp->socket, SHUT_WR);
|
2003-12-06 01:42:46 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2003-09-04 20:11:47 +00:00
|
|
|
break;
|
2002-04-30 22:22:54 +00:00
|
|
|
case HTTP_STATE_REPLY_HEADER:
|
|
|
|
if (htp->io_ready) {
|
|
|
|
action = true;
|
2003-09-04 20:11:47 +00:00
|
|
|
scope_messages.printf(
|
|
|
|
"HTTP_OP_SET::poll(): reading reply header; io_ready %d io_done %d\n",
|
|
|
|
htp->io_ready, htp->io_done
|
|
|
|
);
|
2003-08-22 00:15:51 +00:00
|
|
|
if (htp->hrh.read_reply(htp->socket)) {
|
2004-12-10 00:57:20 +00:00
|
|
|
// reading header, not done yet
|
2003-09-04 20:11:47 +00:00
|
|
|
htp->io_ready = false;
|
2003-08-22 00:15:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-03-02 19:24:09 +00:00
|
|
|
|
2004-12-10 00:57:20 +00:00
|
|
|
// here we've read and parsed the header.
|
|
|
|
// hth.http_status tells us the HTTP status
|
2003-03-02 19:24:09 +00:00
|
|
|
|
2003-09-30 18:09:58 +00:00
|
|
|
if (htp->hrh.http_status == HTTP_STATUS_MOVED_PERM || htp->hrh.http_status == HTTP_STATUS_MOVED_TEMP) {
|
2002-08-02 18:31:20 +00:00
|
|
|
htp->close_socket();
|
2004-07-02 04:49:17 +00:00
|
|
|
retval = 0;
|
|
|
|
const char* new_url = htp->hrh.redirect_location.c_str();
|
|
|
|
if (!new_url || !strlen(new_url)) {
|
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
|
|
|
htp->http_op_retval = retval;
|
|
|
|
break;
|
|
|
|
}
|
2002-08-02 18:31:20 +00:00
|
|
|
switch (htp->http_op_type) {
|
2004-12-10 04:18:40 +00:00
|
|
|
case HTTP_OP_HEAD:
|
|
|
|
htp->init_head(new_url);
|
|
|
|
break;
|
|
|
|
case HTTP_OP_GET:
|
|
|
|
htp->init_get(new_url, htp->outfile, false);
|
|
|
|
break;
|
|
|
|
case HTTP_OP_POST:
|
|
|
|
htp->init_post(new_url, htp->infile, htp->outfile);
|
|
|
|
break;
|
|
|
|
case HTTP_OP_POST2:
|
|
|
|
htp->init_post2(new_url, htp->req1, htp->infile, htp->file_offset);
|
|
|
|
break;
|
2002-08-02 18:31:20 +00:00
|
|
|
}
|
2003-03-02 19:24:09 +00:00
|
|
|
|
2002-08-02 18:31:20 +00:00
|
|
|
// Open connection to the redirected server
|
2003-03-02 19:24:09 +00:00
|
|
|
//
|
2003-09-04 00:41:51 +00:00
|
|
|
retval = htp->open_server();
|
|
|
|
if (retval) {
|
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
|
|
|
htp->http_op_retval = retval;
|
|
|
|
}
|
2002-08-02 18:31:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-06-13 04:47:20 +00:00
|
|
|
|
|
|
|
if (htp->hrh.http_status == HTTP_STATUS_PROXY_AUTH_REQ) {
|
|
|
|
htp->close_socket();
|
2004-06-17 00:34:27 +00:00
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
|
|
|
htp->http_op_retval = htp->hrh.http_status;
|
|
|
|
msg_printf(NULL, MSG_ERROR, "HTTP_OP_SET::poll(): Proxy Authentication Failed\n");
|
|
|
|
break;
|
2004-06-13 04:47:20 +00:00
|
|
|
}
|
|
|
|
|
2004-12-10 19:17:32 +00:00
|
|
|
if ((htp->hrh.http_status/100)*100 != HTTP_STATUS_OK) {
|
2002-04-30 22:22:54 +00:00
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
2003-09-30 18:09:58 +00:00
|
|
|
htp->http_op_retval = htp->hrh.http_status;
|
2002-04-30 22:22:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (htp->http_op_type) {
|
2002-06-10 06:14:18 +00:00
|
|
|
case HTTP_OP_HEAD:
|
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
|
|
|
htp->http_op_retval = 0;
|
|
|
|
break;
|
2002-04-30 22:22:54 +00:00
|
|
|
case HTTP_OP_POST:
|
2002-08-29 04:08:40 +00:00
|
|
|
retval = unlink(htp->outfile);
|
2002-10-09 04:56:41 +00:00
|
|
|
// no error check here because file need not already exist
|
2005-02-18 16:52:46 +00:00
|
|
|
|
2005-02-18 17:14:13 +00:00
|
|
|
htp->bytes_xferred = 0;
|
|
|
|
htp->file_offset = 0;
|
2002-08-29 04:08:40 +00:00
|
|
|
// fall through
|
2005-02-18 16:52:46 +00:00
|
|
|
//
|
2002-08-07 22:52:10 +00:00
|
|
|
case HTTP_OP_GET:
|
|
|
|
htp->http_op_state = HTTP_STATE_REPLY_BODY;
|
2003-06-24 00:33:59 +00:00
|
|
|
|
2005-04-07 22:32:04 +00:00
|
|
|
// if server doesn't support range request,
|
|
|
|
// prepare to receive the entire file
|
|
|
|
//
|
|
|
|
if (htp->hrh.http_status == HTTP_STATUS_PARTIAL_CONTENT) {
|
|
|
|
htp->file = boinc_fopen(htp->outfile, "ab");
|
|
|
|
} else {
|
|
|
|
htp->file = boinc_fopen(htp->outfile, "wb");
|
|
|
|
htp->file_offset = 0;
|
|
|
|
htp->bytes_xferred = 0;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
if (!htp->file) {
|
2003-07-03 05:01:29 +00:00
|
|
|
msg_printf(NULL, MSG_ERROR,
|
|
|
|
"HTTP_OP_SET::poll(): can't open output file %s\n",
|
2002-04-30 22:22:54 +00:00
|
|
|
htp->outfile
|
|
|
|
);
|
|
|
|
htp->io_done = true;
|
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
|
|
|
htp->http_op_retval = ERR_FOPEN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
htp->do_file_io = true;
|
|
|
|
break;
|
2002-07-05 05:33:40 +00:00
|
|
|
case HTTP_OP_POST2:
|
|
|
|
htp->http_op_state = HTTP_STATE_REPLY_BODY;
|
2004-12-10 00:57:20 +00:00
|
|
|
htp->io_done = false;
|
2002-07-05 05:33:40 +00:00
|
|
|
break;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HTTP_STATE_REPLY_BODY:
|
2004-12-10 04:18:40 +00:00
|
|
|
switch (htp->http_op_type) {
|
|
|
|
case HTTP_OP_POST2:
|
|
|
|
if (htp->io_ready) {
|
|
|
|
action = true;
|
2002-07-05 05:33:40 +00:00
|
|
|
read_reply(htp->socket, htp->req1, 256);
|
2004-12-10 04:18:40 +00:00
|
|
|
scope_messages.printf("HTTP_OP_SET::poll(): got reply body\n");
|
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
|
|
|
htp->http_op_retval = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (htp->io_done) {
|
|
|
|
action = true;
|
2002-07-05 05:33:40 +00:00
|
|
|
fclose(htp->file);
|
|
|
|
htp->file = 0;
|
2004-12-10 04:18:40 +00:00
|
|
|
scope_messages.printf("HTTP_OP_SET::poll(): got reply body\n");
|
|
|
|
htp->http_op_retval = 0;
|
2005-01-13 18:57:27 +00:00
|
|
|
if (htp->hrh.content_length) {
|
2005-02-18 16:52:46 +00:00
|
|
|
if ((htp->bytes_xferred - htp->file_offset) != htp->hrh.content_length) {
|
2005-03-10 00:43:22 +00:00
|
|
|
scope_messages.printf(
|
|
|
|
"HTTP_OP_SET::poll(): ERR_IO: bytes_xferred: %d,"
|
|
|
|
"file offset: %d, expected content length: %d\n",
|
|
|
|
(int)htp->bytes_xferred, (int)htp->file_offset, htp->hrh.content_length
|
|
|
|
);
|
2005-01-13 18:57:27 +00:00
|
|
|
htp->http_op_retval = ERR_IO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
2002-07-05 05:33:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
break;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Remove an HTTP_OP from the set
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int HTTP_OP_SET::remove(HTTP_OP* p) {
|
|
|
|
vector<HTTP_OP*>::iterator iter;
|
2002-08-15 22:03:41 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
net_xfers->remove(p);
|
|
|
|
|
|
|
|
iter = http_ops.begin();
|
|
|
|
while (iter != http_ops.end()) {
|
2002-08-29 05:15:54 +00:00
|
|
|
if (*iter == p) {
|
|
|
|
http_ops.erase(iter);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
iter++;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2003-07-29 23:26:32 +00:00
|
|
|
msg_printf(NULL, MSG_ERROR, "HTTP_OP_SET::remove(): not found\n");
|
2004-01-30 22:19:19 +00:00
|
|
|
return ERR_NOT_FOUND;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_57f273bb60 = "$Id$";
|