-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.c
More file actions
60 lines (51 loc) · 1.63 KB
/
Copy patherror.c
File metadata and controls
60 lines (51 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* error.c - Code and data for Error Handling
*
* Author: Thomas Cherryhomes <thom.cherryhomes@gmail.com>
* Version: 1.0
*/
#include <string.h>
#include "error.h"
#include "log.h"
#include <syslog.h>
int error_code=0;
char error_message[2048];
void set_error(int _error_code, const char* _error_message)
{
error_code=_error_code;
strncpy(error_message,_error_message,sizeof(error_message));
}
void return_error(struct mg_connection* nc, struct http_message* hm)
{
int http_result_code=500;
char* path = NULL;
char reply[1024];
if (nc==NULL)
{
syslog(LOG_ALERT,"return_error(): mg_connection is NULL, so can't return error to client! error code %d, error message: %s",error_code, error_message);
return;
}
if (hm==NULL)
{
syslog(LOG_ALERT,"return_error(): http_message was NULL, so can't form access log output. error code was %d, error message was: %s",error_code, error_message);
}
switch(error_code)
{
case ERROR_PATH_NOT_FOUND:
http_result_code=404;
break;
case ERROR_NULL_VALUE:
case ERROR_EMPTY_VALUE:
http_result_code=400;
break;
default:
http_result_code=500;
break;
}
path=strndup(hm->uri.p,hm->uri.len);
snprintf(reply,sizeof(reply),"{ \"result\": \"error\", \"error_code\":\"%d\", \"error_message\":\"%s\" }", error_code,error_message);
mg_printf(nc,"HTTP/1.1 %d OK\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s",http_result_code,strlen(reply),reply);
syslog(LOG_ALERT,"return_error(): error code %d, error message: %s",error_code,error_message);
access_log(nc,hm,"GET",http_result_code,0);
free(path);
}