cppcheck/htdocs/archive/addfile.c

86 lines
2.2 KiB
C
Raw Normal View History

2013-07-06 17:24:23 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2013-07-07 10:27:45 +02:00
#include <ctype.h>
#include "webarchive.h"
2013-07-06 17:24:23 +02:00
static void unencode(const char *src, char *dest)
{
for (; *src; src++, dest++) {
if (*src == '+')
*dest = ' ';
else if (*src == '%') {
int code;
if (sscanf(src+1, "%2x", &code) != 1)
code = '?';
*dest = code;
src += 2;
} else
*dest = *src;
}
*dest = '\0';
}
2013-07-07 10:27:45 +02:00
const char *validate(const char *data)
{
int i;
if (strncmp(data,"name=",5) != 0)
return "invalid query string: must start with 'name='";
i = 5;
while (isalnum(data[i]))
i++;
if (i == 5)
return "invalid query string: no name";
if (i > 35)
return "invalid query string: max name size is 32";
if (data[i] == '\0')
return NULL;
if (data[i] != '&')
return "invalid query string: only alphanumeric characters are allowed in the name";
if (strncmp(data+i,"&data=",6)!=0)
return "invalid query string";
i += 6;
// TODO: check XML data..
2013-07-07 11:07:23 +02:00
2013-07-07 10:27:45 +02:00
return NULL;
}
2013-07-06 17:24:23 +02:00
int main()
{
const char *query_string = getenv("QUERY_STRING");
if (query_string == NULL) {
2013-07-07 10:27:45 +02:00
printf("Content-type: text/plain\r\n\r\n");
2013-07-06 17:24:23 +02:00
printf("empty/invalid data\n");
} else if (strlen(query_string) > 1024) {
2013-07-07 10:27:45 +02:00
printf("Content-type: text/plain\r\n\r\n");
2013-07-06 17:24:23 +02:00
printf("data size limit exceeded (1024)\n");
2013-07-07 10:27:45 +02:00
} else if (NULL != validate(query_string)) {
printf("Content-type: text/plain\r\n\r\n");
printf("%s\n", validate(query_string));
2013-07-06 17:24:23 +02:00
} else {
char data[4096] = {0};
unencode(query_string, data);
2013-07-07 10:27:45 +02:00
printf("Content-type: text/plain\r\n\r\n");
if (NULL != validate(data)) {
printf("%s\n", validate(data));
} else {
char *olddata[MAX_RECORDS] = {0};
olddata[0] = data;
readdata(&olddata[1], MAX_RECORDS-1);
sortdata(olddata, MAX_RECORDS);
2013-07-06 17:24:23 +02:00
2013-07-07 10:27:45 +02:00
FILE *f = fopen("data.txt", "wt");
for (int i = 0; i < MAX_RECORDS && olddata[i]; i++)
fprintf(f, "%s\n", olddata[i]);
fclose(f);
printf("saved\n");
}
2013-07-06 17:24:23 +02:00
}
return EXIT_SUCCESS;
}