--- /dev/null
+#include <malloc.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "debug.h"
+
+#include "file.h"
+
+/* read full file */
+
+int readfile (unsigned char **buffer, char *filename)
+{
+
+ /* open file */
+
+ FILE *fd = fopen (filename, "rb");
+ if (fd == NULL) {
+ VERBOSE (WARNING, PRINT ("Can't open file (%s)\n", filename));
+ return -1;
+ }
+
+ fseek (fd, 0, SEEK_END);
+ int size = ftell (fd);
+ fseek (fd, 0, SEEK_SET); /* same as rewind(f); */
+
+ /* read full file */
+
+ *buffer = calloc (size + 1, 1);
+ fread (*buffer, size, 1, fd);
+ fclose (fd);
+
+ *buffer[size] = 0;
+ return size;
+}
+
+/* vim: set ts=4 sw=4 et: */
--- /dev/null
+#ifndef __FILE_H__
+#define __FILE_H__
+
+int readfile (unsigned char **buffer, char *filename);
+
+#endif /* __FILE_H__ */
+
+/* vim: set ts=4 sw=4 et: */
#include <time.h>
#include "debug.h"
+#include "file.h"
#include "http.h"
return add_entity (buffer, (unsigned char *)response, strlen (response), "text/html", "iso-8859-1");
}
+/* error 404 */
+
+int error_404 (char **buffer, char *uri)
+{
+ add_status_line (buffer, c404);
+ add_general_header (buffer);
+ add_response_header (buffer, uri);
+
+ char *response = "<html><head><title>Error 404</title></head><body><p>File not found</p></body></html>";
+ return add_entity (buffer, (unsigned char *)response, strlen (response), "text/html", "iso-8859-1");
+}
+
/* response html */
int response_html (char **buffer, char *location, char *response)
/* main HTTP processing */
-int processing (char *data, int len, char **pdata)
+int processing (char *data, int len, char *root, char **pdata)
{
char *saved_data = data;
VERBOSE (DEBUG, PRINT ("Start processing\n"));
}
VERBOSE (INFO, PRINT ("%s %s (%s)\n", (type == get_e) ? "Get" : (type == head_e) ? "Head" : "Post", uri, version));
+ /* analyse uri */
+ char *filename = calloc (strlen (root) + strlen (uri) + 2, 1);
+ //sprintf (filename, "%s%s%s", root, ((root[strlen (root) - 1] != '/') && (uri[0] != '/')) ? "/" : "", uri);
+ sprintf (filename, "%s/%s", root, uri);
+
/* check header */
header_t header = {0};
while (strcmp (line = find_sequence (data, len, "\r\n", &data), "") != 0) {
VERBOSE (DEBUG, PRINT ("Processing data: %s\n", data));
}
- /* cleaning */
- free (saved_data);
-
/* response */
+ char *buffer = NULL;
switch (type) {
case get_e:
- len = response_html (pdata, "http://localhost/", "<html><head><title>Test</title></head><body><p>This a test</p></body></html>");
+ len = readfile ((unsigned char **)&buffer, filename);
+ if (len == 0) {
+ len = error_404 (pdata, "http://localhost/");
+ } else {
+ len = response_html (pdata, "http://localhost/", buffer);
+ free (buffer);
+ }
break;
case head_e:
break;
break;
}
+ /* cleaning */
+ free (saved_data);
+ free (filename);
+
return len;
}
#ifndef __HTTP_H__
#define __HTTP_H__
-int processing (char *data, int len, char **pdata);
+int processing (char *data, int len, char *root, char **pdata);
#endif /* __HTTP_H__ */
/* depend: */
/* cflags: */
-/* linker: color.o debug.o http.o server.o */
+/* linker: color.o debug.o file.o http.o server.o */
#include <assert.h>
+#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* constants */
#define BUFFER_SIZE 4096
+#define ROOT_DIR "webroot"
/* macros */
char *progname = NULL;
int port = 8080;
+char *root = ROOT_DIR;
/* help function */
fprintf (fid, "usage: %s\n", progname);
fprintf (fid, " -h : help message\n");
fprintf (fid, " -p : port number (%d)\n", port);
+ fprintf (fid, " -r : web root directory (%s)\n", root);
fprintf (fid, " -v : verbose level (%d)\n", verbose);
return ret;
/* argument processing */
- while (argc-- > 1) {
+ while (argc-- > 1) {
char *arg = *(++argv);
if (arg[0] != '-') {
VERBOSE (ERROR, PERROR ("%s: invalid option -- '%s'\n", progname, arg); usage (1));
return 1;
}
break;
+ case 'r':
+ arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+ if (arg == NULL) {
+ VERBOSE (ERROR, PERROR ("%s: missing directory name\n", progname); usage (1));
+ return 1;
+ }
+ root = arg;
+ break;
case 'v':
arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
if (arg == NULL) {
}
}
+ /* check root directory */
+ VERBOSE (DEBUG, PRINT ("Check web root\n"));
+ DIR *pdir = opendir (root);
+ if (pdir == NULL) {
+ VERBOSE (ERROR, PERROR ("Can't read directory (%s)\n", root));
+ return 1;
+ }
+ closedir (pdir);
+
+ /* init network stack */
VERBOSE (DEBUG, PRINT ("Initializing socket\n"));
init_network_context ();
if (open_listening_socket (port) == 0) {
// processing
VERBOSE (DEBUG, PRINT ("Processing %s\n", data));
- len = processing ((char *)data, len, (char **)&data);
+ len = processing ((char *)data, len, root, (char **)&data);
int rc = send_data (data, len);
if (rc == 0) {