Subversion Repositories RepoView

Rev

Rev 40 | Rev 42 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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