aboutsummaryrefslogtreecommitdiffstats
path: root/P4/util.h
diff options
context:
space:
mode:
authorMatt Strapp <strap012@umn.edu>2020-12-05 11:09:22 -0600
committerMatt Strapp <strap012@umn.edu>2020-12-05 11:09:22 -0600
commit741eb8467ca0b8464261f37a6ab3ec130eb8078c (patch)
treee3e00517144ad5daf9666d2d95c423e232bc011b /P4/util.h
parentadd finishing "touches" (diff)
downloadcsci4061-741eb8467ca0b8464261f37a6ab3ec130eb8078c.tar
csci4061-741eb8467ca0b8464261f37a6ab3ec130eb8078c.tar.gz
csci4061-741eb8467ca0b8464261f37a6ab3ec130eb8078c.tar.bz2
csci4061-741eb8467ca0b8464261f37a6ab3ec130eb8078c.tar.lz
csci4061-741eb8467ca0b8464261f37a6ab3ec130eb8078c.tar.xz
csci4061-741eb8467ca0b8464261f37a6ab3ec130eb8078c.tar.zst
csci4061-741eb8467ca0b8464261f37a6ab3ec130eb8078c.zip
Start P4
Diffstat (limited to 'P4/util.h')
-rw-r--r--P4/util.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/P4/util.h b/P4/util.h
new file mode 100644
index 0000000..d197763
--- /dev/null
+++ b/P4/util.h
@@ -0,0 +1,76 @@
+#ifndef _UTIL_H
+#define _UTIL_H
+
+/**********************************************
+ * init
+ - port is the number of the port you want the server to be
+ started on
+ - initializes the connection acception/handling system
+ - YOU MUST CALL THIS EXACTLY ONCE (not once per thread,
+ but exactly one time, in the main thread of your program)
+ BEFORE USING ANY OF THE FUNCTIONS BELOW
+ - if init encounters any errors, it will call exit().
+************************************************/
+void init(int port);
+
+/**********************************************
+ * accept_connection - takes no parameters
+ - returns a file descriptor for further request processing.
+ DO NOT use the file descriptor on your own -- use
+ get_request() instead.
+ - if the return value is negative, the request should be ignored.
+***********************************************/
+int accept_connection(void);
+
+/**********************************************
+ * get_request
+ - parameters:
+ - fd is the file descriptor obtained by accept_connection()
+ from where you wish to get a request
+ - filename is the location of a character buffer in which
+ this function should store the requested filename. (Buffer
+ should be of size 1024 bytes.)
+ - returns 0 on success, nonzero on failure. You must account
+ for failures because some connections might send faulty
+ requests. This is a recoverable error - you must not exit
+ inside the thread that called get_request. After an error, you
+ must NOT use a return_request or return_error function for that
+ specific 'connection'.
+************************************************/
+int get_request(int fd, char *filename);
+
+/**********************************************
+ * return_result
+ - returns the contents of a file to the requesting client and cleans
+ up the connection to the client
+ - parameters:
+ - fd is the file descriptor obtained by accept_connection()
+ to where you wish to return the result of a request
+ - content_type is a pointer to a string that indicates the
+ type of content being returned. possible types include
+ "text/html", "text/plain", "image/gif", "image/jpeg" cor-
+ responding to .html, .txt, .gif, .jpg files.
+ - buf is a pointer to a memory location where the requested
+ file has been read into memory (the heap). return_result
+ will use this memory location to return the result to the
+ user. (remember to use -D_REENTRANT for CFLAGS.) you may
+ safely deallocate the memory after the call to
+ return_result (if it will not be cached).
+ - numbytes is the number of bytes the file takes up in buf
+ - returns 0 on success, nonzero on failure.
+************************************************/
+int return_result(int fd, char *content_type, char *buf, int numbytes);
+
+/**********************************************
+ * return_error
+ - returns an error message in response to a bad request and cleans
+ up the connection to the client
+ - parameters:
+ - fd is the file descriptor obtained by accept_connection()
+ to where you wish to return the error
+ - buf is a pointer to the location of the error text
+ - returns 0 on success, nonzero on failure.
+************************************************/
+int return_error(int fd, char *buf);
+
+#endif /* _UTIL_H */