mirror of
https://github.com/sigmasternchen/Serwer
synced 2025-03-15 07:08:54 +00:00
Update README.md
This commit is contained in:
parent
7caf5c2c64
commit
e131a268ea
1 changed files with 82 additions and 1 deletions
83
README.md
83
README.md
|
@ -1,2 +1,83 @@
|
|||
# Serwer
|
||||
Standalone Webserver
|
||||
## Standalone webserver written in C99
|
||||
|
||||
### What is this shit?
|
||||
Well, it's standalone webserver written entirely in C99 (no libs required).
|
||||
|
||||
### Why would someone need this?
|
||||
I've no idea. But I'm going to use this for my next project.
|
||||
|
||||
### Is this ... thing ... even finished?
|
||||
No. There are many functions missing and the code needs some revision (and comments I suppose).
|
||||
There is a working mode though (see [the main file](main.c) for an example).
|
||||
|
||||
If you want to help me with this project, feel free to fork and submit-pull requests.
|
||||
I also appreciate opinions, suggestions and complaints.
|
||||
|
||||
### I don't get it. Is this a webserver for a lib for webservers?
|
||||
Both, I guess. Although as I'm thinking about it, it might actually be more of a library...
|
||||
|
||||
### Okay...? So, how can I use this thing?
|
||||
|
||||
See [the main file](main.c).
|
||||
|
||||
Basically you need at least one handler.
|
||||
```c
|
||||
/**
|
||||
* @param method contains the method-type (see method_t); can be stringified with const char* ws_strm(method_t)
|
||||
* @param host the hostname (e.g. "localhost:8080" or "www.google.com")
|
||||
* @param path the requested URI
|
||||
* @param requestHeaders the headers of the request (see headers_t)
|
||||
* @param responseHeaders well... guess. (Hint: You can modify these.)
|
||||
* @param request "file" stream of the request (can be read with e.g. fscanf(2))
|
||||
* @param respinse "file" stream for the response (can be written with w.g. fprintf(2))
|
||||
*/
|
||||
int not_found(method_t method, const char* host, const char* path, headers_t requestHeaders,
|
||||
headers_t* responseHeaders, stream_t request, stream_t response) {
|
||||
// The definition of this function is defined as handler_t (don't confuse with handle_t) in /webserver.h.
|
||||
|
||||
// You can write a response like this.
|
||||
fprintf(response, "<!DOCTYPE html>\n");
|
||||
fprintf(response, "<html><head><title>Not Found</title></head><body>");
|
||||
fprintf(response, "<h1>404 - File Not Found</h1>");
|
||||
fprintf(response, "<h1>The requested file (%s) was not found on this server.</h1>", path);
|
||||
fprintf(response, "</body></html>");
|
||||
|
||||
return 404; // This is the HTTP status code by the way.
|
||||
}
|
||||
```
|
||||
|
||||
Then you can setup a server.
|
||||
```c
|
||||
srvoptions_t options = {
|
||||
.mode = LINEAR, // the mode to use
|
||||
.timeout = 30, // the connection timeout (in this case useless, because of LINEAR)
|
||||
.maxconnections = 5, // the maximum number of connections
|
||||
.loglevel = LOG_DEBUG // the log level for the logfile
|
||||
};
|
||||
|
||||
// webserver_t ws_create(const char* name, const char* host (can be NULL for default), const char* port, FILE* logfile, srvoptions_t options);
|
||||
webserver_t server = ws_create("server_name", NULL, "8080", stderr, options);
|
||||
```
|
||||
|
||||
Now you can add you handlers.
|
||||
```c
|
||||
handle_t handle = {
|
||||
.host = NULL, // the host to listen on (NULL means any)
|
||||
.path = "/*.c", // yes, you can use wildcards (NULL still means any)
|
||||
.handler = ¬_found // function pointer of the handler
|
||||
};
|
||||
ws_handle_add(&server, handle);
|
||||
```
|
||||
|
||||
Done. Now you can start it.
|
||||
```c
|
||||
ws_run(&server);
|
||||
```
|
||||
|
||||
Simple as that. : )
|
||||
|
||||
Any questions?
|
||||
|
||||
### ... Oh, you were talking to me?
|
||||
-.-
|
||||
|
|
Loading…
Reference in a new issue