summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--file.c51
-rw-r--r--file.h7
-rw-r--r--http.c2
-rw-r--r--net.c19
4 files changed, 60 insertions, 19 deletions
diff --git a/file.c b/file.c
index a8fef33..98b5d50 100644
--- a/file.c
+++ b/file.c
@@ -8,8 +8,19 @@
#include <sys/stat.h>
#include <unistd.h>
-size_t file_read(const char *uri_path, char **buffer)
+int file_handle_request(struct http_request *req, int sockfd)
{
+ if (strstr(req->uri, ".php") != 0) {
+ return file_read_cgi(req->uri, sockfd);
+ }
+ return file_read(req->uri, sockfd);
+}
+
+int file_read(const char *uri_path, int sockfd)
+{
+ /* TODO: Implement this again */
+ return 1;
+
FILE *fp;
struct stat statbuf;
char path[PATH_MAX];
@@ -39,9 +50,43 @@ size_t file_read(const char *uri_path, char **buffer)
len = ftell(fp);
fseek(fp, 0, SEEK_SET);
- *buffer = malloc(len);
- fread(*buffer, 1, len, fp);
+ /*buffer = malloc(len);*/
+ /*fread(*buffer, 1, len, fp);*/
fclose(fp);
return len;
}
+
+int file_read_cgi(const char *uri_path, int sockfd)
+{
+ /* TODO: Lazy copy paste just to get it working */
+ FILE *fp;
+ struct stat statbuf;
+ char cmdbuf[PATH_MAX];
+ char path[PATH_MAX];
+ char ch;
+
+ /* Prepend the current working directory to the uri path */
+ getcwd(path, PATH_MAX);
+ strncat(path, uri_path, PATH_MAX - 1);
+
+ /* Append 'index.php' to directory paths. */
+ stat(path, &statbuf);
+ if (S_ISDIR(statbuf.st_mode))
+ strcat(path, "index.php");
+
+ strcpy(cmdbuf, "php-cgi ");
+ strcat(cmdbuf, path);
+
+ fp = popen(cmdbuf, "r");
+ if (fp == NULL) {
+ return 1;
+ }
+
+ while ((ch = fgetc(fp)) != EOF) {
+ write(sockfd, &ch, 1);
+ }
+
+ pclose(fp);
+ return 0;
+}
diff --git a/file.h b/file.h
index 304cc75..ee39eae 100644
--- a/file.h
+++ b/file.h
@@ -3,6 +3,11 @@
#include <stddef.h>
-size_t file_read(const char *, char **);
+#include "http.h"
+
+int file_handle_request(struct http_request *, int);
+
+int file_read(const char *, int);
+int file_read_cgi(const char *, int);
#endif
diff --git a/http.c b/http.c
index b6bad8e..40a2b82 100644
--- a/http.c
+++ b/http.c
@@ -32,5 +32,5 @@ void http_free_request(struct http_request *req)
int http_build_response(char *res, enum http_res_code code, const char *msg, size_t msglen)
{
- return snprintf(res, CFWS_MAX_RESPONSE, "HTTP/1.1 200 OK\r\n\r\n%.*s\r\n", msglen, msg);
+ return snprintf(res, CFWS_MAX_RESPONSE, "HTTP/1.1 200 OK\r\n%.*s\r\n", msglen, msg);
}
diff --git a/net.c b/net.c
index e13d664..c7d075a 100644
--- a/net.c
+++ b/net.c
@@ -53,10 +53,7 @@ int initialize_server(int port)
void handle_connection(int connfd)
{
- char *content_buf;
- size_t content_len, response_len;
-
- char resbuf[CFWS_MAX_RESPONSE];
+ const char *header = "HTTP/1.1 200 OK\r\n";
char readbuf[CFWS_MAXREAD];
struct http_request req;
@@ -65,17 +62,11 @@ void handle_connection(int connfd)
req = http_parse_request(readbuf);
- content_len = file_read(req.uri, &content_buf);
- if (content_len == 0) {
- const char *msg = "Could not find the specified file.";
- response_len = http_build_response(resbuf,
- HTTP_RESPONSE_NOTFOUND, msg, strlen(msg));
- } else {
- response_len = http_build_response(resbuf, HTTP_RESPONSE_OK,
- content_buf, content_len);
- free(content_buf);
+ write(connfd, header, strlen(header));
+ if (file_handle_request(&req, connfd) != 0) {
+ const char *msg = "\r\nCould not find the specified file.";
+ write(connfd, msg, strlen(msg));
}
- write(connfd, resbuf, response_len);
http_free_request(&req);
}