mirror of
https://github.com/transmission/transmission
synced 2024-12-28 10:38:45 +00:00
refactor: use std::vector in extract_parts_from_multipart() (#1860)
* refactor: use std::vector in extract_parts_from_multipart()
This commit is contained in:
parent
78e571d6f2
commit
e47762c7ca
1 changed files with 21 additions and 37 deletions
|
@ -27,7 +27,6 @@
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
#include "platform.h" /* tr_getWebClientDir() */
|
#include "platform.h" /* tr_getWebClientDir() */
|
||||||
#include "ptrarray.h"
|
|
||||||
#include "rpcimpl.h"
|
#include "rpcimpl.h"
|
||||||
#include "rpc-server.h"
|
#include "rpc-server.h"
|
||||||
#include "session.h"
|
#include "session.h"
|
||||||
|
@ -108,21 +107,14 @@ static void send_simple_response(struct evhttp_request* req, int code, char cons
|
||||||
|
|
||||||
struct tr_mimepart
|
struct tr_mimepart
|
||||||
{
|
{
|
||||||
char* headers;
|
std::string headers;
|
||||||
size_t headers_len;
|
std::string body;
|
||||||
char* body;
|
|
||||||
size_t body_len;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void tr_mimepart_free(struct tr_mimepart* p)
|
static auto extract_parts_from_multipart(struct evkeyvalq const* headers, struct evbuffer* body)
|
||||||
{
|
{
|
||||||
tr_free(p->body);
|
auto ret = std::vector<tr_mimepart>{};
|
||||||
tr_free(p->headers);
|
|
||||||
tr_free(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void extract_parts_from_multipart(struct evkeyvalq const* headers, struct evbuffer* body, tr_ptrArray* setme_parts)
|
|
||||||
{
|
|
||||||
char const* content_type = evhttp_find_header(headers, "Content-Type");
|
char const* content_type = evhttp_find_header(headers, "Content-Type");
|
||||||
char const* in = (char const*)evbuffer_pullup(body, -1);
|
char const* in = (char const*)evbuffer_pullup(body, -1);
|
||||||
size_t inlen = evbuffer_get_length(body);
|
size_t inlen = evbuffer_get_length(body);
|
||||||
|
@ -152,17 +144,17 @@ static void extract_parts_from_multipart(struct evkeyvalq const* headers, struct
|
||||||
|
|
||||||
if (rnrn != nullptr)
|
if (rnrn != nullptr)
|
||||||
{
|
{
|
||||||
struct tr_mimepart* p = tr_new(struct tr_mimepart, 1);
|
auto tmp = tr_mimepart{};
|
||||||
p->headers_len = (size_t)(rnrn - part);
|
tmp.headers.assign(part, rnrn - part);
|
||||||
p->headers = tr_strndup(part, p->headers_len);
|
tmp.body.assign(rnrn + 4, (part + part_len) - (rnrn + 4));
|
||||||
p->body_len = (size_t)((part + part_len) - (rnrn + 4));
|
ret.push_back(tmp);
|
||||||
p->body = tr_strndup(rnrn + 4, p->body_len);
|
|
||||||
tr_ptrArrayAppend(setme_parts, p);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tr_free(boundary);
|
tr_free(boundary);
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handle_upload(struct evhttp_request* req, struct tr_rpc_server* server)
|
static void handle_upload(struct evhttp_request* req, struct tr_rpc_server* server)
|
||||||
|
@ -173,26 +165,21 @@ static void handle_upload(struct evhttp_request* req, struct tr_rpc_server* serv
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int n;
|
|
||||||
bool hasSessionId = false;
|
bool hasSessionId = false;
|
||||||
auto parts = tr_ptrArray{};
|
|
||||||
|
|
||||||
char const* query = strchr(req->uri, '?');
|
char const* query = strchr(req->uri, '?');
|
||||||
bool const paused = query != nullptr && strstr(query + 1, "paused=true") != nullptr;
|
bool const paused = query != nullptr && strstr(query + 1, "paused=true") != nullptr;
|
||||||
|
|
||||||
extract_parts_from_multipart(req->input_headers, req->input_buffer, &parts);
|
auto const parts = extract_parts_from_multipart(req->input_headers, req->input_buffer);
|
||||||
n = tr_ptrArraySize(&parts);
|
|
||||||
|
|
||||||
/* first look for the session id */
|
/* first look for the session id */
|
||||||
for (int i = 0; i < n; ++i)
|
for (auto const& p : parts)
|
||||||
{
|
{
|
||||||
auto const* const p = static_cast<struct tr_mimepart const*>(tr_ptrArrayNth(&parts, i));
|
if (tr_strcasestr(p.headers.c_str(), TR_RPC_SESSION_ID_HEADER) != nullptr)
|
||||||
|
|
||||||
if (tr_strcasestr(p->headers, TR_RPC_SESSION_ID_HEADER) != nullptr)
|
|
||||||
{
|
{
|
||||||
char const* ours = get_current_session_id(server);
|
char const* ours = get_current_session_id(server);
|
||||||
size_t const ourlen = strlen(ours);
|
size_t const ourlen = strlen(ours);
|
||||||
hasSessionId = ourlen <= p->body_len && memcmp(p->body, ours, ourlen) == 0;
|
hasSessionId = ourlen <= std::size(p.body) && memcmp(p.body.c_str(), ours, ourlen) == 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -208,15 +195,14 @@ static void handle_upload(struct evhttp_request* req, struct tr_rpc_server* serv
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (int i = 0; i < n; ++i)
|
for (auto const& p : parts)
|
||||||
{
|
{
|
||||||
auto const* const p = static_cast<struct tr_mimepart const*>(tr_ptrArrayNth(&parts, i));
|
auto const& body = p.body;
|
||||||
size_t body_len = p->body_len;
|
size_t body_len = std::size(body);
|
||||||
tr_variant top;
|
tr_variant top;
|
||||||
tr_variant* args;
|
tr_variant* args;
|
||||||
tr_variant test;
|
tr_variant test;
|
||||||
bool have_source = false;
|
bool have_source = false;
|
||||||
char const* body = p->body;
|
|
||||||
|
|
||||||
if (body_len >= 2 && memcmp(&body[body_len - 2], "\r\n", 2) == 0)
|
if (body_len >= 2 && memcmp(&body[body_len - 2], "\r\n", 2) == 0)
|
||||||
{
|
{
|
||||||
|
@ -228,14 +214,14 @@ static void handle_upload(struct evhttp_request* req, struct tr_rpc_server* serv
|
||||||
args = tr_variantDictAddDict(&top, TR_KEY_arguments, 2);
|
args = tr_variantDictAddDict(&top, TR_KEY_arguments, 2);
|
||||||
tr_variantDictAddBool(args, TR_KEY_paused, paused);
|
tr_variantDictAddBool(args, TR_KEY_paused, paused);
|
||||||
|
|
||||||
if (tr_urlIsValid(body, body_len))
|
if (tr_urlIsValid(body.c_str(), body_len))
|
||||||
{
|
{
|
||||||
tr_variantDictAddRaw(args, TR_KEY_filename, body, body_len);
|
tr_variantDictAddRaw(args, TR_KEY_filename, body.c_str(), body_len);
|
||||||
have_source = true;
|
have_source = true;
|
||||||
}
|
}
|
||||||
else if (tr_variantFromBenc(&test, body, body_len) == 0)
|
else if (tr_variantFromBenc(&test, body.c_str(), body_len) == 0)
|
||||||
{
|
{
|
||||||
auto* b64 = static_cast<char*>(tr_base64_encode(body, body_len, nullptr));
|
auto* b64 = static_cast<char*>(tr_base64_encode(body.c_str(), body_len, nullptr));
|
||||||
tr_variantDictAddStr(args, TR_KEY_metainfo, b64);
|
tr_variantDictAddStr(args, TR_KEY_metainfo, b64);
|
||||||
tr_free(b64);
|
tr_free(b64);
|
||||||
have_source = true;
|
have_source = true;
|
||||||
|
@ -250,8 +236,6 @@ static void handle_upload(struct evhttp_request* req, struct tr_rpc_server* serv
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tr_ptrArrayDestruct(&parts, (PtrArrayForeachFunc)tr_mimepart_free);
|
|
||||||
|
|
||||||
/* send "success" response */
|
/* send "success" response */
|
||||||
{
|
{
|
||||||
int code = HTTP_OK;
|
int code = HTTP_OK;
|
||||||
|
|
Loading…
Reference in a new issue