Subversion Repositories RepoView

Rev

Rev 40 | Rev 42 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 40 Rev 41
Line 1... Line 1...
1
/* $Id: main.c 40 2024-08-22 03:43:42Z nishi $ */
1
/* $Id: main.c 41 2024-08-22 04:26:35Z nishi $ */
2
 
2
 
3
#include <stdio.h>
3
#include <stdio.h>
4
 
4
 
5
#include "../config.h"
5
#include "../config.h"
6
 
6
 
Line 8... Line 8...
8
#include "rv_query.h"
8
#include "rv_query.h"
9
#include "rv_page.h"
9
#include "rv_page.h"
10
#include "rv_util.h"
10
#include "rv_util.h"
11
#include "rv_db.h"
11
#include "rv_db.h"
12
#include "rv_auth.h"
12
#include "rv_auth.h"
-
 
13
#include "rv_multipart.h"
13
 
14
 
14
#include <stdlib.h>
15
#include <stdlib.h>
15
#include <string.h>
16
#include <string.h>
16
#include <time.h>
17
#include <time.h>
17
 
18
 
Line 24... Line 25...
24
	rv_parse_query(getenv("QUERY_STRING"));
25
	rv_parse_query(getenv("QUERY_STRING"));
25
	rv_save_query('Q');
26
	rv_save_query('Q');
26
	postdata = malloc(1);
27
	postdata = malloc(1);
27
	postdata[0] = 0;
28
	postdata[0] = 0;
28
	int hasauth = 0;
29
	int hasauth = 0;
-
 
30
	int use_multi = 0;
29
	char* type = getenv("CONTENT_TYPE");
31
	char* type = getenv("CONTENT_TYPE");
30
	if(type == NULL) {
32
	if(type == NULL) {
31
		type = rv_strdup("");
33
		type = rv_strdup("");
32
	} else {
34
	} else {
33
		type = rv_strdup(type);
35
		type = rv_strdup(type);
Line 47... Line 49...
47
		for(i = 0; type[i] != 0; i++) {
49
		for(i = 0; type[i] != 0; i++) {
48
			if(type[i] == ';') {
50
			if(type[i] == ';') {
49
				type[i] = 0;
51
				type[i] = 0;
50
				i++;
52
				i++;
51
				bool found = false;
53
				bool found = false;
52
				rv_error_http();
54
				char* bd = NULL;
53
				for(; type[i] != 0; i++) {
55
				for(; type[i] != 0; i++) {
54
					if(type[i] != ' ' && type[i] != '\t') {
56
					if(type[i] != ' ' && type[i] != '\t') {
55
						int j;
57
						int j;
56
						char* boundary = type + i;
58
						char* boundary = type + i;
57
						for(j = 0; boundary[j] != 0; j++){
59
						for(j = 0; boundary[j] != 0; j++) {
58
							if(boundary[j] == '='){
60
							if(boundary[j] == '=') {
59
								boundary[j] = 0;
61
								boundary[j] = 0;
60
								printf("%s\n%s\n", boundary, boundary + j + 1);
62
								if(strcmp(boundary, "boundary") == 0) {
-
 
63
									bd = rv_strdup(boundary + j + 1);
61
								found = true;
64
									found = true;
-
 
65
								}
62
								break;
66
								break;
63
							}
67
							}
64
						}
68
						}
65
						break;
69
						break;
66
					}
70
					}
67
				}
71
				}
68
				if(!found) {
72
				if(!found) {
-
 
73
					rv_error_http();
69
					printf("Bad multipart/form-data. Parsing fail.");
74
					printf("Bad multipart/form-data. Parsing fail.");
70
					goto freeall;
75
					goto freeall;
-
 
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
					}
71
				}
103
				}
-
 
104
				if(bd != NULL) free(bd);
-
 
105
				use_multi = 1;
72
				break;
106
				break;
73
			}
107
			}
74
		}
108
		}
75
	}
109
	}
-
 
110
	if(use_multi == 0) {
76
	rv_parse_query(postdata);
111
		rv_parse_query(postdata);
77
	rv_save_query('P');
112
		rv_save_query('P');
-
 
113
	}
78
	rv_init_auth();
114
	rv_init_auth();
79
	hasauth = 1;
115
	hasauth = 1;
80
	rv_process_page();
116
	rv_process_page();
81
	printf("Content-Type: text/html\r\n");
117
	printf("Content-Type: text/html\r\n");
82
	printf("\r\n");
118
	printf("\r\n");
83
	rv_print_page();
119
	rv_print_page();
-
 
120
	if(use_multi == 0) {
84
	rv_load_query('P');
121
		rv_load_query('P');
85
	rv_free_query();
122
		rv_free_query();
-
 
123
	}
86
freeall:
124
freeall:
87
	rv_load_query('Q');
125
	rv_load_query('Q');
88
	rv_free_query();
126
	rv_free_query();
89
	rv_close_db();
127
	rv_close_db();
-
 
128
	rv_free_multipart();
90
	if(hasauth) rv_free_auth();
129
	if(hasauth) rv_free_auth();
91
	free(type);
130
	free(type);
92
}
131
}