Skip to content

Commit fa9fc69

Browse files
author
Beej Jorgensen
committed
new skeletonized
1 parent 8fab31e commit fa9fc69

File tree

15 files changed

+1129
-257
lines changed

15 files changed

+1129
-257
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.vscode
33
*.dSYM
44
src/server
5-
src/data.txt
5+
src/data.txt
6+
*.o

src/Makefile

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,30 @@
1-
server: server.c
2-
gcc -Wall -Wextra -g -o $@ $^
1+
OBJS=server.o net.o file.o mime.o cache.o hashtable.o llist.o
2+
3+
server: $(OBJS)
4+
gcc -Wall -Wextra -o $@ $^
5+
6+
net.o: net.c net.h
7+
gcc -Wall -Wextra -c $<
8+
9+
server.o: server.c net.h
10+
gcc -Wall -Wextra -c $<
11+
12+
file.o: file.c file.h
13+
gcc -Wall -Wextra -c $<
14+
15+
mime.o: mime.c mime.h
16+
gcc -Wall -Wextra -c $<
17+
18+
cache.o: cache.c cache.h
19+
gcc -Wall -Wextra -c $<
20+
21+
hashtable.o: hashtable.c hashtable.h
22+
gcc -Wall -Wextra -c $<
23+
24+
llist.o: llist.c llist.h
25+
gcc -Wall -Wextra -c $<
26+
27+
.PHONY: clean
28+
29+
clean:
30+
rm -f $(OBJS)

src/cache.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include "hashtable.h"
5+
#include "cache.h"
6+
7+
/**
8+
* Allocate a cache entry
9+
*/
10+
struct cache_entry *alloc_entry(char *path, char *content_type, void *content, int content_length)
11+
{
12+
///////////////////
13+
// IMPLEMENT ME! //
14+
///////////////////
15+
}
16+
17+
/**
18+
* Deallocate a cache entry
19+
*/
20+
void free_entry(void *v_ent, void *varg)
21+
{
22+
///////////////////
23+
// IMPLEMENT ME! //
24+
///////////////////
25+
}
26+
27+
/**
28+
* Insert a cache entry at the head of the linked list
29+
*/
30+
void dllist_insert_head(struct cache *cache, struct cache_entry *ce)
31+
{
32+
// Insert at the head of the list
33+
if (cache->head == NULL) {
34+
cache->head = cache->tail = ce;
35+
ce->prev = ce->next = NULL;
36+
} else {
37+
cache->head->prev = ce;
38+
ce->next = cache->head;
39+
ce->prev = NULL;
40+
cache->head = ce;
41+
}
42+
}
43+
44+
/**
45+
* Move a cache entry to the head of the list
46+
*/
47+
void dllist_move_to_head(struct cache *cache, struct cache_entry *ce)
48+
{
49+
if (ce != cache->head) {
50+
if (ce == cache->tail) {
51+
// We're the tail
52+
cache->tail = ce->prev;
53+
cache->tail->next = NULL;
54+
55+
} else {
56+
// We're neither the head nor the tail
57+
ce->prev->next = ce->next;
58+
ce->next->prev = ce->prev;
59+
}
60+
61+
ce->next = cache->head;
62+
cache->head->prev = ce;
63+
ce->prev = NULL;
64+
cache->head = ce;
65+
}
66+
}
67+
68+
69+
/**
70+
* Removes the tail from the list and returns it
71+
*
72+
* NOTE: does not deallocate the tail
73+
*/
74+
struct cache_entry *dllist_remove_tail(struct cache *cache)
75+
{
76+
struct cache_entry *oldtail = cache->tail;
77+
78+
cache->tail = oldtail->prev;
79+
cache->tail->next = NULL;
80+
81+
cache->cur_size--;
82+
83+
return oldtail;
84+
}
85+
86+
/**
87+
* Create a new cache
88+
*
89+
* max_size: maximum number of entries in the cache
90+
* hashsize: hashtable size (0 for default)
91+
*/
92+
struct cache *cache_create(int max_size, int hashsize)
93+
{
94+
///////////////////
95+
// IMPLEMENT ME! //
96+
///////////////////
97+
}
98+
99+
/**
100+
* Store an entry in the cache
101+
*
102+
* This will also remove the least-recently-used items as necessary.
103+
*
104+
* NOTE: doesn't check for duplicate cache entries
105+
*/
106+
void cache_put(struct cache *cache, char *path, char *content_type, void *content, int content_length)
107+
{
108+
///////////////////
109+
// IMPLEMENT ME! //
110+
///////////////////
111+
}
112+
113+
/**
114+
* Retrieve an entry from the cache
115+
*/
116+
struct cache_entry *cache_get(struct cache *cache, char *path)
117+
{
118+
///////////////////
119+
// IMPLEMENT ME! //
120+
///////////////////
121+
}

