#include #include #include #include #include #include #include void ParseeRequest(HttpServerContext *ctx, void *argp) { ParseeData *data = argp; char *path = HttpRequestPath(ctx); HashMap *response = NULL; Stream *stream = HttpServerStream(ctx); int encodedSize; char *encodedStr; ParseeHttpArg arg; /* Basic headers */ HttpResponseStatus(ctx, HTTP_OK); HttpResponseHeader(ctx, "Server", NAME "/v" VERSION); HttpResponseHeader(ctx, "Connection", "close"); arg.data = data; arg.ctx = ctx ; arg.stream = stream; Log(LOG_NOTICE, "%s %s", HttpRequestMethodToString(HttpRequestMethodGet(ctx)), path ); if (!HttpRouterRoute(data->router, path, &arg, (void **) &response)) { Log(LOG_NOTICE, "Couldn't route %s", path); HttpResponseStatus(ctx, HTTP_NOT_FOUND); JsonFree(response); response = MatrixCreateError("M_NOT_FOUND", "Route not found."); /* TODO: Set a thing */ } /* Whatever, we routed a thing. */ if (response) { encodedSize = JsonEncode(response, NULL, JSON_DEFAULT); encodedStr = StrInt(encodedSize); HttpResponseHeader(ctx, "Content-Length", encodedStr); HttpResponseHeader(ctx, "Content-Type", "application/json"); Free(encodedStr); HttpSendHeaders(ctx); JsonEncode(response, stream, JSON_DEFAULT); JsonFree(response); return; } } HttpClientContext * ParseeCreateRequest(ParseeConfig *conf, HttpRequestMethod meth, char *path) { HttpClientContext *ctx; if (!conf || !path) { return NULL; } ctx = HttpRequest( meth, HTTP_FLAG_TLS, conf->homeserver_port, conf->homeserver_host, path ); return ctx; } HttpStatus ParseeSetRequestJSON(HttpClientContext *ctx, HashMap *json) { Stream *stream; int size; char *sizestr; if (!ctx || !json) { return HTTP_STATUS_UNKNOWN; } size = JsonEncode(json, NULL, JSON_DEFAULT); sizestr = StrInt(size); HttpRequestHeader(ctx, "Content-Length", sizestr); Free(sizestr); stream = HttpClientStream(ctx); HttpRequestSendHeaders(ctx); JsonEncode(json, stream, JSON_DEFAULT); return HttpRequestSend(ctx); }