163 lines
4.3 KiB
C
163 lines
4.3 KiB
C
#include "fcgi_server_internal.h"
|
|
|
|
#include "utils/logger.h"
|
|
|
|
#include <assert.h>
|
|
|
|
int
|
|
fcgi_get_content_length(const FCGX_Request* request)
|
|
{
|
|
int content_length = 0;
|
|
char* p = FCGX_GetParam(FCGI_PARAM_NAME_CONTENT_LENGTH, request->envp);
|
|
if (p != NULL) {
|
|
content_length = atoi(p);
|
|
}
|
|
|
|
return content_length;
|
|
}
|
|
|
|
|
|
fcgi_handler_status_t
|
|
check_content_length(int content_length, int client_max_body_size)
|
|
{
|
|
LOG_TRACE("check_content_length");
|
|
|
|
if (content_length > 0 && content_length <= client_max_body_size) {
|
|
LOG_DEBUG("Content-Length is ok");
|
|
return HANDLER_SUCCESS;
|
|
}
|
|
|
|
if (content_length == 0) {
|
|
LOG_DEBUG("Content-Length == 0, so there is no body");
|
|
return HANDLER_HTTP_OK;
|
|
}
|
|
|
|
if (content_length < 0) {
|
|
LOG_WARN("Content-Length < 0 (%d)", content_length);
|
|
return HANDLER_HTTP_BAD_REQUEST;
|
|
}
|
|
|
|
/* (content_length > client_max_body_size) */
|
|
LOG_WARN("Content-Length (%d) is too large (> %d)",
|
|
content_length,
|
|
client_max_body_size);
|
|
return HANDLER_HTTP_REQUEST_ENTITY_TOO_LARGE;
|
|
}
|
|
|
|
|
|
fcgi_handler_status_t
|
|
check_content_type(const FCGX_Request* request, const char* acceptable_content_type)
|
|
{
|
|
LOG_TRACE("check_content_type");
|
|
|
|
const char* content_type = FCGX_GetParam(FCGI_PARAM_NAME_CONTENT_TYPE, request->envp);
|
|
if (content_type == NULL) {
|
|
LOG_ERROR("Could not get FCGI param: "FCGI_PARAM_NAME_CONTENT_TYPE);
|
|
return HANDLER_HTTP_BAD_REQUEST;
|
|
}
|
|
|
|
if (strcmp(content_type, acceptable_content_type) == 0) {
|
|
LOG_DEBUG("Content-Type is ok");
|
|
return HANDLER_SUCCESS;
|
|
}
|
|
|
|
LOG_WARN("Content-Type is not acceptable, '%s' (!= '%s')",
|
|
content_type, acceptable_content_type);
|
|
|
|
return HANDLER_HTTP_NOT_ACCEPTABLE;
|
|
}
|
|
|
|
|
|
char*
|
|
fcgi_request_get_param(const FCGX_Request* request, const char* name, /*out*/ bool* err)
|
|
{
|
|
assert(request != NULL);
|
|
assert(name != NULL);
|
|
|
|
char* param_value = FCGX_GetParam(name, request->envp);
|
|
if (param_value == NULL) {
|
|
LOG_ERROR("Could not get fcgi parameter '%s'", name);
|
|
goto error;
|
|
}
|
|
|
|
LOG_DEBUG("fcgi_request_get_param. name: %s, value: %s", name, param_value);
|
|
if (err) {
|
|
*err = false;
|
|
}
|
|
return param_value;
|
|
|
|
error:
|
|
LOG_ERROR("fcgi_request_get_param exit with error");
|
|
if (err) {
|
|
*err = true;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
|
|
fcgi_handler_status_t
|
|
fcgi_request_load_data(const FCGX_Request* request, int content_length, char** content)
|
|
{
|
|
LOG_TRACE("fcgi_request_load_data enter");
|
|
|
|
char* data = malloc(content_length + 1);
|
|
if (data == NULL) {
|
|
LOG_ERROR("Could not allocate memory for request body (%d bytes)", content_length + 1);
|
|
return HANDLER_ERROR;
|
|
}
|
|
|
|
int actual_content_length = FCGX_GetStr(data, content_length, request->in);
|
|
|
|
if (actual_content_length != content_length) {
|
|
LOG_ERROR("fcgi_request_load_data: "
|
|
"actual_content_length(%d) != content_length(%d); content: '%.*s'",
|
|
actual_content_length, content_length,
|
|
actual_content_length, data);
|
|
free(data);
|
|
return HANDLER_HTTP_BAD_REQUEST;
|
|
}
|
|
|
|
data[content_length] = '\0';
|
|
*content = data;
|
|
|
|
LOG_DEBUG("content_length: %d", content_length);
|
|
LOG_DEBUG("content: '%s'", *content);
|
|
|
|
LOG_TRACE("fcgi_request_load_data exit");
|
|
|
|
return HANDLER_SUCCESS;
|
|
}
|
|
|
|
|
|
fcgi_handler_status_t
|
|
fcgi_printf_str(const FCGX_Request* request, const char* response, int response_len)
|
|
{
|
|
assert(request != NULL);
|
|
assert(response != NULL);
|
|
|
|
//int n = FCGX_FPrintF(request->out, response);
|
|
int n = FCGX_PutStr(response, response_len, request->out);
|
|
if (n < 0) {
|
|
LOG_ERROR("FCGX_PutStr failed");
|
|
return HANDLER_ERROR;
|
|
}
|
|
if (n != response_len) {
|
|
LOG_ERROR("response_len (%d) differs from the number of characters written (%d)",
|
|
response_len, n);
|
|
return HANDLER_ERROR;
|
|
}
|
|
|
|
return HANDLER_SUCCESS;
|
|
}
|
|
|
|
|
|
fcgi_handler_status_t
|
|
fcgi_printf_header(const FCGX_Request* request, const char* name, const char* value, int value_len)
|
|
{
|
|
if (FCGX_FPrintF(request->out, "%s: %.*s"CRLF, name, value_len, value) == -1) {
|
|
LOG_ERROR("FCGX_FPrintF failed");
|
|
return HANDLER_ERROR;
|
|
}
|
|
|
|
return HANDLER_SUCCESS;
|
|
}
|