add support for HEAD method

master
tiptorrent development team 2021-09-13 11:32:21 +02:00
parent d6dbb6c979
commit cbcc71db72
3 changed files with 39 additions and 17 deletions

View File

@ -48,10 +48,12 @@ static void tip_client_release(struct ev_loop *loop, struct tip_client *cli)
free((void *)cli->uri);
free((void *)cli->path);
num_clients--;
if (tip_client_large_file(cli))
tip_client_activate_pending();
if (cli->method == TIP_METHOD_GET) {
num_clients--;
if (tip_client_large_file(cli))
tip_client_activate_pending();
}
free(cli);
}
@ -161,11 +163,15 @@ static void tip_client_read_cb(struct ev_loop *loop, struct ev_io *io, int event
ntohs(cli->addr.sin_port));
goto close;
}
syslog(LOG_INFO, "client %s:%hu starts download for %s\n",
inet_ntoa(cli->addr.sin_addr),
ntohs(cli->addr.sin_port), cli->uri);
if (cli->method == TIP_METHOD_GET) {
syslog(LOG_INFO, "client %s:%hu starts download for %s\n",
inet_ntoa(cli->addr.sin_addr),
ntohs(cli->addr.sin_port), cli->uri);
} else {
syslog(LOG_INFO, "client %s:%hu checks if %s exists\n",
inet_ntoa(cli->addr.sin_addr),
ntohs(cli->addr.sin_port), cli->uri);
}
ev_io_stop(loop, &cli->io);
ev_io_set(&cli->io, tip_client_socket(cli), EV_READ | EV_WRITE);
ev_io_start(loop, &cli->io);

View File

@ -26,6 +26,11 @@ enum tip_client_state {
TIP_CLIENT_CLOSE_WAIT,
};
enum tip_http_method {
TIP_METHOD_GET = 0,
TIP_METHOD_HEAD,
};
struct tip_client {
struct list_head list;
struct ev_io io;
@ -39,6 +44,7 @@ struct tip_client {
char auth_token[64];
/* for file serving. */
enum tip_http_method method;
const char *uri;
const char *path;
off_t size;
@ -73,12 +79,6 @@ int tip_client_state_process_payload(struct tip_client *cli);
int tip_client_state_process_payload_reply(struct tip_client *cli);
int tip_client_state_process_payload_bulk(struct tip_client *cli);
enum tip_http_method {
TIP_METHOD_GET = 0,
TIP_METHOD_POST,
TIP_METHOD_NO_HTTP
};
struct tip_client_redirect {
struct list_head list;
struct sockaddr_in addr;

View File

@ -55,7 +55,6 @@ static bool sanitize(const char *uri)
int tip_client_state_process_payload(struct tip_client *cli)
{
const char *trailer, *x_redirect;
enum tip_http_method method;
bool allow_redirect = true;
char _uri[32], *uri = _uri;
char path[PATH_MAX + 1];
@ -68,9 +67,13 @@ int tip_client_state_process_payload(struct tip_client *cli)
ntohs(cli->addr.sin_port), cli->buf); */
if (!strncmp(cli->buf, "GET", strlen("GET"))) {
method = TIP_METHOD_GET;
cli->method = TIP_METHOD_GET;
if (sscanf(cli->buf, "GET %31s HTTP/1.1", uri) != 1)
return tip_client_method_not_found(cli);
} else if (!strncmp(cli->buf, "HEAD", strlen("HEAD"))) {
cli->method = TIP_METHOD_HEAD;
if (sscanf(cli->buf, "HEAD %31s HTTP/1.1", uri) != 1)
return tip_client_method_not_found(cli);
} else {
return tip_client_method_not_found(cli);
}
@ -98,6 +101,12 @@ int tip_client_state_process_payload(struct tip_client *cli)
cli->uri = strdup(uri);
cli->path = strdup(path);
cli->size = st.st_size;
if (cli->method == TIP_METHOD_HEAD) {
cli->state = TIP_CLIENT_PROCESSING_REQUEST_2;
return 0;
}
cli->allow_redirect = allow_redirect;
num_clients++;
@ -139,7 +148,14 @@ int tip_client_state_process_payload_reply(struct tip_client *cli)
send(tip_client_socket(cli), buf, strlen(buf), 0);
cli->fd = fd;
cli->state = TIP_CLIENT_PROCESSING_REQUEST_3;
switch (cli->method) {
case TIP_METHOD_GET:
cli->state = TIP_CLIENT_PROCESSING_REQUEST_3;
break;
case TIP_METHOD_HEAD:
/* close connection. */
return 1;
}
return 0;
}