test-http-payload.c revision d5cdf90f01fe1b5a5e049f341398b2ee329b4de5
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch/* Copyright (c) 2013-2016 Dovecot authors, see the included COPYING file */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include "lib.h"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include "str.h"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include "llist.h"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include "abspath.h"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include "hostpid.h"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include "ioloop.h"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include "istream.h"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include "ostream.h"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include "iostream-temp.h"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include "connection.h"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include "test-common.h"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include "http-url.h"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include "http-request.h"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include "http-server.h"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include "http-client.h"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include <sys/types.h>
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include <sys/stat.h>
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include <sys/wait.h>
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include <signal.h>
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include <fcntl.h>
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include <unistd.h>
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch#include <dirent.h>
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic bool blocking = FALSE;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic bool debug = FALSE;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic bool request_100_continue = FALSE;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic unsigned int test_max_pending = 200;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic struct ip_addr bind_ip;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic in_port_t bind_port = 0;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic int fd_listen = -1;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch/*
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch * Test files
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic ARRAY_TYPE(const_string) files;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic pool_t files_pool;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_files_read_dir(const char *path)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch DIR *dirp;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* open the directory */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if ((dirp = opendir(path)) == NULL) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (errno == ENOENT || errno == EACCES)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test files: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "failed to open directory %s: %m", path);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* read entries */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch for (;;) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char *file;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct dirent *dp;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct stat st;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch errno = 0;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if ((dp=readdir(dirp)) == NULL)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch break;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (*dp->d_name == '.')
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch continue;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch file = t_abspath_to(dp->d_name, path);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (stat(file, &st) == 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (S_ISREG(st.st_mode)) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch file += 2; /* skip "./" */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch file = p_strdup(files_pool, file);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch array_append(&files, &file, 1);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch } else {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_files_read_dir(file);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (errno != 0)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test files: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "failed to read directory %s: %m", path);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* Close the directory */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (closedir(dirp) < 0)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_error("test files: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "failed to close directory %s: %m", path);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_files_init(void)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* initialize file array */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch files_pool = pool_alloconly_create
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (MEMPOOL_GROWING"http_server_request", 4096);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch p_array_init(&files, files_pool, 512);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* obtain all filenames */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_files_read_dir(".");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_files_deinit(void)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch pool_unref(&files_pool);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic struct istream *
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschtest_file_open(const char *path,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch unsigned int *status_r, const char **reason_r)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch ATTR_NULL(2, 3)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch int fd;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (status_r != NULL)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch *status_r = 200;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (reason_r != NULL)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch *reason_r = "OK";
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch fd = open(path, O_RDONLY);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (fd < 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test files: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "open(%s) failed: %m", path);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch switch (errno) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch case EFAULT:
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch case ENOENT:
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (status_r != NULL)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch *status_r = 404;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (reason_r != NULL)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch *reason_r = "Not Found";
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch break;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch case EISDIR:
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch case EACCES:
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (status_r != NULL)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch *status_r = 403;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (reason_r != NULL)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch *reason_r = "Forbidden";
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch break;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch default:
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (status_r != NULL)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch *status_r = 500;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (reason_r != NULL)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch *reason_r = "Internal Server Error";
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return NULL;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return i_stream_create_fd(fd, 40960, TRUE);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch/*
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch * Test server
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstruct client {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch pool_t pool;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct client *prev, *next;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_server_connection *http_conn;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch};
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstruct client_request {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct client *client;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_server_request *server_req;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char *path;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct istream *payload_input;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct ostream *payload_output;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct io *io;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch};
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic const struct http_server_callbacks http_callbacks;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic struct http_server *http_server;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic struct io *io_listen;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic struct client *clients;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch/* location: /succes */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschclient_handle_success_request(struct client_request *creq)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_server_request *req = creq->server_req;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const struct http_request *hreq =
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_get(req);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_server_response *resp;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (strcmp(hreq->method, "GET") != 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_fail(req,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch 405, "Method Not Allowed");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch resp = http_server_response_create(req, 200, "OK");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_response_submit(resp);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch/* location: /download/... */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschclient_handle_download_request(
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct client_request *creq,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char *path)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_server_request *req = creq->server_req;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const struct http_request *hreq =
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_get(req);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_server_response *resp;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char *fpath, *reason;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct istream *fstream;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct ostream *output;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch unsigned int status;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch int ret;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (strcmp(hreq->method, "GET") != 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_fail(req,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch 405, "Method Not Allowed");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch fpath = t_strconcat(".", path, NULL);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test server: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "sending payload for %s", fpath);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch fstream = test_file_open(fpath, &status, &reason);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (fstream == NULL) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_fail(req, status, reason);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch resp = http_server_response_create(req, 200, "OK");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_response_add_header(resp, "Content-Type", "text/plain");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (blocking) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch output = http_server_response_get_payload_output(resp, TRUE);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch while ((ret=o_stream_send_istream (output, fstream)) > 0);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (ret < 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test server: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "failed to send blocking file payload");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test server: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "finished sending blocking payload for %s"
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "(%"PRIuUOFF_T":%"PRIuUOFF_T")",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch fpath, fstream->v_offset, output->offset);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch o_stream_close(output);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch o_stream_unref(&output);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch } else {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_response_set_payload(resp, fstream);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_response_submit(resp);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_unref(&fstream);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch/* location: /echo */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschclient_request_read_echo_more(struct client_request *creq)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_server_response *resp;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct istream *payload_input;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch off_t ret;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch o_stream_set_max_buffer_size(creq->payload_output, IO_BLOCK_SIZE);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch ret = o_stream_send_istream(creq->payload_output, creq->payload_input);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch o_stream_set_max_buffer_size(creq->payload_output, (size_t)-1);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (ret < 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (creq->payload_output->stream_errno != 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test server: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "Failed to write all echo payload [%s]", creq->path);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (creq->payload_input->stream_errno != 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test server: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "Failed to read all echo payload [%s]", creq->path);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_unreached();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (i_stream_have_bytes_left(creq->payload_input))
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch io_remove(&creq->io);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_unref(&creq->payload_input);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test server: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "finished receiving payload for %s", creq->path);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch payload_input = iostream_temp_finish(&creq->payload_output, 4096);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch resp = http_server_response_create
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (creq->server_req, 200, "OK");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_response_add_header(resp, "Content-Type", "text/plain");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_response_set_payload(resp, payload_input);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_response_submit(resp);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_unref(&payload_input);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschclient_handle_echo_request(struct client_request *creq,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char *path)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_server_request *req = creq->server_req;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const struct http_request *hreq =
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_get(req);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_server_response *resp;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct ostream *payload_output;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch uoff_t size;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch int ret;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch creq->path = p_strdup
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (http_server_request_get_pool(req), path);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (strcmp(hreq->method, "PUT") != 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_fail(req,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch 405, "Method Not Allowed");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch size = 0;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (void)http_request_get_payload_size(hreq, &size);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (size == 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch resp = http_server_response_create
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (creq->server_req, 200, "OK");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_response_add_header(resp, "Content-Type", "text/plain");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_response_submit(resp);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch payload_output = iostream_temp_create
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch ("/tmp/test-http-server", 0);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (blocking) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct istream *payload_input;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch payload_input =
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_get_payload_input(req, TRUE);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch while ((ret=o_stream_send_istream
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (payload_output, payload_input)) > 0);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (ret < 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test server: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "failed to receive blocking echo payload");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_unref(&payload_input);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch payload_input = iostream_temp_finish(&payload_output, 4096);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test server: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "finished receiving blocking payload for %s", path);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch resp = http_server_response_create(req, 200, "OK");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_response_add_header(resp, "Content-Type", "text/plain");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch payload_output = http_server_response_get_payload_output(resp, TRUE);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch while ((ret=o_stream_send_istream
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (payload_output, payload_input)) > 0);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (ret < 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test server: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "failed to send blocking echo payload");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test server: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "finished sending blocking payload for %s", path);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_unref(&payload_input);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch o_stream_close(payload_output);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch o_stream_unref(&payload_output);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch } else {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch creq->payload_output = payload_output;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch creq->payload_input =
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_get_payload_input(req, FALSE);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch creq->io = io_add_istream(creq->payload_input,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client_request_read_echo_more, creq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client_request_read_echo_more(creq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch/* request */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschhttp_server_request_destroyed(void *context);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic struct client_request *
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschclient_request_init(struct client *client,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_server_request *req)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct client_request *creq;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch pool_t pool = http_server_request_get_pool(req);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_ref(req);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch creq = p_new(pool, struct client_request, 1);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch creq->client = client;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch creq->server_req = req;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_set_destroy_callback(req,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_destroyed, creq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return creq;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void client_request_deinit(struct client_request **_creq)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct client_request *creq = *_creq;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_server_request *req = creq->server_req;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch *_creq = NULL;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (creq->io != NULL) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_unref(&creq->payload_input);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch io_remove(&creq->io);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_unref(&req);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschhttp_server_request_destroyed(void *context)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct client_request *creq =
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (struct client_request *)context;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client_request_deinit(&creq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschclient_handle_request(void *context,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_server_request *req)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const struct http_request *hreq =
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_get(req);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char *path = hreq->target.url->path, *p;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct client *client = (struct client *)context;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct client_request *creq;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test server: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "request method=`%s' path=`%s'", hreq->method, path);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch creq = client_request_init(client, req);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (strcmp(path, "/success") == 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client_handle_success_request(creq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if ((p=strchr(path+1, '/')) == NULL) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_fail(req, 404, "Not found");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (strncmp(path, "/download", p-path) == 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client_handle_download_request(creq, p);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (strncmp(path, "/echo", p-path) == 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client_handle_echo_request(creq, p);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_request_fail(req, 404, "Not found");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch/* client connection */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschclient_connection_destroy(void *context, const char *reason);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic const struct http_server_callbacks http_callbacks = {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch .connection_destroy = client_connection_destroy,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch .handle_request = client_handle_request
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch};
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void client_init(int fd)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct client *client;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch pool_t pool;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch net_set_nonblock(fd, TRUE);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch pool = pool_alloconly_create("client", 256);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client = p_new(pool, struct client, 1);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client->pool = pool;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client->http_conn = http_server_connection_create(http_server,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch fd, fd, FALSE, &http_callbacks, client);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch DLLIST_PREPEND(&clients, client);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void client_deinit(struct client **_client)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct client *client = *_client;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch *_client = NULL;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch DLLIST_REMOVE(&clients, client);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (client->http_conn != NULL)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_connection_close(&client->http_conn, "deinit");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch pool_unref(&client->pool);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschclient_connection_destroy(void *context, const char *reason ATTR_UNUSED)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct client *client = context;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client->http_conn = NULL;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client_deinit(&client);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void client_accept(void *context ATTR_UNUSED)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch int fd;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* accept new client */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch fd = net_accept(fd_listen, NULL, NULL);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (fd == -1)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (fd == -2) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test server: accept() failed: %m");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client_init(fd);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch/* */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschtest_server_init(const struct http_server_settings *server_set)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* open server socket */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch io_listen = io_add(fd_listen,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch IO_READ, client_accept, (void *)NULL);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server = http_server_init(server_set);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_server_deinit(void)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* close server socket */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch io_remove(&io_listen);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* deinitialize */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_deinit(&http_server);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch/*
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch * Test client
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstruct test_client_request {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct io *io;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct istream *payload;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct istream *file;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch unsigned int files_idx;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch};
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic struct http_client *http_client;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic unsigned int client_files_first, client_files_last;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschtest_client_request_destroy(void *context)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct test_client_request *tcreq =
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (struct test_client_request *)context;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (tcreq->io != NULL)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch io_remove(&tcreq->io);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (tcreq->payload != NULL)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_unref(&tcreq->payload);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (tcreq->file != NULL)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_unref(&tcreq->file);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_free(tcreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch/* download */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_client_download_continue(void);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschtest_client_download_finished(struct test_client_request *tcreq)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char **paths;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch unsigned int count;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch paths = array_get_modifiable(&files, &count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(tcreq->files_idx < count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(client_files_first < count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(paths[tcreq->files_idx] != NULL);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch paths[tcreq->files_idx] = NULL;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_download_continue();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschtest_client_download_payload_input(struct test_client_request *tcreq)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct istream *payload = tcreq->payload;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const unsigned char *pdata, *fdata;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch size_t psize, fsize, pleft;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch off_t ret;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* read payload */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch while ((ret=i_stream_read_data
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (payload, &pdata, &psize, 0)) > 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "got data for [%u] (size=%d)",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->files_idx, (int)psize);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* compare with file on disk */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch pleft = psize;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch while ((ret=i_stream_read_data
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (tcreq->file, &fdata, &fsize, 0)) > 0 && pleft > 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch fsize = (fsize > pleft ? pleft : fsize);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (memcmp(pdata, fdata, fsize) != 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test client: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "received data does not match file "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "(%"PRIuUOFF_T":%"PRIuUOFF_T")",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch payload->v_offset, tcreq->file->v_offset);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_skip(tcreq->file, fsize);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch pleft -= fsize;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch pdata += fsize;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (ret < 0 && tcreq->file->stream_errno != 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test client: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "failed to read file: %s", i_stream_get_error(tcreq->file));
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_skip(payload, psize);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (ret == 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "need more data for [%u]",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->files_idx);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* we will be called again for this request */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch } else {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (void)i_stream_read(tcreq->file);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (payload->stream_errno != 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test client: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "failed to read request payload: %s",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_get_error(payload));
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch } if (i_stream_have_bytes_left(tcreq->file)) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (i_stream_read_data(tcreq->file, &fdata, &fsize, 0) <= 0)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch fsize = 0;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test client: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "payload ended prematurely "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "(at least %"PRIuSIZE_T" bytes left)", fsize);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch } else if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "finished request for [%u]",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->files_idx);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* finished */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_download_finished(tcreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* dereference payload stream; finishes the request */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->payload = NULL;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch io_remove(&tcreq->io); /* holds a reference too */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_unref(&payload);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschtest_client_download_response(const struct http_response *resp,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct test_client_request *tcreq)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char **paths;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char *path;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch unsigned int count, status;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct istream *fstream;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char *reason;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "got response for [%u]",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->files_idx);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch paths = array_get_modifiable(&files, &count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(tcreq->files_idx < count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(client_files_first < count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch path = paths[tcreq->files_idx];
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(path != NULL);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "path for [%u]: %s",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->files_idx, path);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch fstream = test_file_open(path, &status, &reason);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (status != resp->status) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test client: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "got wrong response for %s: %u %s (expected: %u %s)",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch path, resp->status, resp->reason, status, reason);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (resp->status / 100 != 2) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "HTTP request for %s failed: %u %s",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch path, resp->status, resp->reason);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_unref(&fstream);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_download_finished(tcreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (resp->payload == NULL) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "no payload for %s [%u]",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch path, tcreq->files_idx);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_unref(&fstream);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_download_finished(tcreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(fstream != NULL);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->file = fstream;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_ref(resp->payload);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->payload = resp->payload;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->io = io_add_istream(resp->payload,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_download_payload_input, tcreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_download_payload_input(tcreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_client_download_continue(void)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct test_client_request *tcreq;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_client_request *hreq;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char *const *paths;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch unsigned int count;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch paths = array_get(&files, &count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(client_files_first <= count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(client_files_last <= count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(client_files_first <= client_files_last);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch for (; client_files_first < client_files_last &&
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch paths[client_files_first] == NULL; client_files_first++)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "received until [%u]",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client_files_first-1);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (client_files_first >= count) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch io_loop_stop(current_ioloop);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch for (; client_files_last < count &&
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (client_files_last - client_files_first) < test_max_pending;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client_files_last++) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char *path = paths[client_files_last];
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq = i_new(struct test_client_request, 1);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->files_idx = client_files_last;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: download: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "retrieving %s [%u]",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch path, tcreq->files_idx);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch hreq = http_client_request(http_client,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "GET", net_ip2addr(&bind_ip),
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch t_strconcat("/download/", path, NULL),
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_download_response, tcreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_request_set_port(hreq, bind_port);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_request_set_destroy_callback(hreq,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_request_destroy, tcreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_request_submit(hreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschtest_client_download(const struct http_client_settings *client_set)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* create client */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client = http_client_init(client_set);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* start querying server */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client_files_first = client_files_last = 0;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_download_continue();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch/* echo */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_client_echo_continue(void);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschtest_client_echo_finished(struct test_client_request *tcreq)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char **paths;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch unsigned int count;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch paths = array_get_modifiable(&files, &count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(tcreq->files_idx < count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(client_files_first < count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(paths[tcreq->files_idx] != NULL);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch paths[tcreq->files_idx] = NULL;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_echo_continue();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschtest_client_echo_payload_input(struct test_client_request *tcreq)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct istream *payload = tcreq->payload;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const unsigned char *pdata, *fdata;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch size_t psize, fsize, pleft;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch off_t ret;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* read payload */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch while ((ret=i_stream_read_data
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (payload, &pdata, &psize, 0)) > 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "got data for [%u] (size=%d)",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->files_idx, (int)psize);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* compare with file on disk */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch pleft = psize;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch while ((ret=i_stream_read_data
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (tcreq->file, &fdata, &fsize, 0)) > 0 && pleft > 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch fsize = (fsize > pleft ? pleft : fsize);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (memcmp(pdata, fdata, fsize) != 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test client: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "received data does not match file "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "(%"PRIuUOFF_T":%"PRIuUOFF_T")",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch payload->v_offset, tcreq->file->v_offset);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_skip(tcreq->file, fsize);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch pleft -= fsize;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch pdata += fsize;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (ret < 0 && tcreq->file->stream_errno != 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test client: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "failed to read file: %s", i_stream_get_error(tcreq->file));
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_skip(payload, psize);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (ret == 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "need more data for [%u]",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->files_idx);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* we will be called again for this request */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch } else {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (void)i_stream_read(tcreq->file);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (payload->stream_errno != 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test client: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "failed to read request payload: %s",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_get_error(payload));
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch } if (i_stream_have_bytes_left(tcreq->file)) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (i_stream_read_data(tcreq->file, &fdata, &fsize, 0) <= 0)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch fsize = 0;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test client: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "payload ended prematurely "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "(at least %"PRIuSIZE_T" bytes left)", fsize);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch } else if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "finished request for [%u]",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->files_idx);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* finished */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_echo_finished(tcreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* dereference payload stream; finishes the request */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->payload = NULL;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch io_remove(&tcreq->io); /* holds a reference too */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_unref(&payload);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschtest_client_echo_response(const struct http_response *resp,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct test_client_request *tcreq)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char **paths;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char *path;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch unsigned int count, status;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct istream *fstream;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "got response for [%u]",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->files_idx);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch paths = array_get_modifiable(&files, &count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(tcreq->files_idx < count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(client_files_first < count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch path = paths[tcreq->files_idx];
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(path != NULL);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "path for [%u]: %s",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->files_idx, path);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (resp->status / 100 != 2) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test client: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "HTTP request for %s failed: %u %s",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch path, resp->status, resp->reason);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch fstream = test_file_open(path, &status, NULL);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (fstream == NULL) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("test client: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "failed to open %s", path);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (resp->payload == NULL) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch // FIXME: check file is empty
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "no payload for %s [%u]",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch path, tcreq->files_idx);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_unref(&fstream);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_echo_finished(tcreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(fstream != NULL);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->file = fstream;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_ref(resp->payload);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->payload = resp->payload;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->io = io_add_istream(resp->payload,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_echo_payload_input, tcreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_echo_payload_input(tcreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_client_echo_continue(void)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct test_client_request *tcreq;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_client_request *hreq;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char **paths;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch unsigned int count;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch paths = array_get_modifiable(&files, &count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(client_files_first <= count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(client_files_last <= count);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_assert(client_files_first <= client_files_last);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch for (; client_files_first < client_files_last &&
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch paths[client_files_first] == NULL; client_files_first++)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "received until [%u]",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client_files_first-1);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (client_files_first >= count) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch io_loop_stop(current_ioloop);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch return;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch for (; client_files_last < count &&
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (client_files_last - client_files_first) < test_max_pending;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client_files_last++) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct istream *fstream;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const char *path = paths[client_files_last];
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch fstream = test_file_open(path, NULL, NULL);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (fstream == NULL) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch paths[client_files_last] = NULL;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "skipping %s [%u]",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch path, client_files_last);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch continue;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("test client: echo: "
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "retrieving %s [%u]",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch path, client_files_last);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq = i_new(struct test_client_request, 1);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch tcreq->files_idx = client_files_last;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch hreq = http_client_request(http_client,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch "PUT", net_ip2addr(&bind_ip),
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch t_strconcat("/echo/", path, NULL),
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_echo_response, tcreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_request_set_port(hreq, bind_port);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_request_set_payload
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (hreq, fstream, request_100_continue);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_request_set_destroy_callback(hreq,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_request_destroy, tcreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_request_submit(hreq);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_stream_unref(&fstream);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschtest_client_echo(const struct http_client_settings *client_set)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* create client */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client = http_client_init(client_set);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* start querying server */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client_files_first = client_files_last = 0;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_echo_continue();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch/* cleanup */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_client_deinit(void)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_deinit(&http_client);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch/*
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch * Tests
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_open_server_fd(void)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (fd_listen != -1)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_close_fd(&fd_listen);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch fd_listen = net_listen(&bind_ip, &bind_port, 128);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (fd_listen == -1) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("listen(%s:%u) failed: %m",
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch net_ip2addr(&bind_ip), bind_port);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_run_client_server(
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const struct http_client_settings *client_set,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch const struct http_server_settings *server_set,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch void (*client_init)(const struct http_client_settings *client_set))
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct ioloop *ioloop;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch pid_t pid;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch int status;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_open_server_fd();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if ((pid = fork()) == (pid_t)-1)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_fatal("fork() failed: %m");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (pid == 0) {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch hostpid_init();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("server: PID=%s", my_pid);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* child: server */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch ioloop = io_loop_create();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_server_init(server_set);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch io_loop_run(ioloop);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_server_deinit();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch io_loop_destroy(&ioloop);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_close_fd(&fd_listen);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch } else {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch if (debug)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_debug("client: PID=%s", my_pid);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch i_close_fd(&fd_listen);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* parent: client */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch ioloop = io_loop_create();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch client_init(client_set);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch io_loop_run(ioloop);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_client_deinit();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch io_loop_destroy(&ioloop);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (void)kill(pid, SIGKILL);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (void)waitpid(pid, &status, 0);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch }
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_run_sequential(
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch void (*client_init)(const struct http_client_settings *client_set))
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_server_settings http_server_set;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_client_settings http_client_set;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* download files from blocking server */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* server settings */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch memset(&http_server_set, 0, sizeof(http_server_set));
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_set.max_pipelined_requests = 0;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_set.debug = debug;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_set.request_limits.max_payload_size = (uoff_t)-1;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* client settings */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch memset(&http_client_set, 0, sizeof(http_client_set));
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.max_idle_time_msecs = 5*1000;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.max_parallel_connections = 1;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.max_pipelined_requests = 1;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.max_redirects = 0;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.max_attempts = 1;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.debug = debug;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_files_init();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_client_server
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (&http_client_set, &http_server_set, client_init);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_files_deinit();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_run_pipeline(
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch void (*client_init)(const struct http_client_settings *client_set))
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_server_settings http_server_set;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_client_settings http_client_set;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* download files from blocking server */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* server settings */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch memset(&http_server_set, 0, sizeof(http_server_set));
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_set.max_pipelined_requests = 4;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_set.debug = debug;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_set.request_limits.max_payload_size = (uoff_t)-1;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* client settings */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch memset(&http_client_set, 0, sizeof(http_client_set));
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.max_idle_time_msecs = 5*1000;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.max_parallel_connections = 1;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.max_pipelined_requests = 4;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.max_redirects = 0;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.max_attempts = 1;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.debug = debug;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_files_init();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_client_server
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (&http_client_set, &http_server_set, client_init);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_files_deinit();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_run_parallel(
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch void (*client_init)(const struct http_client_settings *client_set))
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_server_settings http_server_set;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch struct http_client_settings http_client_set;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* download files from blocking server */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* server settings */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch memset(&http_server_set, 0, sizeof(http_server_set));
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_set.max_pipelined_requests = 4;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_set.debug = debug;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_server_set.request_limits.max_payload_size = (uoff_t)-1;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* client settings */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch memset(&http_client_set, 0, sizeof(http_client_set));
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.max_idle_time_msecs = 5*1000;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.max_parallel_connections = 40;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.max_pipelined_requests = 4;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.max_redirects = 0;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.max_attempts = 1;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch http_client_set.debug = debug;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_files_init();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_client_server
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch (&http_client_set, &http_server_set, client_init);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_files_deinit();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_download_server_nonblocking(void)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_begin("http payload download (server non-blocking)");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch blocking = FALSE;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_sequential(test_client_download);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_pipeline(test_client_download);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_parallel(test_client_download);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_end();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_download_server_blocking(void)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_begin("http payload download (server blocking)");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch blocking = TRUE;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_sequential(test_client_download);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_pipeline(test_client_download);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_parallel(test_client_download);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_end();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_echo_server_nonblocking(void)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_begin("http payload echo (server non-blocking");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch blocking = FALSE;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_sequential(test_client_echo);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_pipeline(test_client_echo);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_parallel(test_client_echo);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_end();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_echo_server_blocking(void)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_begin("http payload echo (server blocking)");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch blocking = TRUE;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_sequential(test_client_echo);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_pipeline(test_client_echo);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_parallel(test_client_echo);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_end();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_echo_server_nonblocking_sync(void)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_begin("http payload echo (server non-blocking; 100-continue)");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch request_100_continue = TRUE;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch blocking = FALSE;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_sequential(test_client_echo);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_pipeline(test_client_echo);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_parallel(test_client_echo);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_end();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void test_echo_server_blocking_sync(void)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_begin("http payload echo (server blocking; 100-continue)");
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch request_100_continue = TRUE;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch blocking = TRUE;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_sequential(test_client_echo);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_pipeline(test_client_echo);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run_parallel(test_client_echo);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_end();
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschstatic void (*test_functions[])(void) = {
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_download_server_nonblocking,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_download_server_blocking,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_echo_server_nonblocking,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_echo_server_blocking,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_echo_server_nonblocking_sync,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_echo_server_blocking_sync,
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch NULL
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch};
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch/*
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch * Main
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Boschint main(void)
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch{
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch /* listen on localhost */
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch memset(&bind_ip, 0, sizeof(bind_ip));
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch bind_ip.family = AF_INET;
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch bind_ip.u.ip4.s_addr = htonl(INADDR_LOOPBACK);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch test_run(test_functions);
d5cdf90f01fe1b5a5e049f341398b2ee329b4de5Stephan Bosch}