htdocs: new archive interface. add file, rename file, delete file works. But edit doesn't work yet.

This commit is contained in:
Daniel Marjamäki 2013-07-07 16:55:22 +02:00
parent 557be8efc8
commit 15f24fe405
5 changed files with 225 additions and 8 deletions

View File

@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "webarchive.h"
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 "invalid delete command";
return NULL;
}
int main()
{
const char *query_string = getenv("QUERY_STRING");
if (query_string == NULL) {
printf("Content-type: text/plain\r\n\r\n");
puts("empty/invalid data");
} else if (NULL != validate(query_string)) {
printf("Content-type: text/plain\r\n\r\n");
puts(validate(query_string));
} else {
char *data[MAX_RECORDS] = {0};
if (!readdata(data, MAX_RECORDS)) {
printf("Content-type: text/plain\r\n\r\n");
puts("failed to delete file, try again");
}
sortdata(data, MAX_RECORDS);
char name[32] = {0};
strcpy(name, getname(query_string));
int index = -1;
for (int i = 0; i < MAX_RECORDS && data[i]; i++) {
if (strcmp(name, getname(data[i])) == 0) {
index = i;
break;
}
}
if (index == -1) {
puts("Content-type: text/plain\r\n\r\n");
puts("file not found");
return EXIT_SUCCESS;
}
if (index >= 0) {
int deleted = 0;
FILE *f = fopen("data.txt", "wt");
if (f != NULL) {
for (int i = 0; i < MAX_RECORDS && data[i]; i++) {
if (i != index)
fprintf(f, "%s\n", data[i]);
else
deleted = 1;
}
fclose(f);
puts("Content-type: text/plain\r\n\r\n");
puts(deleted ? "file deleted" : "failed to delete file");
} else {
puts("Content-type: text/plain\r\n\r\n");
puts("failed to delete file, try again");
}
}
}
return EXIT_SUCCESS;
}

104
htdocs/archive/renamefile.c Normal file
View File

@ -0,0 +1,104 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "webarchive.h"
const char *validate(const char *data)
{
int i;
// name1
if (strncmp(data,"name1=",6) != 0)
return "invalid query string: must start with 'name1='";
i = 6;
while (isalnum(data[i]))
i++;
if (i == 6)
return "invalid query string: no name1";
if (i > 36)
return "invalid query string: max name1 size is 30";
// name2
const int i1 = i;
if (strncmp(data+i,"&name2=",7) != 0)
return "invalid query string: no name2";
i += 7;
if (!isalnum(data[i]))
return "invalid query string: empty name2";
while (isalnum(data[i]))
i++;
if (i - i1 > 37)
return "invalid query string: max name2 size is 30";
if (data[i] != '\0')
return "invalid query string: invalid char in name2";
return NULL;
}
int main()
{
const char *query_string = getenv("QUERY_STRING");
if (query_string == NULL) {
printf("Content-type: text/plain\r\n\r\n");
puts("empty/invalid data");
} else if (NULL != validate(query_string)) {
printf("Content-type: text/plain\r\n\r\n");
puts(validate(query_string));
} else {
char *data[MAX_RECORDS] = {0};
if (!readdata(data, MAX_RECORDS)) {
printf("Content-type: text/plain\r\n\r\n");
puts("access failed, try again");
}
sortdata(data, MAX_RECORDS);
// Get name1 and name2..
char buf[strlen(query_string)];
strcpy(buf, query_string);
const char * const name1 = strstr(buf, "name1=") + 6;
const char * const name2 = strstr(buf, "name2=") + 6;
for (int i = 0; buf[i] != '\0'; i++) {
if (buf[i] == '&')
buf[i] = '\0';
}
int index = -1;
for (int i = 0; i < MAX_RECORDS && data[i]; i++) {
if (strcmp(name1, getname(data[i])) == 0) {
index = i;
break;
}
}
if (index == -1) {
puts("Content-type: text/plain\r\n\r\n");
puts("file not found");
return EXIT_SUCCESS;
}
FILE *f = fopen("data.txt", "wt");
if (f == NULL) {
puts("Content-type: text/plain\r\n\r\n");
puts("failed to rename file (access denied), try again");
return EXIT_SUCCESS;
}
for (int i = 0; i < MAX_RECORDS && data[i]; i++) {
if (i == index)
fprintf(f, "name=%s%s\n", name2, data[i]+5+strlen(name1));
else
fprintf(f, "%s\n", data[i]);
}
fclose(f);
puts("Content-type: text/plain\r\n\r\n");
puts("file renamed");
}
return EXIT_SUCCESS;
}

