Created
February 16, 2017 13:14
-
-
Save reqshark/5cc1215ddd02c422d1103ba8426e16c0 to your computer and use it in GitHub Desktop.
first try dsock http
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//cc main.c $CFLAGS $LDFLAGS -ldill $opt/lib/libdsock.a -o server && ./server | |
#include <libdill.h> | |
#include <dsock.h> | |
#include <assert.h> | |
#include <errno.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
void recvf(int s) { | |
char n[32]; | |
char v[32]; | |
int r = http_recvfield(s, n, sizeof(n), v, sizeof(v), -1); | |
if ( r > -1 && errno != EPIPE ) { | |
printf("---\n%s\n%s\n", n, v); | |
recvf(s); | |
} | |
} | |
int main() { | |
int requests = 0; | |
int port = 5555; | |
ipaddr addr; | |
int rc = ipaddr_local(&addr, NULL, port, 0); | |
assert(rc == 0); | |
int ls = tcp_listen(&addr, 10); | |
if(ls < 0) { | |
perror("Can't open listening socket"); | |
return 1; | |
} | |
while(1) { | |
int s = tcp_accept(ls, NULL, -1); | |
assert(s >= 0); | |
s = http_start(s); | |
/* recv req */ | |
char cmd[16]; | |
char url[16]; | |
rc = http_recvrequest(s, cmd, sizeof(cmd), url, sizeof(url), -1); | |
assert(rc == 0); | |
printf("\n\n\nNEW REQUEST\n%s: %s\n", cmd, url); | |
recvf ( s ); | |
rc = http_sendstatus(s, 200, "OK", -1); | |
assert(rc == 0); | |
rc = http_sendfield(s, "hello", "world", -1); | |
rc = http_sendfield(s, "one", "two", -1); | |
rc = http_sendfield(s, "three", "four", -1); | |
//rc = http_sendfield(s, "Content-Type", "text/html", -1); | |
//rc = http_sendfield(s, "Content-Length", "44", -1); | |
//rc = http_sendfield(s, "/r", "", -1); | |
//rc = http_sendfield(s, "<html><body><h1>hi there.</h1></body></html>", "", -1); | |
rc = http_done(s, -1); | |
assert(rc == 0); | |
s = http_stop(s, -1); | |
rc = hclose(s); | |
printf("---\nlooks like it works\n"); | |
// just do two requests for now | |
if (requests++) { | |
return 0; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# make a sandbox | |
mkdir dsock_stuff && cd dsock_stuff | |
# c compiler hints and prep for the current sandbox shell | |
export opt=`pwd`/opt | |
export {CFLAGS=-I$opt/include,LDFLAGS=-L$opt/lib} | |
# build the libs | |
git clone --depth 1 https://github.com/sustrik/libdill && cd libdill | |
./autogen.sh && ./configure --prefix=$opt && make && make install | |
git clone --depth 1 https://github.com/sustrik/dsock && cd dsock | |
./autogen.sh && ./configure --disable-shared --prefix=$opt && make && make install | |
cd .. | |
# play | |
cc main.c $CFLAGS $LDFLAGS -ldill $opt/lib/libdsock.a -o server && ./server |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment