Subversion Repositories RepoView

Rev

Rev 42 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 nishi 1
/* $Id: main.c 45 2024-08-22 05:52:56Z nishi $ */
2
 
3
#include <stdio.h>
4
 
5
#include "../config.h"
6
 
7
#include "rv_sanity.h"
8
#include "rv_query.h"
9
#include "rv_page.h"
10
#include "rv_util.h"
11
#include "rv_db.h"
3 nishi 12
#include "rv_auth.h"
41 nishi 13
#include "rv_multipart.h"
1 nishi 14
 
42 nishi 15
#ifdef USE_GRAPHICSMAGICK
16
#include "rv_magick.h"
17
#endif
18
 
1 nishi 19
#include <stdlib.h>
39 nishi 20
#include <string.h>
5 nishi 21
#include <time.h>
1 nishi 22
 
23
char* postdata;
42 nishi 24
char* nocache;
1 nishi 25
 
3 nishi 26
int main() {
5 nishi 27
	srand(time(NULL));
42 nishi 28
	nocache = malloc(512);
29
	sprintf(nocache, "?time=%llu", (unsigned long long)time(NULL));
1 nishi 30
	rv_check_sanity();
31
	rv_init_db();
42 nishi 32
	rv_init_magick();
1 nishi 33
	rv_parse_query(getenv("QUERY_STRING"));
34
	rv_save_query('Q');
35
	postdata = malloc(1);
36
	postdata[0] = 0;
39 nishi 37
	int hasauth = 0;
41 nishi 38
	int use_multi = 0;
39 nishi 39
	char* type = getenv("CONTENT_TYPE");
40
	if(type == NULL) {
41
		type = rv_strdup("");
42
	} else {
43
		type = rv_strdup(type);
1 nishi 44
	}
39 nishi 45
	if(strcmp(type, "application/x-www-form-urlencoded") == 0) {
46
		char cbuf[2];
47
		cbuf[1] = 0;
48
		while(1) {
49
			fread(cbuf, 1, 1, stdin);
50
			if(feof(stdin)) break;
51
			char* tmp = postdata;
52
			postdata = rv_strcat(tmp, cbuf);
53
			free(tmp);
54
		}
55
	} else {
56
		int i;
57
		for(i = 0; type[i] != 0; i++) {
58
			if(type[i] == ';') {
59
				type[i] = 0;
60
				i++;
61
				bool found = false;
41 nishi 62
				char* bd = NULL;
39 nishi 63
				for(; type[i] != 0; i++) {
64
					if(type[i] != ' ' && type[i] != '\t') {
40 nishi 65
						int j;
66
						char* boundary = type + i;
41 nishi 67
						for(j = 0; boundary[j] != 0; j++) {
68
							if(boundary[j] == '=') {
40 nishi 69
								boundary[j] = 0;
41 nishi 70
								if(strcmp(boundary, "boundary") == 0) {
71
									bd = rv_strdup(boundary + j + 1);
72
									found = true;
73
								}
40 nishi 74
								break;
75
							}
76
						}
39 nishi 77
						break;
78
					}
79
				}
80
				if(!found) {
41 nishi 81
					rv_error_http();
39 nishi 82
					printf("Bad multipart/form-data. Parsing fail.");
83
					goto freeall;
41 nishi 84
				} else {
85
					char* multipart = NULL;
86
					unsigned long long length = 0;
87
					char buffer[512];
88
					while(1) {
89
						int len = fread(buffer, 1, 512, stdin);
90
						if(length > 0) {
91
							char* old = multipart;
92
							multipart = malloc(length + len);
93
							int i;
94
							for(i = 0; i < length; i++) {
95
								multipart[i] = old[i];
96
							}
97
							free(old);
98
							memcpy(multipart + length, buffer, len);
99
							length += len;
100
						} else {
101
							length += len;
102
							multipart = malloc(length);
103
							memcpy(multipart, buffer, len);
104
						}
105
						if(feof(stdin)) break;
106
					}
107
					if(multipart != NULL) {
45 nishi 108
						rv_parse_multipart((unsigned char*)multipart, bd, length);
41 nishi 109
						free(multipart);
110
					}
39 nishi 111
				}
41 nishi 112
				if(bd != NULL) free(bd);
113
				use_multi = 1;
39 nishi 114
				break;
115
			}
116
		}
117
	}
41 nishi 118
	if(use_multi == 0) {
119
		rv_parse_query(postdata);
120
		rv_save_query('P');
121
	}
5 nishi 122
	rv_init_auth();
39 nishi 123
	hasauth = 1;
1 nishi 124
	rv_process_page();
125
	printf("Content-Type: text/html\r\n");
126
	printf("\r\n");
127
	rv_print_page();
41 nishi 128
	if(use_multi == 0) {
129
		rv_load_query('P');
130
		rv_free_query();
131
	}
39 nishi 132
freeall:
3 nishi 133
	rv_load_query('Q');
1 nishi 134
	rv_free_query();
135
	rv_close_db();
41 nishi 136
	rv_free_multipart();
39 nishi 137
	if(hasauth) rv_free_auth();
138
	free(type);
1 nishi 139
}