View File

@ -9,13 +9,39 @@
void listAll(char **data) void listAll(char **data)
{ {
puts("Content-type: text/html\r\n\r\n"); puts("Content-type: text/html\r\n\r\n");
puts("<html><meta http-equiv=\"Pragma\" content=\"no-cache\"><body>\n"); puts("<html>");
puts("<table>\n"); puts("<head>");
puts("<meta http-equiv=\"Pragma\" content=\"no-cache\">");
puts("<script>");
puts("function addfile() {");
puts(" var name = prompt(\"Name of library/platform/etc\", \"\");");
puts(" if (name != null)");
puts(" window.location = \"http://cppcheck.sf.net/cgi-bin/addfile.cgi?name=\" + name;");
puts("}");
puts("function editfile(name) {\n");
puts(" window.location = \"http://cppcheck.sf.net/cgi-bin/edit.cgi?name=\" + name;\n");
puts("}");
puts("function renamefile(name1) {\n");
puts(" var name2 = prompt(\"Name\", name1);\n");
puts(" if (name2 != null)\n");
puts(" window.location = \"http://cppcheck.sf.net/cgi-bin/renamefile.cgi?name1=\" + name1 + \"&name2=\" + name2;\n");
puts("}\n");
puts("function deletefile(name) {\n");
puts(" window.location = \"http://cppcheck.sf.net/cgi-bin/deletefile.cgi?name=\" + name;\n");
puts("}");
puts("</script>");
puts("</head><body>");
puts("<input type=\"button\" onclick=\"addfile()\" value=\"Add file\">");
puts("<table border=\"1\"><tr><td><table>");
for (int i = 0; i < MAX_RECORDS && data[i]; i++) { for (int i = 0; i < MAX_RECORDS && data[i]; i++) {
const char *name = getname(data[i]); const char *name = getname(data[i]);
printf("<tr><td>%s</td><td>Delete this</td></tr>\n", name); printf("<tr><td>%s</td>", name);
printf("<td><input type=\"button\" onclick=\"editfile(\'%s\')\" value=\"Edit\"></td>", name);
printf("<td><input type=\"button\" onclick=\"renamefile(\'%s\')\" value=\"Rename\"></td>", name);
printf("<td><input type=\"button\" onclick=\"deletefile(\'%s\')\" value=\"Delete\"></td>", name);
printf("</tr>\n");
} }
puts("<table>\n"); puts("</table></td></tr></table>");
puts("</body></html>"); puts("</body></html>");
} }
@ -48,7 +74,7 @@ int main()
sortdata(data,MAX_RECORDS); sortdata(data,MAX_RECORDS);
const char *query_string = getenv("QUERY_STRING"); const char *query_string = getenv("QUERY_STRING");
if (query_string == NULL) { if (query_string == NULL || *query_string == '\0') {
listAll(data); listAll(data);
} else if (strncmp(query_string, "name=", 5) == 0 && getname(query_string) != NULL) { } else if (strncmp(query_string, "name=", 5) == 0 && getname(query_string) != NULL) {
char name[32] = {0}; char name[32] = {0};

View File

@ -1,4 +1,5 @@
#include <string.h> #include <string.h>
#include <ctype.h>
#define MAX_RECORDS 1000 #define MAX_RECORDS 1000
@ -12,7 +13,10 @@ int readdata(char * * const data, int sz)
int i = 0; int i = 0;
while (i < sz && fgets(line,sizeof(line)-2,f)) { while (i < sz && fgets(line,sizeof(line)-2,f)) {
if (strncmp(line, "name=", 5) == 0) { if (strncmp(line, "name=", 5) == 0) {
data[i] = malloc(strlen(line)); int len = strlen(line);
while (line[len-1] == '\n' || line[len-1] == '\r' || line[len-1] == '\t' || line[len-1] == ' ')
line[--len] = '\0';
data[i] = malloc(len);
strcpy(data[i], line); strcpy(data[i], line);
i++; i++;
} }
@ -46,8 +50,8 @@ void sortdata(char * * const data, int sz)
char *p = data[i-1]; char *p = data[i-1];
data[i-1] = data[i]; data[i-1] = data[i];
data[i] = p; data[i] = p;
if (i > 1) if (i >= 2)
i--; i -= 2;
} }
} }
} }