src/cache.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef _WEBCACHE_H_
2+
#define _WEBCACHE_H_
3+
4+
// Individual hash table entry
5+
struct cache_entry {
6+
///////////////////
7+
// IMPLEMENT ME! //
8+
///////////////////
9+
10+
struct cache_entry *prev, *next; // Doubly-linked list
11+
};
12+
13+
// A cache
14+
struct cache {
15+
struct hashtable *index;
16+
struct cache_entry *head, *tail; // Doubly-linked list
17+
int max_size; // Maxiumum number of entries
18+
int cur_size; // Current number of entries
19+
};
20+
21+
extern struct cache *cache_create(int max_size, int hashsize);
22+
extern void cache_put(struct cache *cache, char *path, char *content_type, void *content, int content_length);
23+
extern struct cache_entry *cache_get(struct cache *cache, char *path);
24+
25+
#endif

src/file.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <sys/stat.h>
4+
#include "file.h"
5+
6+
/**
7+
* Loads a file into memory and returns a pointer to the data.
8+
*
9+
* Buffer is not NUL-terminated.
10+
*/
11+
struct file_data *file_load(char *filename)
12+
{
13+
char *buffer, *p;
14+
struct stat buf;
15+
int bytes_read, bytes_remaining, total_bytes = 0;
16+
17+
// Get the file size
18+
if (stat(filename, &buf) == -1) {
19+
return NULL;
20+
}
21+
22+
// Make sure it's a regular file
23+
if (!(buf.st_mode & S_IFREG)) {
24+
return NULL;
25+
}
26+
27+
// Open the file for reading
28+
FILE *fp = fopen(filename, "rb");
29+
30+
if (fp == NULL) {
31+
return NULL;
32+
}
33+
34+
// Allocate that many bytes
35+
bytes_remaining = buf.st_size;
36+
p = buffer = malloc(bytes_remaining);
37+
38+
if (buffer == NULL) {
39+
return NULL;
40+
}
41+
42+
// Read in the entire file
43+
while (bytes_read = fread(p, 1, bytes_remaining, fp), bytes_read != 0 && bytes_remaining > 0) {
44+
if (bytes_read == -1) {
45+
free(buffer);
46+
return NULL;
47+
}
48+
49+
bytes_remaining -= bytes_read;
50+
p += bytes_read;
51+
total_bytes += bytes_read;
52+
}
53+
54+
// Allocate the file data struct
55+
struct file_data *filedata = malloc(sizeof *filedata);
56+
57+
if (filedata == NULL) {
58+
free(buffer);
59+
return NULL;
60+
}
61+
62+
filedata->data = buffer;
63+
filedata->size = total_bytes;
64+
65+
return filedata;
66+
}
67+
68+
/**
69+
* Frees memory allocated by file_load().
70+
*/
71+
void file_free(struct file_data *filedata)
72+
{
73+
free(filedata->data);
74+
free(filedata);
75+
}

src/file.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef _FILE_H_
2+
#define _FILE_H_
3+
4+
struct file_data {
5+
int size;
6+
void *data;
7+
};
8+
9+
extern struct file_data *file_load(char *filename);
10+
extern void file_free(struct file_data *filedata);
11+
12+
#endif

0 commit comments

Comments
 (0)