include nxweb nxweb include stdio include sqlite3 const char DATABASE_

  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
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "nxweb/nxweb.h"
#include <stdio.h>
#include <sqlite3.h>
const char* DATABASE_FILE = "python/tests.db";
const char* HTML_FILE = "python/page.html";
struct sqlite_result
{
size_t size;
size_t allocated_size;
char* string;
};
static int callback(void* result_ptr, int column_count, char** data, char** colum_names)
{
struct sqlite_result* result = result_ptr;
char* id = data[0];
char* description = data[1];
char* uri = data[2];
char row_str[10240];
if(result->size == 0)
{
snprintf(row_str, sizeof(row_str), "{ \"id\": \"%s\", \"description\": \"%s\", \"URI\": \"%s\" }", id, description, uri);
}
else
{
snprintf(row_str, sizeof(row_str), ", { \"id\": \"%s\", \"description\": \"%s\", \"URI\": \"%s\" }", id, description, uri);
}
size_t old_size = result->size;
result->size += strlen(row_str) + 1;
result->string = realloc(result->string, result->size);
if (old_size == 0)
{
result->string[0] = 0;
}
strcat(result->string, row_str);
return 0;
}
static nxweb_result testpage_on_request(nxweb_http_server_connection* conn, nxweb_http_request* req, nxweb_http_response* resp)
{
nxweb_parse_request_parameters(req, 1);
const char* param_get_data = nxweb_get_request_parameter(req, "get_data");
const char* param_set_data = nxweb_get_request_parameter(req, "set_data");
const char* param_delete = nxweb_get_request_parameter(req, "delete");
sqlite3* db;
if (sqlite3_open(DATABASE_FILE, &db) != SQLITE_OK)
{
nxweb_log_error("Failed to open database file (missing file or bad content): %s", DATABASE_FILE);
nxweb_send_http_error(resp, 500, "Failed to access tests database");
return NXWEB_OK;
}
char* error_message = NULL;
if (param_get_data)
{
struct sqlite_result result;
memset(&result, 0, sizeof(result));
if (SQLITE_OK != sqlite3_exec(db, "SELECT id, description, URI FROM Tests WHERE description != \"random image\"", callback, &result, &error_message) ||
SQLITE_OK != sqlite3_exec(db, "SELECT id, description, URI FROM Tests WHERE description == \"random image\" ORDER BY RANDOM() LIMIT 100", callback, &result, &error_message))
{
nxweb_log_error("Failed to execute tests data retrieval query, sqlite error message: %s", error_message);
nxweb_send_http_error(resp, 500, "Failed to execute database query");
}
else
{
nxweb_response_append_char(resp, '[');
nxweb_response_append_str(resp, result.string);
nxweb_response_append_char(resp, ']');
}
sqlite3_close(db);
return NXWEB_OK;
}
else if (param_set_data)
{
const char* param_desc = nxweb_get_request_parameter(req, "desc");
const char* param_uri = nxweb_get_request_parameter(req, "uri");
char query[10240];
snprintf(query, sizeof(query), "INSERT INTO Tests(description, URI) VALUES (\"%s\", \"%s\")", param_desc, param_uri);
if (SQLITE_OK != sqlite3_exec(db, query, NULL, NULL, &error_message))
{
nxweb_log_error("Failed to execute tests data insertion query");
nxweb_send_http_error(resp, 500, "Failed to execute database query");
}
sqlite3_close(db);
return NXWEB_OK;
}
else if (param_delete)
{
char query[10240];
snprintf(query, sizeof(query), "DELETE FROM Tests WHERE id = %s", param_delete);
if (SQLITE_OK != sqlite3_exec(db, query, NULL, NULL, &error_message))
{
nxweb_log_error("Failed to execute tests data removal query");
nxweb_send_http_error(resp, 500, "Failed to execute database query");
}
sqlite3_close(db);
return NXWEB_OK;
}
else
{
size_t buffer_size = 10240;
char* html_buffer = malloc(buffer_size);
size_t buffer_index = 0;
FILE* html_file = fopen(HTML_FILE, "r");
if (HTML_FILE == NULL)
{
nxweb_log_error("Failed to read a file: %s", HTML_FILE);
nxweb_send_http_error(resp, 500, "Failed to read a file");
sqlite3_close(db);
return NXWEB_OK;
}
size_t chunk_size = 0;
while ((chunk_size = fread(html_buffer + buffer_index, 1, 1024, html_file)) == 1024)
{
buffer_index += chunk_size;
if (buffer_size - buffer_index < 1024)
{
buffer_size *= 2;
html_buffer = realloc(html_buffer, buffer_size);
}
}
buffer_index += chunk_size;
html_buffer = realloc(html_buffer, buffer_index);
fclose(html_file);
nxweb_response_append_str(resp, html_buffer);
free(html_buffer);
return NXWEB_OK;
}
return NXWEB_OK;
}
NXWEB_DEFINE_HANDLER(testpage, .on_request = testpage_on_request, .flags = NXWEB_HANDLE_GET);