2002-04-30 22:22:54 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// 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
|
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2002-06-06 18:50:12 +00:00
|
|
|
#include "windows_cpp.h"
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <string.h>
|
2002-06-06 18:50:12 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include "winsock.h"
|
2002-07-22 23:07:14 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_UNISTD_H
|
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"
|
|
|
|
#include "log_flags.h"
|
|
|
|
#include "http.h"
|
|
|
|
|
|
|
|
#define HTTP_BLOCKSIZE 4096
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Breaks a url down into its server and file path components
|
|
|
|
// TODO: deal with alternate protocols (ftp, gopher, etc) or disallow
|
|
|
|
// them and parse accordingly
|
|
|
|
//
|
2002-12-09 03:07:36 +00:00
|
|
|
void parse_url(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
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
if (strncmp(url, "http://", 7) == 0) strcpy(buf, url+7);
|
|
|
|
else strcpy(buf, url);
|
|
|
|
p = strchr(buf, '/');
|
|
|
|
if (p) {
|
|
|
|
strcpy(file, p+1);
|
|
|
|
*p = 0;
|
|
|
|
} else {
|
|
|
|
strcpy(file, "");
|
|
|
|
}
|
2002-12-09 03:07:36 +00:00
|
|
|
p=strchr(buf,':');
|
|
|
|
if (p) {
|
|
|
|
port = atol(p+1);
|
|
|
|
*p = 0;
|
|
|
|
} else {
|
|
|
|
port=80;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
strcpy(host, buf);
|
|
|
|
}
|
|
|
|
|
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,
|
2002-12-09 09:36:40 +00:00
|
|
|
"GET %s HTTP/1.1\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,
|
2002-12-09 09:36:40 +00:00
|
|
|
"HEAD %s HTTP/1.1\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"
|
2002-08-12 21:54:19 +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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2002-07-05 05:33:40 +00:00
|
|
|
#if 0
|
2002-07-15 23:21:20 +00:00
|
|
|
// Do we still need this?
|
|
|
|
//
|
2002-06-10 06:14:18 +00:00
|
|
|
void http_put_request_header(
|
|
|
|
char* buf, char* host, char* file, int size, int offset
|
|
|
|
) {
|
|
|
|
if (offset) {
|
|
|
|
sprintf(buf,
|
2002-08-12 21:54:19 +00:00
|
|
|
"PUT /%s HTTP/1.1\015\012"
|
2002-06-10 06:14:18 +00:00
|
|
|
"Pragma: no-cache\015\012"
|
|
|
|
"Cache-Control: no-cache\015\012"
|
|
|
|
"Host: %s:80\015\012"
|
2002-08-12 21:54:19 +00:00
|
|
|
"Range: bytes=%d-\015\012"
|
|
|
|
"Connection: close\015\012"
|
2002-06-10 06:14:18 +00:00
|
|
|
"Content-Type: application/octet-stream\015\012"
|
|
|
|
"Content-Length: %d\015\012"
|
|
|
|
"\015\012",
|
2002-08-12 21:54:19 +00:00
|
|
|
file, host, offset, size
|
2002-06-10 06:14:18 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
sprintf(buf,
|
2002-08-12 21:54:19 +00:00
|
|
|
"PUT /%s HTTP/1.1\015\012"
|
2002-06-10 06:14:18 +00:00
|
|
|
"Pragma: no-cache\015\012"
|
|
|
|
"Cache-Control: no-cache\015\012"
|
|
|
|
"Host: %s:80\015\012"
|
2002-08-12 21:54:19 +00:00
|
|
|
"Connection: close\015\012"
|
2002-06-10 06:14:18 +00:00
|
|
|
"Content-Type: application/octet-stream\015\012"
|
|
|
|
"Content-Length: %d\015\012"
|
|
|
|
"\015\012",
|
|
|
|
file,
|
|
|
|
host, size
|
|
|
|
);
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Parse an http reply header into the header struct
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int read_http_reply_header(int socket, HTTP_REPLY_HEADER& header) {
|
2002-07-05 05:33:40 +00:00
|
|
|
int i, n;
|
2002-04-30 22:22:54 +00:00
|
|
|
char buf[1024], *p;
|
2002-07-11 01:09:53 +00:00
|
|
|
if(socket<0) {
|
|
|
|
fprintf(stderr, "error: read_http_reply_header: negative socket\n");
|
|
|
|
return ERR_NEG;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
header.content_length = 0;
|
|
|
|
header.status = 404; // default to failure
|
|
|
|
for (i=0; i<1024; i++) {
|
2002-07-05 05:33:40 +00:00
|
|
|
n = recv(socket, buf+i, 1, 0);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (strstr(buf, "\r\n\r\n") || strstr(buf, "\n\n")) {
|
|
|
|
if (log_flags.http_debug) printf("reply header:\n%s", buf);
|
|
|
|
p = strchr(buf, ' ');
|
|
|
|
if (p) {
|
|
|
|
header.status = atoi(p+1);
|
|
|
|
}
|
|
|
|
p = strstr(buf, "Content-Length: ");
|
|
|
|
if (p) {
|
|
|
|
header.content_length = atoi(p+strlen("Content-Length: "));
|
|
|
|
}
|
2002-08-02 18:31:20 +00:00
|
|
|
p = strstr(buf, "Location: ");
|
|
|
|
if (p) {
|
|
|
|
// TODO: Is there a better way to do this?
|
|
|
|
n = 0;
|
|
|
|
p += strlen( "Location: " );
|
|
|
|
|
|
|
|
while (p[n] != '\n' && p[n] != '\r') {
|
|
|
|
header.redirect_location[n] = p[n];
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
p[n] = '\0';
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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() {
|
2002-07-15 23:21:20 +00:00
|
|
|
strcpy(hostname,"");
|
|
|
|
strcpy(filename,"");
|
|
|
|
req1 = NULL;
|
|
|
|
strcpy(infile,"");
|
|
|
|
strcpy(outfile,"");
|
|
|
|
content_length = 0;
|
|
|
|
file_offset = 0;
|
|
|
|
strcpy(request_header,"");
|
|
|
|
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;
|
|
|
|
strcpy(proxy_server_name,"");
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HTTP_OP::~HTTP_OP() {
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Initialize HTTP HEAD operation to url
|
|
|
|
//
|
2002-06-10 06:14:18 +00:00
|
|
|
int HTTP_OP::init_head(char* url) {
|
2002-12-09 09:36:40 +00:00
|
|
|
char proxy_buf[256];
|
2002-12-09 03:07:36 +00:00
|
|
|
parse_url(url, hostname, port, filename);
|
2002-12-19 00:10:39 +00:00
|
|
|
NET_XFER::init(use_http_proxy?proxy_server_name: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) {
|
2002-12-09 09:36:40 +00:00
|
|
|
sprintf( proxy_buf, "http://%s:%d/%s", hostname, port, filename );
|
|
|
|
} else {
|
|
|
|
sprintf( proxy_buf, "/%s", filename );
|
|
|
|
}
|
|
|
|
http_head_request_header(request_header, hostname, port, proxy_buf);
|
2002-06-10 06:14:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Initialize HTTP GET operation to url
|
|
|
|
//
|
2002-08-07 22:52:10 +00:00
|
|
|
int HTTP_OP::init_get(char* url, char* out, bool del_old_file, double off) {
|
2002-12-09 09:36:40 +00:00
|
|
|
char proxy_buf[256];
|
|
|
|
|
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;
|
2002-12-09 03:07:36 +00:00
|
|
|
parse_url(url, hostname, port, filename);
|
2002-12-19 00:10:39 +00:00
|
|
|
NET_XFER::init(use_http_proxy?proxy_server_name:hostname, use_http_proxy?proxy_server_port:port, HTTP_BLOCKSIZE);
|
2002-04-30 22:22:54 +00:00
|
|
|
strcpy(outfile, out);
|
|
|
|
http_op_type = HTTP_OP_GET;
|
|
|
|
http_op_state = HTTP_STATE_CONNECTING;
|
2002-12-19 00:10:39 +00:00
|
|
|
if (use_http_proxy) {
|
2002-12-09 09:36:40 +00:00
|
|
|
sprintf( proxy_buf, "http://%s:%d/%s", hostname, port, filename );
|
|
|
|
} else {
|
|
|
|
sprintf( proxy_buf, "/%s", filename );
|
|
|
|
}
|
|
|
|
http_get_request_header(request_header, hostname, port, proxy_buf, (int)file_offset);
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Initialize HTTP POST operation to url
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int HTTP_OP::init_post(char* url, char* in, char* out) {
|
|
|
|
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
|
|
|
|
2002-12-09 03:07:36 +00:00
|
|
|
parse_url(url, hostname, port, filename);
|
2002-12-19 00:10:39 +00:00
|
|
|
NET_XFER::init(use_http_proxy?proxy_server_name:hostname, use_http_proxy?proxy_server_port:port, HTTP_BLOCKSIZE);
|
2002-04-30 22:22:54 +00:00
|
|
|
strcpy(infile, in);
|
|
|
|
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) {
|
2002-12-09 09:36:40 +00:00
|
|
|
sprintf( proxy_buf, "http://%s:%d/%s", hostname, port, filename );
|
|
|
|
} else {
|
|
|
|
sprintf( proxy_buf, "/%s", filename );
|
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
http_post_request_header(
|
2002-12-09 09:36:40 +00:00
|
|
|
request_header, hostname, port, proxy_buf, content_length
|
2002-07-05 05:33:40 +00:00
|
|
|
);
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Initialize HTTP POST operation to url including file offset
|
|
|
|
//
|
2002-07-05 05:33:40 +00:00
|
|
|
int HTTP_OP::init_post2(
|
|
|
|
char* url, char* r1, char* in, double offset
|
|
|
|
) {
|
|
|
|
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
|
|
|
|
2002-12-09 03:07:36 +00:00
|
|
|
parse_url(url, hostname, port, filename);
|
2002-12-19 00:10:39 +00:00
|
|
|
NET_XFER::init(use_http_proxy?proxy_server_name: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) {
|
|
|
|
strcpy(infile, in);
|
|
|
|
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) {
|
2002-12-09 09:36:40 +00:00
|
|
|
sprintf( proxy_buf, "http://%s:%d/%s", hostname, port, filename );
|
|
|
|
} else {
|
|
|
|
sprintf( proxy_buf, "/%s", filename );
|
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
http_post_request_header(
|
2002-12-09 09:36:40 +00:00
|
|
|
request_header, hostname, port, proxy_buf, content_length
|
2002-07-05 05:33:40 +00:00
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2002-09-20 21:17:47 +00:00
|
|
|
// not currently used
|
2002-06-10 06:14:18 +00:00
|
|
|
int HTTP_OP::init_put(char* url, char* in, int off) {
|
2002-04-30 22:22:54 +00:00
|
|
|
int retval;
|
|
|
|
|
2002-06-10 06:14:18 +00:00
|
|
|
offset = off;
|
2002-12-09 03:07:36 +00:00
|
|
|
parse_url(url, hostname, port, filename);
|
2002-12-19 00:10:39 +00:00
|
|
|
NET_XFER::init(use_http_proxy?proxy_server_name:hostname, use_http_proxy?proxy_server_port:port, HTTP_BLOCKSIZE);
|
2002-04-30 22:22:54 +00:00
|
|
|
strcpy(infile, in);
|
|
|
|
retval = file_size(infile, content_length);
|
|
|
|
if (retval) return retval;
|
|
|
|
http_op_type = HTTP_OP_PUT;
|
|
|
|
http_op_state = HTTP_STATE_CONNECTING;
|
2002-07-05 05:33:40 +00:00
|
|
|
http_put_request_header(
|
|
|
|
request_header, hostname, filename, content_length, offset
|
|
|
|
);
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-08-02 18:31:20 +00:00
|
|
|
// Inserts an HTTP_OP into 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;
|
|
|
|
|
|
|
|
for (i=0; i<http_ops.size(); i++) {
|
|
|
|
htp = http_ops[i];
|
|
|
|
switch(htp->http_op_state) {
|
|
|
|
case HTTP_STATE_CONNECTING:
|
2002-07-15 23:21:20 +00:00
|
|
|
// If the op is in the connecting state, and we notice it is done
|
|
|
|
// connecting, move it to the HTTP_STATE_REQUEST_HEADER state
|
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;
|
2002-07-05 05:33:40 +00:00
|
|
|
n = send(htp->socket, htp->request_header, strlen(htp->request_header), 0);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (log_flags.http_debug) {
|
2002-09-20 21:17:47 +00:00
|
|
|
printf("wrote HTTP header: %d bytes\n%s", n, htp->request_header);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
htp->io_ready = false;
|
|
|
|
switch(htp->http_op_type) {
|
|
|
|
case HTTP_OP_POST:
|
2002-07-05 05:33:40 +00:00
|
|
|
//case HTTP_OP_PUT:
|
2002-04-30 22:22:54 +00:00
|
|
|
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) {
|
|
|
|
fprintf(stderr, "HTTP_OP: no input file %s\n", htp->infile);
|
|
|
|
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:
|
2002-04-30 22:22:54 +00:00
|
|
|
htp->http_op_state = HTTP_STATE_REPLY_HEADER;
|
|
|
|
htp->want_upload = false;
|
|
|
|
htp->want_download = true;
|
|
|
|
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;
|
2002-08-12 21:54:19 +00:00
|
|
|
// If there's a file we also want to send, then start transferring
|
|
|
|
// it, otherwise, go on to the next step
|
|
|
|
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) {
|
|
|
|
fprintf(stderr, "HTTP_OP: no input2 file %s\n", htp->infile);
|
|
|
|
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;
|
|
|
|
if (log_flags.http_debug) {
|
|
|
|
printf("finished sending request body\n");
|
|
|
|
}
|
|
|
|
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;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
case HTTP_STATE_REPLY_HEADER:
|
|
|
|
if (htp->io_ready) {
|
|
|
|
action = true;
|
|
|
|
if (log_flags.http_debug) printf("got reply header\n");
|
|
|
|
read_http_reply_header(htp->socket, htp->hrh);
|
2002-08-02 18:31:20 +00:00
|
|
|
// TODO: handle all kinds of redirects here
|
2002-08-26 22:57:17 +00:00
|
|
|
if (htp->hrh.status == HTTP_STATUS_MOVED_PERM || htp->hrh.status == HTTP_STATUS_MOVED_TEMP) {
|
2002-08-02 18:31:20 +00:00
|
|
|
// Close the old socket
|
|
|
|
htp->close_socket();
|
|
|
|
switch (htp->http_op_type) {
|
|
|
|
case HTTP_OP_HEAD:
|
|
|
|
htp->init_head( htp->hrh.redirect_location );
|
|
|
|
break;
|
|
|
|
case HTTP_OP_GET:
|
2002-08-07 22:52:10 +00:00
|
|
|
// *** Not sure if delete_old_file should be true
|
|
|
|
htp->init_get( htp->hrh.redirect_location, htp->outfile, true );
|
2002-08-02 18:31:20 +00:00
|
|
|
break;
|
|
|
|
case HTTP_OP_POST:
|
|
|
|
htp->init_post( htp->hrh.redirect_location, htp->infile, htp->outfile );
|
|
|
|
break;
|
|
|
|
case HTTP_OP_POST2:
|
|
|
|
// TODO: Change offset to correct value
|
2002-08-12 21:54:19 +00:00
|
|
|
htp->init_post2( htp->hrh.redirect_location, htp->req1, htp->infile, 0 );
|
2002-08-02 18:31:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Open connection to the redirected server
|
|
|
|
htp->open_server();
|
|
|
|
break;
|
|
|
|
}
|
2002-08-26 22:57:17 +00:00
|
|
|
if ((htp->hrh.status/100)*100 != HTTP_STATUS_OK) {
|
2002-04-30 22:22:54 +00:00
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
|
|
|
htp->http_op_retval = htp->hrh.status;
|
|
|
|
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;
|
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// Append to a file if it already exists, otherwise
|
|
|
|
// create a new one. init_get should have already
|
|
|
|
// deleted the file if necessary
|
2002-08-15 22:03:41 +00:00
|
|
|
htp->file = fopen(htp->outfile, "ab");
|
2002-04-30 22:22:54 +00:00
|
|
|
if (!htp->file) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"HTTP_OP: can't open output file %s\n",
|
|
|
|
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;
|
|
|
|
break;
|
|
|
|
#if 0
|
2002-04-30 22:22:54 +00:00
|
|
|
case HTTP_OP_PUT:
|
|
|
|
htp->http_op_state = HTTP_STATE_DONE;
|
|
|
|
htp->http_op_retval = 0;
|
2002-07-05 05:33:40 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HTTP_STATE_REPLY_BODY:
|
|
|
|
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;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
if (log_flags.http_debug) printf("got reply body\n");
|
|
|
|
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
|
|
|
}
|
2002-08-20 00:30:13 +00:00
|
|
|
fprintf(stdout, "HTTP_OP_SET::remove(): not found\n");
|
2002-04-30 22:22:54 +00:00
|
|
|
return 1;
|
|
|
|
}
|