1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

1008: Remember the sizes of files during the upload.

This will soon allow read_http_post_fd() to detect if a size has
changed.
This commit is contained in:
Kalle Olavi Niemitalo 2008-06-03 00:46:12 +03:00 committed by Kalle Olavi Niemitalo
parent c2d854b240
commit 44bf538cab
4 changed files with 47 additions and 11 deletions

View File

@ -111,13 +111,12 @@ send_post_data(struct connection *conn)
struct http_connection_info *http = conn->info;
unsigned char *post = conn->uri->post;
unsigned char *postend;
unsigned int files;
enum connection_state error;
postend = strchr(post, '\n');
if (postend) post = postend + 1;
if (!open_http_post(&http->post, post, &files, &error))
if (!open_http_post(&http->post, post, &error))
abort_connection(conn, error);
else
send_more_post_data(conn->data_socket);

View File

@ -637,7 +637,6 @@ http_send_header(struct socket *socket)
struct uri *uri = conn->proxied_uri; /* Set to the real uri */
unsigned char *optstr;
int use_connect, talking_to_proxy;
unsigned int files = 0;
/* Sanity check for a host */
if (!uri || !uri->host || !*uri->host || !uri->hostlen) {
@ -980,7 +979,7 @@ http_send_header(struct socket *socket)
}
post_data = postend ? postend + 1 : uri->post;
if (!open_http_post(&http->post, post_data, &files, &error)) {
if (!open_http_post(&http->post, post_data, &error)) {
http_end_request(conn, error, 0);
done_string(&header);
return;
@ -1014,7 +1013,7 @@ http_send_header(struct socket *socket)
assert(!use_connect); /* see comment above */
socket->state = SOCKET_END_ONCLOSE;
if (!conn->upload_progress && files)
if (!conn->upload_progress && http->post.file_count)
conn->upload_progress = init_progress(0);
write_to_socket(socket, header.source, header.length, S_TRANS,
send_more_post_data);

View File

@ -33,6 +33,9 @@ init_http_post(struct http_post *http_post)
http_post->uploaded = 0;
http_post->post_data = NULL;
http_post->post_fd = -1;
http_post->file_index = 0;
http_post->file_count = 0;
http_post->files = NULL;
}
/** Free all resources owned by *@a http_post, but do not free the
@ -49,6 +52,9 @@ done_http_post(struct http_post *http_post)
close(http_post->post_fd);
http_post->post_fd = -1;
}
http_post->file_index = 0;
http_post->file_count = 0;
mem_free_set(&http_post->files, NULL);
}
/** Prepare to read POST data from a URI and possibly to upload files.
@ -60,8 +66,6 @@ done_http_post(struct http_post *http_post)
* However, unlike uri.post, @a post_data must not contain any
* Content-Type. The caller must ensure that the @a post_data
* pointer remains valid until done_http_post().
* @param[out] files
* The number of files going to be uploaded.
* @param[out] error
* If the function fails, it writes the error state here so that
* the caller can pass that on to abort_connection(). If the
@ -78,7 +82,7 @@ done_http_post(struct http_post *http_post)
* @relates http_post */
int
open_http_post(struct http_post *http_post, unsigned char *post_data,
unsigned int *files, enum connection_state *error)
enum connection_state *error)
{
off_t size = 0;
size_t length = strlen(post_data);
@ -87,11 +91,11 @@ open_http_post(struct http_post *http_post, unsigned char *post_data,
done_http_post(http_post);
http_post->post_data = end;
*files = 0;
while (1) {
struct stat sb;
unsigned char *begin;
int res;
struct http_post_file *new_files;
begin = strchr(end, FILE_CHAR);
if (!begin) break;
@ -101,7 +105,22 @@ open_http_post(struct http_post *http_post, unsigned char *post_data,
res = stat(begin + 1, &sb);
*end = FILE_CHAR;
if (res) break;
(*files)++;
/* This use of mem_realloc() in a loop consumes O(n^2)
* time but how many files are you really going to
* upload in one request? */
new_files = mem_realloc(http_post->files,
(http_post->file_count + 1)
* sizeof(*new_files));
if (new_files == NULL) {
done_http_post(http_post);
*error = S_OUT_OF_MEM;
return 0;
}
http_post->files = new_files;
new_files[http_post->file_count].size = sb.st_size;
http_post->file_count++;
size += sb.st_size;
length -= (end - begin + 1);
end++;

View File

@ -6,6 +6,14 @@
#include "network/state.h"
/** Information about a file to be uploaded in a POST request.
* open_http_post() collects this information and done_http_post()
* discards it. */
struct http_post_file {
/** The size of the file. */
off_t size;
};
/** State of reading POST data from connection.uri->post and related
* files. */
struct http_post {
@ -24,12 +32,23 @@ struct http_post {
/** File descriptor from which data is being read, or -1 if
* none. */
int post_fd;
/** Current position in the #files array. This is the file
* that is currently being read (when post_fd != -1) or would
* be read next (when post_fd == -1). */
size_t file_index;
/** Number of elements in the #files array. */
size_t file_count;
/** Array of information about files to be uploaded. */
struct http_post_file *files;
};
void init_http_post(struct http_post *http_post);
void done_http_post(struct http_post *http_post);
int open_http_post(struct http_post *http_post, unsigned char *post_data,
unsigned int *files, enum connection_state *error);
enum connection_state *error);
int read_http_post(struct http_post *http_post,
unsigned char buffer[], int max);