headers = NULL;
}
unsigned long long conlen = 0;
+ unsigned long long conincr = 0;
+ char* postbuf = NULL;
while(true) {
int len = recv(sock, buf, BUFFER_SIZE, 0);
if(len <= 0) goto goquit;
int i;
for(i = 0; i < len; i++) {
cbuf[0] = buf[i];
- if(conlen > 0){
+ if(conlen > 0) {
conlen--;
- printf("%c", cbuf[0]);
- fflush(stdout);
+ postbuf[conincr] = cbuf[0];
+ conincr++;
+ if(conlen == 0) {
+ if(headers != NULL) {
+ int j;
+ bool hastype = false;
+ bool form = false;
+ for(j = 0; headers[j] != NULL; j += 2) {
+ if(mandshurica_strcaseequ(headers[j], "Content-Type")) {
+ if(mandshurica_strcaseequ(headers[j + 1], "application/x-www-form-urlencoded")) {
+ form = true;
+ hastype = true;
+ break;
+ }
+ }
+ }
+ if(!hastype) {
+ send(sock, "HTTP/1.1 415 Unsupported Media Type\r\n", 28 + 9, 0);
+ send(sock, "Connection: keep-alive\r\n", 24, 0);
+ send(sock, "Content-Length: 0\r\n", 19, 0);
+ send(sock, "\r\n", 2, 0);
+ } else if(form) {
+ bool sent = false;
+ for(j = 0; (*config->libs)[j] != NULL; j++) {
+ const char* type = (const char*)dlsym((*config->libs)[j]->lib, "mod_type");
+ if(strcmp(type, MS_MOD_AUTH) == 0) {
+ const char* httppath = (const char*)dlsym((*config->libs)[j]->lib, "mod_http_path");
+ if(httppath != NULL) {
+ if(strcmp(httppath, path) == 0) {
+ int (*mod_auth)(const char*, const char*) = (int (*)(const char*, const char*))dlsym((*config->libs)[j]->lib, "mod_auth");
+ char* usr = mandshurica_parse_form(postbuf, "username");
+ char* pwd = mandshurica_parse_form(postbuf, "password");
+ printf("%s, %s\n", usr == NULL ? "(null)" : usr, pwd == NULL ? "(null)" : pwd);
+ sent = true;
+ }
+ }
+ }
+ }
+ if(!sent) {
+ send(sock, "HTTP/1.1 403 Forbidden\r\n", 15 + 9, 0);
+ send(sock, "Connection: keep-alive\r\n", 24, 0);
+ send(sock, "Content-Length: 0\r\n", 19, 0);
+ send(sock, "\r\n", 2, 0);
+ }
+ } else {
+ send(sock, "HTTP/1.1 500 Internal Server Error\r\n", 15 + 9 + 12, 0);
+ send(sock, "Connection: keep-alive\r\n", 24, 0);
+ send(sock, "Content-Length: 0\r\n", 19, 0);
+ send(sock, "\r\n", 2, 0);
+ }
+ }
+
+ fflush(stdout);
+ free(postbuf);
+ state = 0;
+ goto reset;
+ }
continue;
}
if(state == 0) {
count++;
if(count == 2) {
if(strcmp(method, "POST") == 0) {
- if(headers != NULL){
+ if(headers != NULL) {
int j;
bool haslen = false;
- for(j = 0; headers[j] != NULL; j += 2){
- if(mandshurica_strcaseequ(headers[j], "Content-Length")){
+ for(j = 0; headers[j] != NULL; j += 2) {
+ if(mandshurica_strcaseequ(headers[j], "Content-Length")) {
haslen = true;
conlen = atoll(headers[j + 1]);
+ conincr = 0;
+ postbuf = malloc(conlen + 1);
+ postbuf[conlen] = 0;
+ count = 0;
break;
}
}
- if(!haslen){
+ if(!haslen) {
send(sock, "HTTP/1.1 400 Bad Request\r\n", 17 + 9, 0);
send(sock, "Connection: keep-alive\r\n", 24, 0);
send(sock, "Content-Length: 0\r\n", 19, 0);
send(sock, "\r\n", 2, 0);
+ } else {
+ continue;
}
- }else{
+ } else {
send(sock, "HTTP/1.1 400 Bad Request\r\n", 17 + 9, 0);
send(sock, "Connection: keep-alive\r\n", 24, 0);
send(sock, "Content-Length: 0\r\n", 19, 0);
send(sock, "\r\n", 2, 0);
}
- }else if(strcmp(method, "GET") == 0) {
+ } else if(strcmp(method, "GET") == 0) {
char* chpath = mandshurica_strcat(webroot, path);
struct stat st2;
if(stat(chpath, &st2) != 0) {