2020-07-08 05:24:40 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
2022-06-11 05:32:01 +00:00
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
2020-07-08 05:24:40 +00:00
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "http-prv.h"
|
2021-07-22 07:30:20 +00:00
|
|
|
#include <hio-pro.h>
|
|
|
|
#include <hio-fmt.h>
|
|
|
|
#include <hio-chr.h>
|
2020-07-08 05:24:40 +00:00
|
|
|
|
|
|
|
#include <unistd.h> /* TODO: move file operations to sys-file.XXX */
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
2020-07-09 09:54:13 +00:00
|
|
|
#include <errno.h>
|
2020-11-15 04:02:47 +00:00
|
|
|
#include <netinet/in.h>
|
2020-07-20 14:08:25 +00:00
|
|
|
#include <netinet/tcp.h>
|
2020-07-08 05:24:40 +00:00
|
|
|
|
|
|
|
#define FILE_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH
|
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
#define FILE_OVER_READ_FROM_CLIENT (1 << 0)
|
|
|
|
#define FILE_OVER_READ_FROM_PEER (1 << 1)
|
|
|
|
#define FILE_OVER_WRITE_TO_CLIENT (1 << 2)
|
|
|
|
#define FILE_OVER_WRITE_TO_PEER (1 << 3)
|
|
|
|
#define FILE_OVER_ALL (FILE_OVER_READ_FROM_CLIENT | FILE_OVER_READ_FROM_PEER | FILE_OVER_WRITE_TO_CLIENT | FILE_OVER_WRITE_TO_PEER)
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
struct file_t
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2023-01-11 15:41:01 +00:00
|
|
|
HIO_SVC_HTTS_TASK_HEADER;
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-03-04 15:55:15 +00:00
|
|
|
hio_svc_htts_task_on_kill_t on_kill; /* user-provided on_kill callback */
|
|
|
|
|
2022-10-09 16:41:07 +00:00
|
|
|
int options;
|
2022-12-21 14:57:52 +00:00
|
|
|
hio_svc_htts_file_cbs_t* cbs;
|
2023-03-25 18:01:40 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t num_pending_writes_to_peer;
|
2020-07-19 18:55:35 +00:00
|
|
|
int sendfile_ok;
|
2020-07-08 05:24:40 +00:00
|
|
|
int peer;
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_foff_t total_size;
|
|
|
|
hio_foff_t start_offset;
|
|
|
|
hio_foff_t end_offset;
|
|
|
|
hio_foff_t cur_offset;
|
|
|
|
hio_bch_t peer_buf[8192];
|
|
|
|
hio_tmridx_t peer_tmridx;
|
|
|
|
hio_bch_t peer_etag[128];
|
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
unsigned int over: 4; /* must be large enough to accomodate FILE_OVER_ALL */
|
2020-07-08 05:24:40 +00:00
|
|
|
unsigned int client_htrd_recbs_changed: 1;
|
2020-07-20 14:08:25 +00:00
|
|
|
unsigned int etag_match: 1;
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dev_sck_on_read_t client_org_on_read;
|
|
|
|
hio_dev_sck_on_write_t client_org_on_write;
|
|
|
|
hio_dev_sck_on_disconnect_t client_org_on_disconnect;
|
|
|
|
hio_htrd_recbs_t client_htrd_org_recbs;
|
2020-07-08 05:24:40 +00:00
|
|
|
};
|
2020-11-16 16:52:49 +00:00
|
|
|
typedef struct file_t file_t;
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-03-25 18:01:40 +00:00
|
|
|
static void unbind_task_from_client (file_t* file, int rcdown);
|
2023-03-26 14:46:40 +00:00
|
|
|
static void unbind_task_from_peer (file_t* file, int rcdown);
|
2020-11-16 16:52:49 +00:00
|
|
|
static int file_send_contents_to_client (file_t* file);
|
2020-07-13 10:01:42 +00:00
|
|
|
|
2023-03-25 18:01:40 +00:00
|
|
|
static HIO_INLINE void set_tcp_cork (hio_dev_sck_t* sck, int tcp_cork)
|
|
|
|
{
|
|
|
|
#if defined(TCP_CORK)
|
|
|
|
#if defined(SOL_TCP)
|
|
|
|
hio_dev_sck_setsockopt (sck, SOL_TCP, TCP_CORK, &tcp_cork, HIO_SIZEOF(tcp_cork));
|
|
|
|
#elif defined(IPPROTO_TCP)
|
|
|
|
hio_dev_sck_setsockopt (sck, IPPROTO_TCP, TCP_CORK, &tcp_cork, HIO_SIZEOF(tcp_cork));
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
static void file_halt_participating_devices (file_t* file)
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2023-11-16 15:38:02 +00:00
|
|
|
hio_dev_sck_t* csck = file->task_csck;
|
|
|
|
|
|
|
|
HIO_DEBUG3 (file->htts->hio, "HTTS(%p) - file(c=%d,p=%d) Halting participating devices\n", file->htts, (int)(csck? csck->hnd: -1), (int)file->peer);
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-11-16 15:38:02 +00:00
|
|
|
if (csck) hio_dev_sck_halt (csck);
|
2023-03-26 14:46:40 +00:00
|
|
|
unbind_task_from_peer (file, 1);
|
2020-07-08 05:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
static void file_mark_over (file_t* file, int over_bits)
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2023-03-25 18:01:40 +00:00
|
|
|
hio_svc_htts_t* htts = file->htts;
|
|
|
|
hio_t* hio = htts->hio;
|
2020-07-08 05:24:40 +00:00
|
|
|
unsigned int old_over;
|
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
old_over = file->over;
|
|
|
|
file->over |= over_bits;
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-03-25 18:01:40 +00:00
|
|
|
HIO_DEBUG6 (hio, "HTTS(%p) - file(c=%d,p=%d) updating mark - old_over=%x | new-bits=%x => over=%x\n", htts, (int)file->task_csck->hnd, file->peer, (int)old_over, (int)over_bits, (int)file->over);
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
if (!(old_over & FILE_OVER_READ_FROM_CLIENT) && (file->over & FILE_OVER_READ_FROM_CLIENT))
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2023-03-05 18:27:42 +00:00
|
|
|
if (file->task_csck && hio_dev_sck_read(file->task_csck, 0) <= -1)
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2023-03-25 18:01:40 +00:00
|
|
|
HIO_DEBUG3 (hio, "HTTS(%p) - file(c=%d,p=%d) halting client for failure to disable input watching\n", htts, (int)file->task_csck->hnd, file->peer);
|
2023-03-05 18:27:42 +00:00
|
|
|
hio_dev_sck_halt (file->task_csck);
|
2020-07-08 05:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 10:30:12 +00:00
|
|
|
#if 0
|
2020-11-16 16:52:49 +00:00
|
|
|
if (!(old_over & FILE_OVER_READ_FROM_PEER) && (file->over & FILE_OVER_READ_FROM_PEER))
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2020-07-15 10:30:12 +00:00
|
|
|
/* there is no partial close... keep it open */
|
2020-07-08 05:24:40 +00:00
|
|
|
}
|
2020-07-15 10:30:12 +00:00
|
|
|
#endif
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
if (old_over != FILE_OVER_ALL && file->over == FILE_OVER_ALL)
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
|
|
|
/* ready to stop */
|
2023-03-25 18:01:40 +00:00
|
|
|
HIO_DEBUG3 (hio, "HTTS(%p) - file(c=%d,p=%d) halting peer as it is unneeded\n", htts, (int)file->task_csck->hnd, file->peer);
|
2023-03-26 14:46:40 +00:00
|
|
|
unbind_task_from_peer (file, 1);
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-03-25 18:01:40 +00:00
|
|
|
if (file->task_csck)
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2023-03-25 18:01:40 +00:00
|
|
|
if (file->task_keep_client_alive)
|
2023-01-11 17:02:09 +00:00
|
|
|
{
|
2023-11-19 17:30:38 +00:00
|
|
|
/* TODO: restore to the original value... */
|
2023-03-25 18:01:40 +00:00
|
|
|
set_tcp_cork (file->task_csck, 0);
|
2020-07-20 14:08:25 +00:00
|
|
|
|
2023-01-11 17:02:09 +00:00
|
|
|
/* the file task must not be accessed from here down as it could have been destroyed */
|
2023-03-25 18:01:40 +00:00
|
|
|
HIO_DEBUG2 (hio, "HTTS(%p) - keeping client(%p) alive\n", htts, file->task_csck);
|
|
|
|
HIO_ASSERT (hio, file->task_client->task == (hio_svc_htts_task_t*)file);
|
|
|
|
unbind_task_from_client (file, 1);
|
2023-01-11 17:02:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-25 18:01:40 +00:00
|
|
|
HIO_DEBUG2 (hio, "HTTS(%p) - halting client(%p)\n", htts, file->task_csck);
|
2023-03-05 18:27:42 +00:00
|
|
|
hio_dev_sck_shutdown (file->task_csck, HIO_DEV_SCK_SHUTDOWN_WRITE);
|
|
|
|
hio_dev_sck_halt (file->task_csck);
|
2023-03-04 15:55:15 +00:00
|
|
|
/* the file task will be detached from file->task_client->task by the upstream disconnect handler in http_svr.c */
|
2023-01-11 17:02:09 +00:00
|
|
|
}
|
2020-07-08 05:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int file_write_to_peer (file_t* file, const void* data, hio_iolen_t dlen)
|
2020-07-15 10:30:12 +00:00
|
|
|
{
|
2023-01-11 14:59:41 +00:00
|
|
|
/* hio_t* hio = file->htts->hio; */
|
2020-07-15 10:30:12 +00:00
|
|
|
|
|
|
|
if (dlen <= 0)
|
|
|
|
{
|
2020-11-16 16:52:49 +00:00
|
|
|
file_mark_over (file, FILE_OVER_WRITE_TO_PEER);
|
2020-07-15 10:30:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-09 16:41:07 +00:00
|
|
|
hio_iolen_t pos, rem, n;
|
2023-03-04 11:26:38 +00:00
|
|
|
if (file->task_req_method == HIO_HTTP_GET) return 0;
|
2022-10-09 16:41:07 +00:00
|
|
|
if (file->peer <= -1) return 0; /* peer open proabably failed */
|
2020-07-15 10:30:12 +00:00
|
|
|
|
2022-10-09 16:41:07 +00:00
|
|
|
/* TODO: async file io -> create a file device?? */
|
|
|
|
pos = 0;
|
|
|
|
rem = dlen;
|
|
|
|
while (rem > 0)
|
|
|
|
{
|
|
|
|
n = write(file->peer, &((const hio_uint8_t*)data)[pos], rem);
|
|
|
|
if (n <= -1) return -1;
|
|
|
|
rem -= n;
|
|
|
|
pos += n;
|
|
|
|
}
|
2020-07-15 10:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-01-11 15:41:01 +00:00
|
|
|
static void file_on_kill (hio_svc_htts_task_t* task)
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2023-01-11 15:41:01 +00:00
|
|
|
file_t* file = (file_t*)task;
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_t* hio = file->htts->hio;
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-03-05 18:27:42 +00:00
|
|
|
HIO_DEBUG5 (hio, "HTTS(%p) - file(t=%p,c=%p[%d],p=%d) - killing the task\n", file->htts, file, file->task_client, (file->task_csck? file->task_csck->hnd: -1), file->peer);
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-03-04 15:55:15 +00:00
|
|
|
if (file->on_kill) file->on_kill (task);
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-03-26 14:46:40 +00:00
|
|
|
/* this callback function doesn't decrement the reference count on file because
|
|
|
|
* it is the task destruction callback. (passing 0 to unbind_task_from_peer/client) */
|
|
|
|
unbind_task_from_peer (file, 0);
|
2022-12-26 17:03:53 +00:00
|
|
|
|
2023-03-05 18:27:42 +00:00
|
|
|
if (file->task_csck)
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2023-03-25 18:01:40 +00:00
|
|
|
HIO_ASSERT (hio, file->task_client != HIO_NULL);
|
|
|
|
unbind_task_from_client (file, 0);
|
2020-07-08 05:24:40 +00:00
|
|
|
}
|
2023-01-11 17:02:09 +00:00
|
|
|
|
|
|
|
if (file->task_next) HIO_SVC_HTTS_TASKL_UNLINK_TASK (file); /* detach from the htts service only if it's attached */
|
2023-03-25 18:01:40 +00:00
|
|
|
|
2023-03-05 18:27:42 +00:00
|
|
|
HIO_DEBUG5 (hio, "HTTS(%p) - file(t=%p,c=%p[%d],p=%d) - killed the task\n", file->htts, file, file->task_client, (file->task_csck? file->task_csck->hnd: -1), file->peer);
|
2020-07-08 05:24:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void file_client_on_disconnect (hio_dev_sck_t* sck)
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2022-10-08 07:47:55 +00:00
|
|
|
hio_t* hio = sck->hio;
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck);
|
2023-01-11 15:41:01 +00:00
|
|
|
file_t* file = (file_t*)cli->task;
|
2023-01-11 17:02:09 +00:00
|
|
|
hio_svc_htts_t* htts = file->htts;
|
2022-10-08 07:47:55 +00:00
|
|
|
|
|
|
|
HIO_ASSERT (hio, sck == cli->sck);
|
2023-03-05 18:27:42 +00:00
|
|
|
HIO_ASSERT (hio, sck == file->task_csck);
|
2022-10-08 07:47:55 +00:00
|
|
|
|
2023-01-11 17:02:09 +00:00
|
|
|
HIO_DEBUG4 (hio, "HTTS(%p) - file(t=%p,c=%p,csck=%p) - client socket disconnect notified\n", htts, file, sck, cli);
|
2022-10-08 07:47:55 +00:00
|
|
|
|
2023-03-25 18:01:40 +00:00
|
|
|
if (file)
|
2023-01-11 17:02:09 +00:00
|
|
|
{
|
2023-11-16 15:38:02 +00:00
|
|
|
HIO_SVC_HTTS_TASK_RCUP ((hio_svc_htts_task_t*)file);
|
2023-03-25 18:01:40 +00:00
|
|
|
|
|
|
|
/* detach the task from the client and the client socket */
|
|
|
|
unbind_task_from_client (file, 1);
|
|
|
|
|
2023-03-26 14:46:40 +00:00
|
|
|
/* the current file peer implemenation is not async. so there is no IO event associated
|
2023-11-19 17:30:38 +00:00
|
|
|
* when the client side is disconnected, simple close the peer side as it's not needed.
|
2023-03-26 14:46:40 +00:00
|
|
|
* this behavior is different from http-fcgi or http-cgi */
|
|
|
|
unbind_task_from_peer (file, 1);
|
|
|
|
|
2023-03-25 18:01:40 +00:00
|
|
|
/* call the parent handler*/
|
|
|
|
/*if (file->client_org_on_disconnect) file->client_org_on_disconnect (sck);*/
|
|
|
|
if (sck->on_disconnect) sck->on_disconnect (sck); /* restored to the orginal parent handelr in unbind_task_from_client() */
|
|
|
|
|
2023-11-16 15:38:02 +00:00
|
|
|
HIO_SVC_HTTS_TASK_RCDOWN ((hio_svc_htts_task_t*)file);
|
2023-01-11 17:02:09 +00:00
|
|
|
}
|
2022-10-08 07:47:55 +00:00
|
|
|
|
2023-01-11 17:02:09 +00:00
|
|
|
HIO_DEBUG4 (hio, "HTTS(%p) - file(t=%p,c=%p,csck=%p) - client socket disconnect handled\n", htts, file, sck, cli);
|
|
|
|
/* Note: after this callback, the actual device pointed to by 'sck' will be freed in the main loop. */
|
2020-07-08 05:24:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int file_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t len, const hio_skad_t* srcaddr)
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_t* hio = sck->hio;
|
|
|
|
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck);
|
2023-01-11 15:41:01 +00:00
|
|
|
file_t* file = (file_t*)cli->task;
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_ASSERT (hio, sck == cli->sck);
|
2023-03-05 18:27:42 +00:00
|
|
|
HIO_ASSERT (hio, sck == file->task_csck);
|
2020-07-08 05:24:40 +00:00
|
|
|
|
|
|
|
if (len <= -1)
|
|
|
|
{
|
|
|
|
/* read error */
|
2023-03-18 20:04:32 +00:00
|
|
|
HIO_DEBUG3 (cli->htts->hio, "HTTS(%p) - file(c=%d,p=%d) read error on client\n", file->htts, (int)sck->hnd, file->peer);
|
2020-07-08 05:24:40 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2022-10-09 16:41:07 +00:00
|
|
|
if (file->peer <= -1)
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2022-10-09 16:41:07 +00:00
|
|
|
/* the peer is gone or not even opened */
|
2023-03-18 20:04:32 +00:00
|
|
|
HIO_DEBUG3 (cli->htts->hio, "HTTS(%p) - file(c=%d,p=%d) read on client, no peer to write\n", file->htts, (int)sck->hnd, file->peer);
|
2020-07-08 05:24:40 +00:00
|
|
|
goto oops; /* do what? just return 0? */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
/* EOF on the client side. arrange to close */
|
2023-03-18 20:04:32 +00:00
|
|
|
HIO_DEBUG3 (cli->htts->hio, "HTTS(%p) - file(c=%d,p=%d) EOF detected on client\n", file->htts, (int)sck->hnd, file->peer);
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
if (!(file->over & FILE_OVER_READ_FROM_CLIENT)) /* if this is true, EOF is received without file_client_htrd_poke() */
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2022-10-09 16:41:07 +00:00
|
|
|
int n;
|
|
|
|
n = file_write_to_peer(file, HIO_NULL, 0);
|
2020-11-16 16:52:49 +00:00
|
|
|
file_mark_over (file, FILE_OVER_READ_FROM_CLIENT);
|
2022-10-09 16:41:07 +00:00
|
|
|
if (n <= -1) goto oops;
|
2020-07-08 05:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t rem;
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_ASSERT (hio, !(file->over & FILE_OVER_READ_FROM_CLIENT));
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (hio_htrd_feed(cli->htrd, buf, len, &rem) <= -1) goto oops;
|
2020-07-08 05:24:40 +00:00
|
|
|
|
|
|
|
if (rem > 0)
|
|
|
|
{
|
|
|
|
/* TODO store this to client buffer. once the current resource is completed, arrange to call on_read() with it */
|
2023-03-18 20:04:32 +00:00
|
|
|
HIO_DEBUG3 (cli->htts->hio, "HTTS(%p) - file(c=%d,p=%d) excessive data after contents on client\n", file->htts, (int)sck->hnd, file->peer);
|
2020-07-08 05:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
oops:
|
2020-11-16 16:52:49 +00:00
|
|
|
file_halt_participating_devices (file);
|
2020-07-08 05:24:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int file_client_on_write (hio_dev_sck_t* sck, hio_iolen_t wrlen, void* wrctx, const hio_skad_t* dstaddr)
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_t* hio = sck->hio;
|
|
|
|
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck);
|
2023-01-11 15:41:01 +00:00
|
|
|
file_t* file = (file_t*)cli->task;
|
2023-03-25 18:01:40 +00:00
|
|
|
int n;
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-03-25 18:01:40 +00:00
|
|
|
n = file->client_org_on_write? file->client_org_on_write(sck, wrlen, wrctx, dstaddr): 0;
|
2020-07-08 05:24:40 +00:00
|
|
|
|
|
|
|
if (wrlen == 0)
|
|
|
|
{
|
2020-11-16 16:52:49 +00:00
|
|
|
file_mark_over (file, FILE_OVER_WRITE_TO_CLIENT);
|
2020-07-08 05:24:40 +00:00
|
|
|
}
|
2023-03-25 18:01:40 +00:00
|
|
|
else if (wrlen > 0)
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2023-03-04 11:26:38 +00:00
|
|
|
if (file->task_req_method == HIO_HTTP_GET)
|
2023-11-18 17:30:44 +00:00
|
|
|
{
|
2023-11-19 17:30:38 +00:00
|
|
|
if (file_send_contents_to_client (file) <= -1) n = -1;
|
2023-11-18 17:30:44 +00:00
|
|
|
}
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-03-25 18:01:40 +00:00
|
|
|
if ((file->over & FILE_OVER_READ_FROM_PEER) && file->task_res_pending_writes <= 0)
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2020-11-16 16:52:49 +00:00
|
|
|
file_mark_over (file, FILE_OVER_WRITE_TO_CLIENT);
|
2020-07-08 05:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-25 18:01:40 +00:00
|
|
|
if (n <= -1 || wrlen <= -1) file_halt_participating_devices (file);
|
2020-07-08 05:24:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-15 10:30:12 +00:00
|
|
|
/* --------------------------------------------------------------------- */
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int file_client_htrd_poke (hio_htrd_t* htrd, hio_htre_t* req)
|
2020-07-15 10:30:12 +00:00
|
|
|
{
|
|
|
|
/* client request got completed */
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_svc_htts_cli_htrd_xtn_t* htrdxtn = (hio_svc_htts_cli_htrd_xtn_t*)hio_htrd_getxtn(htrd);
|
|
|
|
hio_dev_sck_t* sck = htrdxtn->sck;
|
|
|
|
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck);
|
2023-01-11 15:41:01 +00:00
|
|
|
file_t* file = (file_t*)cli->task;
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2020-07-15 10:30:12 +00:00
|
|
|
/* indicate EOF to the client peer */
|
2021-07-22 07:30:20 +00:00
|
|
|
if (file_write_to_peer(file, HIO_NULL, 0) <= -1) return -1;
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-03-04 11:26:38 +00:00
|
|
|
if (file->task_req_method != HIO_HTTP_GET)
|
2020-07-15 10:30:12 +00:00
|
|
|
{
|
2023-11-16 15:38:02 +00:00
|
|
|
if (hio_svc_htts_task_sendfinalres((hio_svc_htts_task_t*)file, HIO_HTTP_STATUS_OK, HIO_NULL, HIO_NULL, 0) <= -1) return -1;
|
2020-07-08 05:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
file_mark_over (file, FILE_OVER_READ_FROM_CLIENT);
|
2020-07-08 05:24:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-07-15 10:30:12 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int file_client_htrd_push_content (hio_htrd_t* htrd, hio_htre_t* req, const hio_bch_t* data, hio_oow_t dlen)
|
2020-07-15 10:30:12 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_svc_htts_cli_htrd_xtn_t* htrdxtn = (hio_svc_htts_cli_htrd_xtn_t*)hio_htrd_getxtn(htrd);
|
|
|
|
hio_dev_sck_t* sck = htrdxtn->sck;
|
|
|
|
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck);
|
2023-01-11 15:41:01 +00:00
|
|
|
file_t* file = (file_t*)cli->task;
|
2020-07-15 10:30:12 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_ASSERT (sck->hio, cli->sck == sck);
|
2020-11-16 16:52:49 +00:00
|
|
|
return file_write_to_peer(file, data, dlen);
|
2020-07-15 10:30:12 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static hio_htrd_recbs_t file_client_htrd_recbs =
|
2020-07-15 10:30:12 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_NULL,
|
2020-07-15 10:30:12 +00:00
|
|
|
file_client_htrd_poke,
|
|
|
|
file_client_htrd_push_content
|
|
|
|
};
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int file_send_header_to_client (file_t* file, int status_code, int force_close, const hio_bch_t* mime_type)
|
2020-07-09 09:54:13 +00:00
|
|
|
{
|
2023-03-10 17:32:33 +00:00
|
|
|
hio_svc_htts_cli_t* cli = file->task_client;
|
|
|
|
hio_foff_t content_length;
|
|
|
|
|
|
|
|
if (HIO_UNLIKELY(!cli))
|
|
|
|
{
|
|
|
|
/* client disconnected or not connectd */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
content_length = file->end_offset - file->start_offset + 1;
|
|
|
|
if (status_code == HIO_HTTP_STATUS_OK && file->total_size != content_length) status_code = HIO_HTTP_STATUS_PARTIAL_CONTENT;
|
|
|
|
|
2023-11-16 15:38:02 +00:00
|
|
|
if (hio_svc_htts_task_startreshdr((hio_svc_htts_task_t*)file, status_code, HIO_NULL, 0) <= -1) return -1;
|
2023-03-10 17:32:33 +00:00
|
|
|
|
2023-11-16 15:38:02 +00:00
|
|
|
if (mime_type && mime_type[0] != '\0' && hio_svc_htts_task_addreshdr((hio_svc_htts_task_t*)file, "Content-Type", mime_type) <= -1) return -1;
|
2023-03-10 17:32:33 +00:00
|
|
|
|
|
|
|
if ((file->task_req_method == HIO_HTTP_GET || file->task_req_method == HIO_HTTP_HEAD) &&
|
2023-11-16 15:38:02 +00:00
|
|
|
hio_svc_htts_task_addreshdr((hio_svc_htts_task_t*)file, "ETag", file->peer_etag) <= -1) return -1;
|
2023-03-10 17:32:33 +00:00
|
|
|
|
|
|
|
if (status_code == HIO_HTTP_STATUS_PARTIAL_CONTENT &&
|
2023-11-16 15:38:02 +00:00
|
|
|
hio_svc_htts_task_addreshdrfmt((hio_svc_htts_task_t*)file, "Content-Ranges", "bytes %ju-%ju/%ju", (hio_uintmax_t)file->start_offset, (hio_uintmax_t)file->end_offset, (hio_uintmax_t)file->total_size) <= -1) return -1;
|
2023-03-10 17:32:33 +00:00
|
|
|
|
|
|
|
/* ----- */
|
|
|
|
// TODO: Allow-Contents
|
|
|
|
// Allow-Headers... support custom headers...
|
2023-11-16 15:38:02 +00:00
|
|
|
if (hio_svc_htts_task_addreshdr((hio_svc_htts_task_t*)file, "Access-Control-Allow-Origin", "*") <= -1) return -1;
|
2023-03-10 17:32:33 +00:00
|
|
|
/* ----- */
|
|
|
|
|
2023-11-16 15:38:02 +00:00
|
|
|
if (hio_svc_htts_task_addreshdrfmt((hio_svc_htts_task_t*)file, "Content-Length", "%ju", (hio_uintmax_t)content_length) <= -1) return -1;
|
2023-03-10 17:32:33 +00:00
|
|
|
|
2023-11-16 15:38:02 +00:00
|
|
|
if (hio_svc_htts_task_endreshdr((hio_svc_htts_task_t*)file) <= -1) return -1;
|
2023-03-10 17:32:33 +00:00
|
|
|
|
2023-03-18 20:04:32 +00:00
|
|
|
return 0;
|
2020-07-09 09:54:13 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void send_contents_to_client_later (hio_t* hio, const hio_ntime_t* now, hio_tmrjob_t* tmrjob)
|
2020-07-15 10:30:12 +00:00
|
|
|
{
|
2020-11-16 16:52:49 +00:00
|
|
|
file_t* file = (file_t*)tmrjob->ctx;
|
2023-03-25 18:01:40 +00:00
|
|
|
if (file_send_contents_to_client(file) <= -1) file_halt_participating_devices (file);
|
2020-07-15 10:30:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
static int file_send_contents_to_client (file_t* file)
|
2020-07-09 09:54:13 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_t* hio = file->htts->hio;
|
|
|
|
hio_foff_t lim;
|
2020-07-09 09:54:13 +00:00
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
if (file->cur_offset > file->end_offset)
|
2020-07-09 09:54:13 +00:00
|
|
|
{
|
2020-07-13 10:01:42 +00:00
|
|
|
/* reached the end */
|
2020-11-16 16:52:49 +00:00
|
|
|
file_mark_over (file, FILE_OVER_READ_FROM_PEER);
|
2020-07-13 10:01:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
lim = file->end_offset - file->cur_offset + 1;
|
|
|
|
if (file->sendfile_ok)
|
2020-07-13 10:01:42 +00:00
|
|
|
{
|
2020-07-19 16:19:18 +00:00
|
|
|
if (lim > 0x7FFF0000) lim = 0x7FFF0000; /* TODO: change this... */
|
2023-11-16 15:38:02 +00:00
|
|
|
if (hio_svc_htts_task_addresbodyfromfile((hio_svc_htts_task_t*)file, file->peer, file->cur_offset, lim) <= -1) return -1;
|
2020-11-16 16:52:49 +00:00
|
|
|
file->cur_offset += lim;
|
2020-07-09 09:54:13 +00:00
|
|
|
}
|
2020-07-19 06:23:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ssize_t n;
|
2020-07-09 09:54:13 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
n = read(file->peer, file->peer_buf, (lim < HIO_SIZEOF(file->peer_buf)? lim: HIO_SIZEOF(file->peer_buf)));
|
2020-07-19 06:23:43 +00:00
|
|
|
if (n == -1)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
if ((errno == EAGAIN || errno == EINTR) && file->peer_tmridx == HIO_TMRIDX_INVALID)
|
2020-07-19 06:23:43 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_tmrjob_t tmrjob;
|
2020-07-19 06:23:43 +00:00
|
|
|
/* use a timer job for a new sending attempt */
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_MEMSET (&tmrjob, 0, HIO_SIZEOF(tmrjob));
|
2020-11-16 16:52:49 +00:00
|
|
|
tmrjob.ctx = file;
|
2020-07-19 06:23:43 +00:00
|
|
|
/*tmrjob.when = leave it at 0 for immediate firing.*/
|
|
|
|
tmrjob.handler = send_contents_to_client_later;
|
2020-11-16 16:52:49 +00:00
|
|
|
tmrjob.idxptr = &file->peer_tmridx;
|
2021-07-22 07:30:20 +00:00
|
|
|
return hio_instmrjob(hio, &tmrjob) == HIO_TMRIDX_INVALID? -1: 0;
|
2020-07-19 06:23:43 +00:00
|
|
|
}
|
2020-07-15 10:30:12 +00:00
|
|
|
|
2020-07-19 06:23:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (n == 0)
|
|
|
|
{
|
2022-12-21 14:57:51 +00:00
|
|
|
/* no more data to read - this must not happen unless file size changed while the file is open. */
|
2023-11-18 17:30:44 +00:00
|
|
|
/* TODO: I probably must close the connection by force??? */
|
2023-01-11 14:59:41 +00:00
|
|
|
file_mark_over (file, FILE_OVER_READ_FROM_PEER);
|
2020-07-19 06:23:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2023-11-16 15:38:02 +00:00
|
|
|
/*if (file_write_to_client(file, file->peer_buf, n) <= -1) return -1;*/
|
|
|
|
if (hio_svc_htts_task_addresbody((hio_svc_htts_task_t*)file, file->peer_buf, n) <= -1) return -1;
|
2020-07-15 10:30:12 +00:00
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
file->cur_offset += n;
|
2020-07-19 06:23:43 +00:00
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
/* if (file->cur_offset > file->end_offset) should i check this or wait until this function is invoked?
|
|
|
|
file_mark_over (file, FILE_OVER_READ_FROM_PEER);*/
|
2020-07-19 06:23:43 +00:00
|
|
|
}
|
2020-07-15 10:30:12 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-09 16:41:07 +00:00
|
|
|
#define ERRNO_TO_STATUS_CODE(x) ( \
|
2022-12-26 17:03:53 +00:00
|
|
|
((x) == ENOENT || (x) == ENOTDIR)? HIO_HTTP_STATUS_NOT_FOUND: \
|
2022-10-09 16:41:07 +00:00
|
|
|
((x) == EPERM || (x) == EACCES)? HIO_HTTP_STATUS_FORBIDDEN: HIO_HTTP_STATUS_INTERNAL_SERVER_ERROR \
|
|
|
|
)
|
|
|
|
|
|
|
|
static HIO_INLINE int process_range_header (file_t* file, hio_htre_t* req, int* error_status)
|
2020-07-15 10:30:12 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_htre_hdrval_t* tmp;
|
|
|
|
hio_oow_t etag_len;
|
2020-07-15 10:30:12 +00:00
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
if (fstat(file->peer, &st) <= -1)
|
2020-07-13 10:01:42 +00:00
|
|
|
{
|
2022-10-09 16:41:07 +00:00
|
|
|
*error_status = ERRNO_TO_STATUS_CODE(errno);
|
2020-07-13 10:01:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2020-07-20 14:08:25 +00:00
|
|
|
|
2020-08-10 17:03:54 +00:00
|
|
|
if ((st.st_mode & S_IFMT) != S_IFREG)
|
2020-08-02 16:07:42 +00:00
|
|
|
{
|
|
|
|
/* TODO: support directory listing if S_IFDIR? still disallow special files. */
|
2022-10-09 16:41:07 +00:00
|
|
|
*error_status = HIO_HTTP_STATUS_FORBIDDEN;
|
2020-08-02 16:07:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-12-26 17:03:53 +00:00
|
|
|
#if defined(HAVE_STRUCT_STAT_MTIM)
|
|
|
|
etag_len = hio_fmt_uintmax_to_bcstr(&file->peer_etag[0], HIO_COUNTOF(file->peer_etag), st.st_mtim.tv_sec, 16, -1, '\0', HIO_NULL);
|
|
|
|
file->peer_etag[etag_len++] = '-';
|
|
|
|
#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
|
|
|
|
etag_len += hio_fmt_uintmax_to_bcstr(&file->peer_etag[etag_len], HIO_COUNTOF(file->peer_etag), st.st_mtim.tv_nsec, 16, -1, '\0', HIO_NULL);
|
|
|
|
file->peer_etag[etag_len++] = '-';
|
2022-06-17 05:35:30 +00:00
|
|
|
#endif
|
2022-12-26 17:03:53 +00:00
|
|
|
#else
|
|
|
|
etag_len = hio_fmt_uintmax_to_bcstr(&file->peer_etag[0], HIO_COUNTOF(file->peer_etag), st.st_mtime, 16, -1, '\0', HIO_NULL);
|
|
|
|
file->peer_etag[etag_len++] = '-';
|
|
|
|
#endif
|
|
|
|
etag_len += hio_fmt_uintmax_to_bcstr(&file->peer_etag[etag_len], HIO_COUNTOF(file->peer_etag) - etag_len, st.st_size, 16, -1, '\0', HIO_NULL);
|
|
|
|
file->peer_etag[etag_len++] = '-';
|
|
|
|
etag_len += hio_fmt_uintmax_to_bcstr(&file->peer_etag[etag_len], HIO_COUNTOF(file->peer_etag) - etag_len, st.st_ino, 16, -1, '\0', HIO_NULL);
|
|
|
|
file->peer_etag[etag_len++] = '-';
|
|
|
|
hio_fmt_uintmax_to_bcstr (&file->peer_etag[etag_len], HIO_COUNTOF(file->peer_etag) - etag_len, st.st_dev, 16, -1, '\0', HIO_NULL);
|
|
|
|
|
|
|
|
tmp = hio_htre_getheaderval(req, "If-None-Match");
|
|
|
|
if (tmp && hio_comp_bcstr(file->peer_etag, tmp->ptr, 0) == 0) file->etag_match = 1;
|
2020-07-20 14:08:25 +00:00
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
file->end_offset = st.st_size;
|
2020-07-13 10:01:42 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
tmp = hio_htre_getheaderval(req, "Range"); /* TODO: support multiple ranges? */
|
2020-07-15 10:30:12 +00:00
|
|
|
if (tmp)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_http_range_t range;
|
2020-07-15 10:30:12 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (hio_parse_http_range_bcstr(tmp->ptr, &range) <= -1)
|
2020-07-15 10:30:12 +00:00
|
|
|
{
|
|
|
|
range_not_satisifiable:
|
2022-10-09 16:41:07 +00:00
|
|
|
*error_status = HIO_HTTP_STATUS_RANGE_NOT_SATISFIABLE;
|
2020-07-15 10:30:12 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (range.type)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_HTTP_RANGE_PROPER:
|
2020-07-15 10:30:12 +00:00
|
|
|
/* Range XXXX-YYYY */
|
|
|
|
if (range.to >= st.st_size) goto range_not_satisifiable;
|
2020-11-16 16:52:49 +00:00
|
|
|
file->start_offset = range.from;
|
|
|
|
file->end_offset = range.to;
|
2020-07-15 10:30:12 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_HTTP_RANGE_PREFIX:
|
2020-07-15 10:30:12 +00:00
|
|
|
/* Range: XXXX- */
|
|
|
|
if (range.from >= st.st_size) goto range_not_satisifiable;
|
2020-11-16 16:52:49 +00:00
|
|
|
file->start_offset = range.from;
|
|
|
|
file->end_offset = st.st_size - 1;
|
2020-07-15 10:30:12 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_HTTP_RANGE_SUFFIX:
|
2020-07-15 10:30:12 +00:00
|
|
|
/* Range: -XXXX */
|
|
|
|
if (range.to >= st.st_size) goto range_not_satisifiable;
|
2020-11-16 16:52:49 +00:00
|
|
|
file->start_offset = st.st_size - range.to;
|
|
|
|
file->end_offset = st.st_size - 1;
|
2020-07-15 10:30:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-07-09 09:54:13 +00:00
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
if (file->start_offset > 0)
|
2022-10-09 16:41:07 +00:00
|
|
|
{
|
|
|
|
if (lseek(file->peer, file->start_offset, SEEK_SET) <= -1)
|
|
|
|
{
|
|
|
|
*error_status = ERRNO_TO_STATUS_CODE(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 10:30:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-16 16:52:49 +00:00
|
|
|
file->start_offset = 0;
|
|
|
|
file->end_offset = st.st_size - 1;
|
2020-07-15 10:30:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
file->cur_offset = file->start_offset;
|
|
|
|
file->total_size = st.st_size;
|
2020-07-09 09:54:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-31 15:20:07 +00:00
|
|
|
static int open_peer_with_mode (file_t* file, const hio_bch_t* actual_file, int flags, int* error_status, const hio_bch_t** res_mime_type)
|
2020-07-15 10:30:12 +00:00
|
|
|
{
|
2022-12-15 15:24:31 +00:00
|
|
|
struct stat st;
|
|
|
|
|
2022-10-09 16:41:07 +00:00
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
#if defined(O_CLOEXEC)
|
|
|
|
flags |= O_CLOEXEC;
|
2020-07-17 10:14:15 +00:00
|
|
|
#endif
|
2022-10-09 16:41:07 +00:00
|
|
|
#if defined(O_LARGEFILE)
|
|
|
|
flags |= O_LARGEFILE;
|
2020-07-15 10:30:12 +00:00
|
|
|
#endif
|
2022-10-09 16:41:07 +00:00
|
|
|
|
|
|
|
file->peer = open(actual_file, flags, 0644);
|
|
|
|
if (HIO_UNLIKELY(file->peer <= -1))
|
|
|
|
{
|
|
|
|
*error_status = ERRNO_TO_STATUS_CODE(errno);
|
|
|
|
return -1;
|
2020-07-15 10:30:12 +00:00
|
|
|
}
|
|
|
|
|
2022-12-31 15:20:07 +00:00
|
|
|
if (((flags | O_RDONLY) && fstat(file->peer, &st) >= 0 && S_ISDIR(st.st_mode)) && /* only for read operation */
|
|
|
|
(file->cbs && file->cbs->open_dir_list)) /* directory listing is enabled */
|
2022-12-15 15:24:31 +00:00
|
|
|
{
|
|
|
|
int alt_fd;
|
|
|
|
|
2023-03-04 15:55:15 +00:00
|
|
|
if (!file->task_req_qpath_ending_with_slash)
|
2022-12-26 17:03:53 +00:00
|
|
|
{
|
|
|
|
*error_status = HIO_HTTP_STATUS_MOVED_PERMANENTLY;
|
|
|
|
close (file->peer);
|
|
|
|
file->peer = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-03-04 15:55:15 +00:00
|
|
|
alt_fd = file->cbs->open_dir_list(file->htts, file->task_req_qpath, actual_file, res_mime_type, file->cbs->ctx);
|
2022-12-31 15:20:07 +00:00
|
|
|
if (alt_fd >= 0)
|
2022-12-15 15:24:31 +00:00
|
|
|
{
|
2022-12-31 15:20:07 +00:00
|
|
|
close (file->peer);
|
|
|
|
file->peer = alt_fd;
|
2022-12-15 15:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-31 15:20:07 +00:00
|
|
|
else
|
2022-12-16 16:24:10 +00:00
|
|
|
{
|
2023-01-11 14:59:41 +00:00
|
|
|
if (res_mime_type && file->cbs && file->cbs->get_mime_type)
|
2022-12-16 16:24:10 +00:00
|
|
|
{
|
2022-12-31 15:20:07 +00:00
|
|
|
const hio_bch_t* mime_type;
|
2023-03-04 15:55:15 +00:00
|
|
|
mime_type = file->cbs->get_mime_type(file->htts, file->task_req_qpath, actual_file, file->cbs->ctx);
|
2022-12-31 15:20:07 +00:00
|
|
|
if (mime_type) *res_mime_type = mime_type;
|
2022-12-16 16:24:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 16:41:07 +00:00
|
|
|
return 0;
|
2020-07-15 10:30:12 +00:00
|
|
|
}
|
|
|
|
|
2023-03-26 14:46:40 +00:00
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
2023-03-25 18:01:40 +00:00
|
|
|
static void bind_task_to_client (file_t* file, hio_dev_sck_t* csck)
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck);
|
2022-10-08 07:47:55 +00:00
|
|
|
|
2023-03-25 18:01:40 +00:00
|
|
|
HIO_ASSERT (file->htts->hio, cli->sck == csck);
|
|
|
|
HIO_ASSERT (file->htts->hio, cli->task == HIO_NULL);
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-03-25 18:01:40 +00:00
|
|
|
/* file->task_client and file->task_csck are set in hio_svc_htts_task_make() */
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-03-25 18:01:40 +00:00
|
|
|
/* remember the client socket's io event handlers */
|
2020-11-16 16:52:49 +00:00
|
|
|
file->client_org_on_read = csck->on_read;
|
|
|
|
file->client_org_on_write = csck->on_write;
|
|
|
|
file->client_org_on_disconnect = csck->on_disconnect;
|
2023-03-25 18:01:40 +00:00
|
|
|
|
|
|
|
/* set new io events handlers on the client socket */
|
2020-07-08 05:24:40 +00:00
|
|
|
csck->on_read = file_client_on_read;
|
|
|
|
csck->on_write = file_client_on_write;
|
|
|
|
csck->on_disconnect = file_client_on_disconnect;
|
|
|
|
|
2023-03-25 18:01:40 +00:00
|
|
|
cli->task = (hio_svc_htts_task_t*)file;
|
|
|
|
HIO_SVC_HTTS_TASK_RCUP (file);
|
|
|
|
}
|
2022-12-26 17:03:53 +00:00
|
|
|
|
2023-03-25 18:01:40 +00:00
|
|
|
static void unbind_task_from_client (file_t* file, int rcdown)
|
|
|
|
{
|
|
|
|
hio_dev_sck_t* csck = file->task_csck;
|
2023-11-18 07:31:24 +00:00
|
|
|
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck);
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-11-18 07:31:24 +00:00
|
|
|
if (cli) /* only if it's bound */
|
2020-07-08 05:24:40 +00:00
|
|
|
{
|
2023-11-18 07:31:24 +00:00
|
|
|
HIO_ASSERT (file->htts->hio, file->task_client != HIO_NULL);
|
|
|
|
HIO_ASSERT (file->htts->hio, file->task_csck != HIO_NULL);
|
|
|
|
HIO_ASSERT (file->htts->hio, file->task_client->task == (hio_svc_htts_task_t*)file);
|
|
|
|
HIO_ASSERT (file->htts->hio, file->task_client->htrd != HIO_NULL);
|
2023-01-11 14:59:41 +00:00
|
|
|
|
2023-11-18 07:31:24 +00:00
|
|
|
if (file->client_htrd_recbs_changed)
|
|
|
|
{
|
|
|
|
hio_htrd_setrecbs (file->task_client->htrd, &file->client_htrd_org_recbs);
|
|
|
|
file->client_htrd_recbs_changed = 0;
|
|
|
|
}
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-11-18 07:31:24 +00:00
|
|
|
if (file->client_org_on_read)
|
|
|
|
{
|
|
|
|
csck->on_read = file->client_org_on_read;
|
|
|
|
file->client_org_on_read = HIO_NULL;
|
|
|
|
}
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-11-18 07:31:24 +00:00
|
|
|
if (file->client_org_on_write)
|
|
|
|
{
|
|
|
|
csck->on_write = file->client_org_on_write;
|
|
|
|
file->client_org_on_write = HIO_NULL;
|
|
|
|
}
|
2023-03-25 18:01:40 +00:00
|
|
|
|
2023-11-18 07:31:24 +00:00
|
|
|
if (file->client_org_on_disconnect)
|
|
|
|
{
|
|
|
|
csck->on_disconnect = file->client_org_on_disconnect;
|
|
|
|
file->client_org_on_disconnect = HIO_NULL;
|
|
|
|
}
|
2023-03-25 18:01:40 +00:00
|
|
|
|
2023-11-18 07:31:24 +00:00
|
|
|
/* there is some ordering issue in using HIO_SVC_HTTS_TASK_UNREF()
|
|
|
|
* because it can destroy the file itself. so reset file->task_client->task
|
|
|
|
* to null and call RCDOWN() later */
|
|
|
|
file->task_client->task = HIO_NULL;
|
2023-03-25 18:01:40 +00:00
|
|
|
|
2023-11-18 07:31:24 +00:00
|
|
|
/* these two lines are also done in csck_on_disconnect() in http-svr.c because the socket is destroyed.
|
|
|
|
* the same lines here are because the task is unbound while the socket is still alive */
|
|
|
|
file->task_client = HIO_NULL;
|
|
|
|
file->task_csck = HIO_NULL;
|
2020-07-08 05:24:40 +00:00
|
|
|
|
2023-11-18 07:31:24 +00:00
|
|
|
/* enable input watching on the socket being unbound */
|
|
|
|
if (file->task_keep_client_alive && hio_dev_sck_read(csck, 1) <= -1)
|
|
|
|
{
|
|
|
|
HIO_DEBUG2 (file->htts->hio, "HTTS(%p) - halting client(%p) for failure to enable input watching\n", file->htts, csck);
|
|
|
|
hio_dev_sck_halt (csck);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rcdown) HIO_SVC_HTTS_TASK_RCDOWN ((hio_svc_htts_task_t*)file);
|
|
|
|
}
|
2023-03-25 18:01:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-26 14:46:40 +00:00
|
|
|
/* ----------------------------------------------------------------------- */
|
2023-03-25 18:01:40 +00:00
|
|
|
|
2023-03-26 14:46:40 +00:00
|
|
|
static int bind_task_to_peer (file_t* file, hio_htre_t* req, const hio_bch_t* file_path, const hio_bch_t* mime_type)
|
2023-03-25 18:01:40 +00:00
|
|
|
{
|
|
|
|
int status_code;
|
|
|
|
|
2023-03-04 11:26:38 +00:00
|
|
|
switch (file->task_req_method)
|
2020-07-15 10:30:12 +00:00
|
|
|
{
|
2022-10-09 16:41:07 +00:00
|
|
|
case HIO_HTTP_GET:
|
2022-12-26 17:03:53 +00:00
|
|
|
case HIO_HTTP_HEAD:
|
2022-12-16 16:24:10 +00:00
|
|
|
{
|
|
|
|
const hio_bch_t* actual_mime_type = mime_type;
|
|
|
|
|
2023-03-26 14:46:40 +00:00
|
|
|
if (open_peer_with_mode(file, file_path, O_RDONLY, &status_code, (mime_type? HIO_NULL: &actual_mime_type)) <= -1 ||
|
2023-11-16 03:29:54 +00:00
|
|
|
process_range_header(file, req, &status_code) <= -1) goto oops_with_status_code;
|
2022-10-09 16:41:07 +00:00
|
|
|
|
2023-03-04 11:26:38 +00:00
|
|
|
if (HIO_LIKELY(file->task_req_method == HIO_HTTP_GET))
|
2022-12-26 17:03:53 +00:00
|
|
|
{
|
|
|
|
if (file->etag_match)
|
|
|
|
{
|
|
|
|
status_code = HIO_HTTP_STATUS_NOT_MODIFIED;
|
2023-11-16 03:29:54 +00:00
|
|
|
goto oops_with_status_code;
|
2022-12-26 17:03:53 +00:00
|
|
|
}
|
2020-07-20 14:08:25 +00:00
|
|
|
|
2022-12-26 17:03:53 +00:00
|
|
|
/* normal full transfer */
|
|
|
|
#if defined(HAVE_POSIX_FADVISE)
|
|
|
|
posix_fadvise (file->peer, file->start_offset, file->end_offset - file->start_offset + 1, POSIX_FADV_SEQUENTIAL);
|
|
|
|
#endif
|
2023-11-19 17:30:38 +00:00
|
|
|
/* TODO: store the current value and let the program restore to the current value when exiting.. */
|
2023-03-25 18:01:40 +00:00
|
|
|
set_tcp_cork (file->task_csck, 1);
|
2022-10-09 16:41:07 +00:00
|
|
|
|
2022-12-26 17:03:53 +00:00
|
|
|
if (file_send_header_to_client(file, HIO_HTTP_STATUS_OK, 0, actual_mime_type) <= -1) goto oops;
|
|
|
|
if (file_send_contents_to_client(file) <= -1) goto oops;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (file_send_header_to_client(file, HIO_HTTP_STATUS_OK, 0, actual_mime_type) <= -1) goto oops;
|
|
|
|
/* no content must be transmitted for HEAD despite Content-Length in the header. */
|
2023-11-16 03:29:54 +00:00
|
|
|
goto oops_with_status_code_2;
|
2022-12-26 17:03:53 +00:00
|
|
|
}
|
2022-10-09 16:41:07 +00:00
|
|
|
break;
|
2022-12-16 16:24:10 +00:00
|
|
|
}
|
2022-10-09 16:41:07 +00:00
|
|
|
|
|
|
|
case HIO_HTTP_POST:
|
|
|
|
case HIO_HTTP_PUT:
|
|
|
|
if (file->options & HIO_SVC_HTTS_FILE_READ_ONLY)
|
|
|
|
{
|
|
|
|
status_code = HIO_HTTP_STATUS_METHOD_NOT_ALLOWED;
|
2023-11-16 03:29:54 +00:00
|
|
|
goto oops_with_status_code;
|
2022-10-09 16:41:07 +00:00
|
|
|
}
|
|
|
|
|
2023-11-16 03:29:54 +00:00
|
|
|
if (open_peer_with_mode(file, file_path, O_WRONLY | O_TRUNC | O_CREAT, &status_code, HIO_NULL) <= -1) goto oops_with_status_code;
|
2022-10-09 16:41:07 +00:00
|
|
|
|
|
|
|
/* the client input must be written to the peer side */
|
|
|
|
file_mark_over (file, FILE_OVER_READ_FROM_PEER);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HIO_HTTP_DELETE:
|
2023-01-11 14:59:41 +00:00
|
|
|
if (file->options & HIO_SVC_HTTS_FILE_READ_ONLY)
|
2022-10-09 16:41:07 +00:00
|
|
|
{
|
|
|
|
status_code = HIO_HTTP_STATUS_METHOD_NOT_ALLOWED;
|
2023-11-16 03:29:54 +00:00
|
|
|
goto oops_with_status_code;
|
2022-10-09 16:41:07 +00:00
|
|
|
}
|
|
|
|
|
2023-03-26 14:46:40 +00:00
|
|
|
if (unlink(file_path) <= -1)
|
2022-10-09 16:41:07 +00:00
|
|
|
{
|
2023-03-26 14:46:40 +00:00
|
|
|
if (errno != EISDIR || (errno == EISDIR && rmdir(file_path) <= -1))
|
2022-10-09 16:41:07 +00:00
|
|
|
{
|
|
|
|
status_code = ERRNO_TO_STATUS_CODE(errno);
|
2023-11-16 03:29:54 +00:00
|
|
|
goto oops_with_status_code;
|
2022-10-09 16:41:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status_code = HIO_HTTP_STATUS_OK;
|
2023-11-16 03:29:54 +00:00
|
|
|
goto oops_with_status_code;
|
2022-10-09 16:41:07 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
status_code = HIO_HTTP_STATUS_METHOD_NOT_ALLOWED;
|
2023-11-16 03:29:54 +00:00
|
|
|
goto oops_with_status_code;
|
2020-07-17 10:14:15 +00:00
|
|
|
}
|
2020-07-09 09:54:13 +00:00
|
|
|
|
2023-03-26 14:46:40 +00:00
|
|
|
HIO_SVC_HTTS_TASK_RCUP (file); /* for file->peer opened */
|
|
|
|
return 0;
|
|
|
|
|
2023-11-16 03:29:54 +00:00
|
|
|
|
|
|
|
/* the task can be terminated because the requested job has been
|
|
|
|
* completed or it can't proceed for various reasons */
|
|
|
|
oops_with_status_code:
|
2023-11-16 15:38:02 +00:00
|
|
|
hio_svc_htts_task_sendfinalres((hio_svc_htts_task_t*)file, status_code, HIO_NULL, HIO_NULL, 0);
|
2023-11-16 03:29:54 +00:00
|
|
|
oops_with_status_code_2:
|
|
|
|
file_mark_over (file, FILE_OVER_READ_FROM_PEER | FILE_OVER_WRITE_TO_PEER);
|
2023-03-26 14:46:40 +00:00
|
|
|
oops:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unbind_task_from_peer (file_t* file, int rcdown)
|
|
|
|
{
|
|
|
|
hio_svc_htts_t* htts = file->htts;
|
|
|
|
hio_t* hio = htts->hio;
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
if (file->peer_tmridx != HIO_TMRIDX_INVALID)
|
|
|
|
{
|
|
|
|
hio_deltmrjob (hio, file->peer_tmridx);
|
|
|
|
HIO_ASSERT (hio, file->peer_tmridx == HIO_TMRIDX_INVALID);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file->peer >= 0)
|
|
|
|
{
|
|
|
|
close (file->peer);
|
|
|
|
file->peer = -1;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
2023-11-16 03:29:54 +00:00
|
|
|
if (rcdown)
|
2023-03-26 14:46:40 +00:00
|
|
|
{
|
|
|
|
while (n > 0)
|
|
|
|
{
|
|
|
|
n--;
|
|
|
|
HIO_SVC_HTTS_TASK_RCDOWN((hio_svc_htts_task_t*)file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int setup_for_content_length(file_t* file, hio_htre_t* req)
|
|
|
|
{
|
2023-03-26 16:42:39 +00:00
|
|
|
int have_content;
|
|
|
|
|
2023-03-26 14:46:40 +00:00
|
|
|
#if defined(FILE_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH)
|
2023-03-26 16:42:39 +00:00
|
|
|
have_content = file->task_req_conlen > 0 || file->task_req_conlen_unlimited;
|
|
|
|
#else
|
|
|
|
have_content = file->task_req_conlen > 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (have_content)
|
2023-03-26 14:46:40 +00:00
|
|
|
{
|
|
|
|
/* change the callbacks to subscribe to contents to be uploaded */
|
|
|
|
file->client_htrd_org_recbs = *hio_htrd_getrecbs(file->task_client->htrd);
|
|
|
|
file_client_htrd_recbs.peek = file->client_htrd_org_recbs.peek;
|
|
|
|
hio_htrd_setrecbs (file->task_client->htrd, &file_client_htrd_recbs);
|
|
|
|
file->client_htrd_recbs_changed = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-26 16:42:39 +00:00
|
|
|
/* no content to be uploaded from the client */
|
|
|
|
#if 0
|
|
|
|
/* indicate EOF to the peer and disable input wathching from the client */
|
|
|
|
if (file_write_to_peer(file, HIO_NULL, 0) <= -1) goto oops;
|
|
|
|
HIO_ASSERT (hio, file->over | FILE_OVER_WRITE_TO_PEER); /* must be set by the call to file_write_to_peer() above */
|
|
|
|
file_mark_over (file, FILE_OVER_READ_FROM_CLIENT);
|
|
|
|
#else
|
|
|
|
/* no peer is open yet. so simply set the mars forcibly instead of calling file_write_to_peer() with null data */
|
|
|
|
file_mark_over (file, FILE_OVER_READ_FROM_CLIENT | FILE_OVER_WRITE_TO_PEER);
|
|
|
|
#endif
|
2023-03-26 14:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req, const hio_bch_t* docroot, const hio_bch_t* filepath, const hio_bch_t* mime_type, int options, hio_svc_htts_task_on_kill_t on_kill, hio_svc_htts_file_cbs_t* cbs)
|
|
|
|
{
|
|
|
|
hio_t* hio = htts->hio;
|
|
|
|
hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck);
|
|
|
|
file_t* file = HIO_NULL;
|
|
|
|
hio_bch_t* actual_file = HIO_NULL;
|
2023-11-18 07:31:24 +00:00
|
|
|
int status_code = HIO_HTTP_STATUS_INTERNAL_SERVER_ERROR;
|
2023-11-16 10:20:50 +00:00
|
|
|
int bound_to_client = 0, bound_to_peer = 0;
|
2023-03-26 14:46:40 +00:00
|
|
|
|
|
|
|
/* ensure that you call this function before any contents is received */
|
|
|
|
HIO_ASSERT (hio, hio_htre_getcontentlen(req) == 0);
|
|
|
|
HIO_ASSERT (hio, cli->sck == csck);
|
|
|
|
|
|
|
|
HIO_DEBUG5 (hio, "HTTS(%p) - file(c=%d) - [%hs] %hs%hs\n", htts, (int)csck->hnd, cli->cli_addr_bcstr, (docroot[0] == '/' && docroot[1] == '\0' && filepath[0] == '/'? "": docroot), filepath);
|
|
|
|
|
2023-11-18 07:31:24 +00:00
|
|
|
if (cli->task)
|
|
|
|
{
|
|
|
|
hio_seterrbfmt (hio, HIO_EPERM, "duplicate task request prohibited");
|
|
|
|
goto oops;
|
|
|
|
}
|
2023-03-26 14:46:40 +00:00
|
|
|
|
|
|
|
file = (file_t*)hio_svc_htts_task_make(htts, HIO_SIZEOF(*file), file_on_kill, req, csck);
|
|
|
|
if (HIO_UNLIKELY(!file)) goto oops;
|
2023-11-16 10:20:50 +00:00
|
|
|
HIO_SVC_HTTS_TASK_RCUP ((hio_svc_htts_task_t*)file); /* for temporary protection */
|
2023-03-26 14:46:40 +00:00
|
|
|
|
2023-11-18 07:31:24 +00:00
|
|
|
actual_file = hio_svc_htts_dupmergepaths(htts, docroot, filepath);
|
|
|
|
if (HIO_UNLIKELY(!actual_file)) goto oops;
|
|
|
|
|
2023-03-26 14:46:40 +00:00
|
|
|
file->options = options;
|
|
|
|
file->cbs = cbs; /* the given pointer must outlive the lifespan of the while file handling cycle. */
|
|
|
|
file->sendfile_ok = hio_dev_sck_sendfileok(csck);
|
|
|
|
file->peer_tmridx = HIO_TMRIDX_INVALID;
|
|
|
|
file->peer = -1;
|
|
|
|
|
2023-11-16 03:29:54 +00:00
|
|
|
bind_task_to_client (file, csck); /* the file task's reference count is incremented */
|
2023-11-16 10:20:50 +00:00
|
|
|
bound_to_client = 1;
|
2023-11-18 07:31:24 +00:00
|
|
|
|
|
|
|
if (hio_svc_htts_task_handleexpect100((hio_svc_htts_task_t*)file, 0) <= -1) goto oops;
|
2023-03-26 14:46:40 +00:00
|
|
|
if (setup_for_content_length(file, req) <= -1) goto oops;
|
|
|
|
|
|
|
|
if (bind_task_to_peer(file, req, actual_file, mime_type) <= -1) goto oops;
|
2023-11-16 10:20:50 +00:00
|
|
|
bound_to_peer = 1;
|
2023-03-26 14:46:40 +00:00
|
|
|
|
2020-11-16 16:52:49 +00:00
|
|
|
/* TODO: store current input watching state and use it when destroying the file data */
|
2021-07-22 07:30:20 +00:00
|
|
|
if (hio_dev_sck_read(csck, !(file->over & FILE_OVER_READ_FROM_CLIENT)) <= -1) goto oops;
|
|
|
|
hio_freemem (hio, actual_file);
|
2023-01-11 17:02:09 +00:00
|
|
|
|
2023-01-27 15:41:44 +00:00
|
|
|
HIO_SVC_HTTS_TASKL_APPEND_TASK (&htts->task, (hio_svc_htts_task_t*)file);
|
2023-11-16 10:20:50 +00:00
|
|
|
HIO_SVC_HTTS_TASK_RCDOWN ((hio_svc_htts_task_t*)file);
|
2023-11-18 07:31:24 +00:00
|
|
|
|
|
|
|
/* set the on_kill callback only if this function can return success.
|
|
|
|
* the on_kill callback won't be executed if this function returns failure. */
|
|
|
|
file->on_kill = on_kill;
|
2020-07-08 05:24:40 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
oops:
|
2022-10-08 07:47:55 +00:00
|
|
|
HIO_DEBUG2 (hio, "HTTS(%p) - file(c=%d) failure\n", htts, csck->hnd);
|
2023-11-16 03:29:54 +00:00
|
|
|
if (file)
|
|
|
|
{
|
2023-11-18 07:31:24 +00:00
|
|
|
hio_svc_htts_task_sendfinalres(file, status_code, HIO_NULL, HIO_NULL, 1);
|
2023-11-16 10:20:50 +00:00
|
|
|
if (bound_to_peer) unbind_task_from_peer (file, 0);
|
|
|
|
if (bound_to_client) unbind_task_from_client (file, 0);
|
2023-11-16 03:29:54 +00:00
|
|
|
file_halt_participating_devices (file);
|
2023-11-18 07:31:24 +00:00
|
|
|
if (actual_file) hio_freemem (hio, actual_file);
|
2023-11-16 10:20:50 +00:00
|
|
|
HIO_SVC_HTTS_TASK_RCDOWN ((hio_svc_htts_task_t*)file);
|
2023-11-16 03:29:54 +00:00
|
|
|
}
|
2020-07-08 05:24:40 +00:00
|
|
|
return -1;
|
|
|
|
}
|