2003-07-01 20:37:09 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-04-30 22:22:54 +00:00
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
2003-07-01 20:37:09 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-07-02 02:02:18 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
2003-07-02 02:02:18 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
2003-07-01 20:37:09 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-07-02 02:02:18 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2003-10-16 19:03:49 +00:00
|
|
|
#include "cpp.h"
|
2002-06-06 18:50:12 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2004-03-04 11:41:43 +00:00
|
|
|
#include "stdafx.h"
|
2003-12-02 22:54:14 +00:00
|
|
|
#define SHUT_WR SD_SEND
|
2002-07-22 23:07:14 +00:00
|
|
|
#endif
|
2004-03-04 11:41:43 +00:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <cstring>
|
|
|
|
#include <sstream>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2002-07-22 23:07:14 +00:00
|
|
|
#if HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
2004-02-28 19:11:40 +00:00
|
|
|
#if HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
2002-07-22 23:07:14 +00:00
|
|
|
#if HAVE_UNISTD_H
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <unistd.h>
|
2002-06-06 18:50:12 +00:00
|
|
|
#endif
|
2004-03-04 11:41:43 +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"
|
2003-07-02 02:02:18 +00:00
|
|
|
#include "message.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-07-03 05:01:29 +00:00
|
|
|
#include "http.h"
|
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
#define HTTP_BLOCKSIZE 16384
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-03-02 19:24:09 +00:00
|
|
|
// Breaks a HTTP url down into its server and file path components
|
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
|
|
|
|
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
|
|
|
}
|
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
|
|
|
}
|
2002-12-09 03:07:36 +00:00
|
|
|
p=strchr(buf,':');
|
|
|
|
if (p) {
|
|
|
|
port = atol(p+1);
|
|
|
|
*p = 0;
|
|
|
|
} else {
|
|
|
|
port=80;
|
|
|
|
}
|
2003-03-19 01:15:46 +00:00
|
|
|
strcpy(host, buf);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2002-08-12 21:54:19 +00:00
|
|
|
// Prints an HTTP 1.1 GET request header into buf
|
2002-08-15 20:50:15 +00:00
|
|
|
// Hopefully there won't be chunked transfers in a GET
|
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];
|
|
|
|
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"
|
2002-12-09 03:07:36 +00:00
|
|
|
"User-Agent: BOINC client\015\012"
|
|
|
|
"Host: %s:%d\015\012"
|
|
|
|
"%s"
|
|
|
|
"Connection: close\015\012"
|
|
|
|
"Accept: */*\015\012"
|
|
|
|
"\015\012",
|
2002-12-09 09:36:40 +00:00
|
|
|
file, host, port, offset?offset_info:""
|
2002-12-09 03:07:36 +00:00
|
|
|
);
|
2002-06-10 06:14:18 +00:00
|
|
|
}
|
|
|
|
|
2002-08-12 21:54:19 +00:00
|
|
|
// Prints an HTTP 1.1 HEAD request header into buf
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2002-12-09 03:07:36 +00:00
|
|
|
static void http_head_request_header(char* buf, char* host, int port, char* file) {
|
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"
|
2002-04-30 22:22:54 +00:00
|
|
|
"User-Agent: BOINC client\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",
|
2002-12-09 09:36:40 +00:00
|
|
|
file, host, port
|
2002-04-30 22:22:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2002-08-15 20:50:15 +00:00
|
|
|
// Prints an HTTP 1.0 POST request header into buf
|
|
|
|
// Use HTTP 1.0 so we don't have to deal with chunked transfers
|
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
|
|
|
) {
|
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"
|
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",
|
2002-12-09 09:36:40 +00:00
|
|
|
file, host, port, size
|
2002-04-30 22:22:54 +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() {
|
2003-08-22 00:15:51 +00:00
|
|
|
ScopeMessages scope_messages(log_messages, ClientMessages::DEBUG_HTTP);
|
|
|
|
|
|
|
|
istringstream h(recv_buf);
|
|
|
|
string line, w;
|
|
|
|
|
|
|
|
if (getline(h, line)) {
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned int MAX_HEADER_SIZE = 1024;
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Parse an http reply header into the header struct
|
|
|
|
//
|
2003-09-30 18:09:58 +00:00
|
|
|
// Returns 1 if not done yet, 0 if done (header.http_status indicates success)
|
2003-09-04 20:11:47 +00:00
|
|
|
//
|
|
|
|
int HTTP_REPLY_HEADER::read_reply(int socket) {
|
2003-07-02 02:02:18 +00:00
|
|
|
ScopeMessages scope_messages(log_messages, ClientMessages::DEBUG_HTTP);
|
|
|
|
|
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
|
|
|
}
|
2003-08-22 00:15:51 +00:00
|
|
|
if (n == -1 && errno == EAGAIN) {
|
|
|
|
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
|
|
|
|
//
|
2002-07-05 05:33:40 +00:00
|
|
|
static int read_reply(int socket, char* buf, int len) {
|
|
|
|
int i, n;
|
|
|
|
for (i=0; i<len-1; i++) {
|
|
|
|
n = recv(socket, buf+i, 1, 0);
|
|
|
|
if (n != 1) break;
|
|
|
|
}
|
|
|
|
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-12-19 00:10:39 +00:00
|
|
|
use_http_proxy = false;
|
2002-12-09 09:36:40 +00:00
|
|
|
proxy_server_port = 0;
|
2003-03-19 01:15:46 +00:00
|
|
|
strcpy(proxy_server_name, "");
|
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);
|
|
|
|
NET_XFER::init(use_http_proxy?proxy_server_name:url_hostname, use_http_proxy?proxy_server_port:port, HTTP_BLOCKSIZE);
|
2002-06-10 06:14:18 +00:00
|
|
|
http_op_type = HTTP_OP_HEAD;
|
|
|
|
http_op_state = HTTP_STATE_CONNECTING;
|
2002-12-19 00:10:39 +00:00
|
|
|
if (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-02-05 22:36:55 +00:00
|
|
|
http_head_request_header(request_header, url_hostname, port, proxy_buf);
|
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
|
|
|
//
|
2003-08-22 00:15:51 +00:00
|
|
|
int HTTP_OP::init_get(const char* url, 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);
|
|
|
|
NET_XFER::init(use_http_proxy?proxy_server_name:url_hostname, use_http_proxy?proxy_server_port:port, HTTP_BLOCKSIZE);
|
2003-04-03 18:35:40 +00:00
|
|
|
safe_strcpy(outfile, out);
|
2003-08-20 23:29:55 +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;
|
2002-12-19 00:10:39 +00:00
|
|
|
if (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-02-05 22:36:55 +00:00
|
|
|
http_get_request_header(request_header, url_hostname, port, proxy_buf, (int)file_offset);
|
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
|
|
|
//
|
2003-08-22 00:15:51 +00:00
|
|
|
int HTTP_OP::init_post(const char* url, char* in, 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
|
|
|
|
2003-07-02 02:02:18 +00:00
|
|
|
ScopeMessages scope_messages(log_messages, ClientMessages::DEBUG_HTTP);
|
|
|
|
|
2004-02-05 22:36:55 +00:00
|
|
|
parse_url(url, url_hostname, port, filename);
|
|
|
|
NET_XFER::init(use_http_proxy?proxy_server_name:url_hostname, use_http_proxy?proxy_server_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;
|
2002-12-19 00:10:39 +00:00
|
|
|
if (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
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
http_post_request_header(
|
2004-02-05 22:36:55 +00:00
|
|
|
request_header, url_hostname, port, proxy_buf, content_length
|
2003-07-29 23:26: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(
|
2003-08-22 00:15:51 +00:00
|
|
|
const char* url, char* r1, 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);
|
|
|
|
NET_XFER::init(use_http_proxy?proxy_server_name:url_hostname, use_http_proxy?proxy_server_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;
|
2002-12-19 00:10:39 +00:00
|
|
|
if (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
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
http_post_request_header(
|
2004-02-05 22:36:55 +00:00
|
|
|
request_header, url_hostname, port, proxy_buf, content_length
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HTTP_OP_SET::poll() {
|
|
|
|
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;
|
|
|
|
|
2003-07-02 02:02:18 +00:00
|
|
|
ScopeMessages scope_messages(log_messages, ClientMessages::DEBUG_HTTP);
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
for (i=0; i<http_ops.size(); i++) {
|
|
|
|
htp = http_ops[i];
|
|
|
|
switch(htp->http_op_state) {
|
|
|
|
case HTTP_STATE_CONNECTING:
|
2003-06-17 18:59:36 +00:00
|
|
|
if (htp->error) {
|
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
|
|
|
htp->http_op_retval = ERR_CONNECT;
|
|
|
|
break;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
if (htp->is_connected) {
|
|
|
|
htp->http_op_state = HTTP_STATE_REQUEST_HEADER;
|
|
|
|
htp->want_upload = true;
|
|
|
|
action = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
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;
|
2002-08-15 22:03:41 +00:00
|
|
|
htp->file = 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) {
|
2002-08-15 22:03:41 +00:00
|
|
|
htp->file = 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)) {
|
|
|
|
// 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
|
|
|
|
2002-08-02 18:31:20 +00:00
|
|
|
// TODO: handle all kinds of redirects here
|
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();
|
|
|
|
switch (htp->http_op_type) {
|
|
|
|
case HTTP_OP_HEAD:
|
2003-08-22 00:15:51 +00:00
|
|
|
htp->init_head(htp->hrh.redirect_location.c_str());
|
2002-08-02 18:31:20 +00:00
|
|
|
break;
|
|
|
|
case HTTP_OP_GET:
|
2003-08-22 00:15:51 +00:00
|
|
|
htp->init_get(htp->hrh.redirect_location.c_str(), htp->outfile, false);
|
2002-08-02 18:31:20 +00:00
|
|
|
break;
|
|
|
|
case HTTP_OP_POST:
|
2003-08-22 00:15:51 +00:00
|
|
|
htp->init_post(htp->hrh.redirect_location.c_str(), htp->infile, htp->outfile);
|
2002-08-02 18:31:20 +00:00
|
|
|
break;
|
|
|
|
case HTTP_OP_POST2:
|
2003-08-22 00:15:51 +00:00
|
|
|
htp->init_post2(htp->hrh.redirect_location.c_str(), htp->req1, htp->infile, htp->file_offset);
|
2002-08-02 18:31:20 +00:00
|
|
|
break;
|
|
|
|
}
|
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;
|
|
|
|
}
|
2003-09-30 18:09:58 +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
|
|
|
|
//
|
2002-08-29 04:08:40 +00:00
|
|
|
// fall through
|
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
|
|
|
|
2003-12-24 00:50:51 +00:00
|
|
|
htp->file = boinc_fopen(htp->outfile, "ab");
|
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;
|
|
|
|
htp->io_ready = false;
|
2003-02-12 19:21:44 +00:00
|
|
|
htp->io_done = true;
|
2002-07-05 05:33:40 +00:00
|
|
|
break;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HTTP_STATE_REPLY_BODY:
|
2003-02-06 19:08:18 +00:00
|
|
|
if (htp->error) {
|
|
|
|
action = true;
|
2003-07-02 02:02:18 +00:00
|
|
|
scope_messages.printf("HTTP_OP_SET::poll(): net_xfer returned error %d\n", htp->error);
|
2003-02-04 21:47:12 +00:00
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
|
|
|
htp->http_op_retval = htp->error;
|
2003-02-06 19:08:18 +00:00
|
|
|
}
|
2003-02-04 21:47:12 +00:00
|
|
|
else if (htp->io_done) {
|
2002-08-30 21:41:03 +00:00
|
|
|
action = true;
|
2002-07-05 05:33:40 +00:00
|
|
|
switch(htp->http_op_type) {
|
|
|
|
case HTTP_OP_POST2:
|
|
|
|
read_reply(htp->socket, htp->req1, 256);
|
2002-08-12 21:54:19 +00:00
|
|
|
// parse reply here?
|
2002-07-05 05:33:40 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fclose(htp->file);
|
|
|
|
htp->file = 0;
|
|
|
|
break;
|
|
|
|
}
|
2003-07-02 02:02:18 +00:00
|
|
|
scope_messages.printf("HTTP_OP_SET::poll(): got reply body\n");
|
2002-04-30 22:22:54 +00:00
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
|
|
|
htp->http_op_retval = 0;
|
|
|
|
}
|
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
|
|
|
}
|