mirror of
https://forge.fsky.io/lda/Parsee.git
synced 2026-03-13 13:45:10 +00:00
66 lines
1.7 KiB
C
66 lines
1.7 KiB
C
#include <Routes.h>
|
|
|
|
#include <Cytoplasm/Memory.h>
|
|
#include <Cytoplasm/Str.h>
|
|
#include <Cytoplasm/Log.h>
|
|
|
|
#include <Matrix.h>
|
|
#include <Parsee.h>
|
|
#include <AS.h>
|
|
|
|
#include <string.h>
|
|
|
|
RouteHead(RouteMedia, arr, argp)
|
|
{
|
|
ParseeHttpArg *args = argp;
|
|
HttpClientContext *cctx;
|
|
HashMap *reqh, *params;
|
|
char *server = ArrayGet(arr, 0);
|
|
char *identi = ArrayGet(arr, 1);
|
|
char *path, *key, *val;
|
|
char *hmac, *chkmak = NULL;
|
|
|
|
params = HttpRequestParams(args->ctx);
|
|
hmac = HashMapGet(params, "hmac");
|
|
|
|
/* TODO: Make it check the DB for its validicity. "Purging" would be useful.
|
|
*/
|
|
{
|
|
char *concat = StrConcat(3, server, "/", identi);
|
|
chkmak = ParseeHMACS(args->data->id, concat);
|
|
Free(concat);
|
|
}
|
|
if (!server || !identi || !hmac || !StrEquals(hmac, chkmak))
|
|
{
|
|
Free(chkmak);
|
|
HttpResponseStatus(args->ctx, HTTP_BAD_REQUEST);
|
|
return MatrixCreateError("M_NOT_YET_UPLOADED", "No server/identifier");
|
|
}
|
|
Free(chkmak);
|
|
|
|
server = HttpUrlEncode(server);
|
|
identi = HttpUrlEncode(identi);
|
|
path = StrConcat(4, "/_matrix/media/v3/download/", server, "/", identi);
|
|
cctx = ParseeCreateRequest(args->data->config, HTTP_GET, path);
|
|
ASAuthenticateRequest(args->data->config, cctx);
|
|
Free(path);
|
|
|
|
HttpRequestSendHeaders(cctx);
|
|
HttpRequestSend(cctx);
|
|
reqh = HttpResponseHeaders(cctx);
|
|
while (HashMapIterate(reqh, &key, (void **) &val))
|
|
{
|
|
HttpResponseHeader(args->ctx, key, val);
|
|
}
|
|
HttpSendHeaders(args->ctx);
|
|
if (HttpRequestMethodGet(args->ctx) != HTTP_HEAD)
|
|
{
|
|
StreamCopy(HttpClientStream(cctx), HttpServerStream(args->ctx));
|
|
}
|
|
|
|
HttpClientContextFree(cctx);
|
|
Free(server);
|
|
Free(identi);
|
|
|
|
return NULL;
|
|
}
|