Skip to content
Browse files

before adding micro_httpd

  • Loading branch information...
1 parent 766e04d commit 59848a5952dc7f22482c9e496df4a9229ae0256e @aegiryy committed Mar 17, 2011
Showing with 49 additions and 15 deletions.
  1. +1 −1 include/resolver.h
  2. +11 −4 source/gate.c
  3. +4 −4 source/resolver.c
  4. +33 −6 source/server.c
View
2 include/resolver.h
@@ -3,6 +3,6 @@
void rslv_get_url(char * rawreq, char * url);
void rslv_get_method(char * rawreq, char * mtd);
-void rslv_get_postfix(char * rawreq, char * postfix);
+void rslv_get_ext(char * rawreq, char * ext);
#endif
View
15 source/gate.c
@@ -68,15 +68,22 @@ int main(int argc, char* argv[])
if(fork() == 0)
{
char * resp;
- pBuffer[read(hSocket,pBuffer,BUFFER_SIZE)] = '\0';
+ FILE * micro_httpd;
+ int sz_read;
+ pBuffer[read(hSocket,pBuffer,BUFFER_SIZE)] = EOF;
printf("%s", pBuffer);
+ micro_httpd = popen("./micro_httpd .", "r+");
+ fwrite(pBuffer, sizeof(char), strlen(pBuffer), micro_httpd);
+ while (sz_read = fread(pBuffer, sizeof(char), BUFFER_SIZE, micro_httpd))
+ {
+ write(hSocket, pBuffer, sz_read);
+ }
+ fclose(micro_httpd);
/*
- strcpy(pBuffer, "HTTP/1.1 200 OK\nDate: Thu, 10 Mar 2011 15:31:59 GMT\nContent-Type: text/html;charset=ISO-8859-1\nContent-Length: 13\n\nHello, world!");
- write(hSocket, pBuffer, strlen(pBuffer));
- */
resp = respond(pBuffer);
write(hSocket, resp, strlen(resp));
free(resp);
+ */
close(hSocket);
exit(0);
}
View
8 source/resolver.c
@@ -20,7 +20,7 @@ void rslv_get_method(char * rawreq, char * method)
method[i] = '\0';
}
-void rslv_get_postfix(char * rawreq, char * postfix)
+void rslv_get_ext(char * rawreq, char * ext)
{
int i, j;
char url[256];
@@ -29,11 +29,11 @@ void rslv_get_postfix(char * rawreq, char * postfix)
for (; url[i] != '.' && url[i] != '/'; i--);
if (url[i] == '/')
{
- postfix[0] = '\0';
+ ext[0] = '\0';
return;
}
i++;
for (j = 0; url[i] != '\0'; i++, j++)
- postfix[j] = url[i];
- postfix[j] = '\0';
+ ext[j] = url[i];
+ ext[j] = '\0';
}
View
39 source/server.c
@@ -5,32 +5,59 @@
#include "static.h"
#include "resolver.h"
+static char * mapping[] = {
+ "html", "text/html; charset=UTF-8",
+ "js", "text/javascript",
+ "png", "image/png",
+ "jpg", "image/jpeg",
+ "css", "text/css",
+ "", "text/plain"
+};
+
+static int sz_mapping = sizeof(mapping) / sizeof(mapping[0]) / 2;
+
+static int get_contenttype(char * ext);
char * respond(char * rawreq)
{
- char postfix[8];
+ char ext[8];
char * content;
char * resp;
unsigned int ctsize;
- rslv_get_postfix(rawreq, postfix);
+ rslv_get_ext(rawreq, ext);
content = static_serve(rawreq, &ctsize);
if (content == NULL)
{
- char status[] = "HTTP/1.1 404 Not Found";
+ char status[] = "HTTP/1.0 404 Not Found";
content = (char *)malloc(strlen(status) + 1);
strcpy(content, status);
return content;
}
else
{
- char status[] = "HTTP/1.1 200 OK\n";
- char header[] = "Content-Type: text/html; charset=UTF-8\n\n";
- resp = (char *)malloc(strlen(status) + strlen(header) + ctsize);
+ char status[] = "HTTP/1.0 200 OK\n";
+ char header[] = "Content-Type: ";
+ char * type;
+ char header_end[] = "\nLast-Modified: Thu, 05 Aug 2010 22:54:44 GMT\nDate: Wed, 16 Mar 2011 11:35:28 GMT\nConnection: close\n\n";
+ type = mapping[2 * get_contenttype(ext) + 1];
+ resp = (char *)malloc(strlen(status) + strlen(header) + strlen(type) + strlen(header_end) + ctsize);
resp[0] = '\0';
strcat(resp, status);
strcat(resp, header);
+ strcat(resp, type);
+ strcat(resp, header_end);
+ printf("%s\n", resp);
strcat(resp, content);
+ free(content);
return resp;
}
}
+static int get_contenttype(char * ext)
+{
+ int i;
+ for (i = 0; i < sz_mapping; i++)
+ if (strcmp(mapping[i * 2], ext) == 0)
+ return i;
+ return -1;
+}

0 comments on commit 59848a5

Please sign in to comment.
Something went wrong with that request. Please try again.