Permalink
Browse files

Added unix socket support, plus a test file

  • Loading branch information...
1 parent f07b53b commit 59c44f2e1f7122337d69bae680c76c93e7a07823 @k3d3 k3d3 committed Dec 17, 2011
Showing with 65 additions and 16 deletions.
  1. +36 −15 bjoern/server.c
  2. +1 −1 http-parser
  3. +28 −0 tests/hello_unix.py
View
@@ -1,4 +1,5 @@
#include <sys/socket.h>
+#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
@@ -70,26 +71,46 @@ ev_signal_on_sigint(struct ev_loop* mainloop, ev_signal* watcher, const int even
bool server_init(const char* hostaddr, const int port)
{
- if((sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
- return false;
+ if(strncmp("unix:", hostaddr, 5) == 0) {
+ if((sockfd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
+ return false;
- struct sockaddr_in sockaddr;
- sockaddr.sin_family = PF_INET;
- inet_pton(AF_INET, hostaddr, &sockaddr.sin_addr);
- sockaddr.sin_port = htons(port);
+ struct sockaddr_un sockaddr;
+ sockaddr.sun_family = PF_UNIX;
+ strcpy(sockaddr.sun_path, hostaddr+5);
+ if(hostaddr[5] == '@') sockaddr.sun_path[0] = '\0'; /* Use @ for abstract */
- /* Set SO_REUSEADDR t make the IP address available for reuse */
- int optval = true;
- setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
+ if(bind(sockfd, (struct sockaddr*)&sockaddr, strlen(hostaddr+5)+2) < 0)
+ return false;
- if(bind(sockfd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) < 0)
- return false;
+ if(listen(sockfd, LISTEN_BACKLOG) < 0)
+ return false;
- if(listen(sockfd, LISTEN_BACKLOG) < 0)
- return false;
+ DBG("Listening on %s...", hostaddr);
+ return true;
+ }
+ else {
+ if((sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
+ return false;
- DBG("Listening on %s:%d...", hostaddr, port);
- return true;
+ struct sockaddr_in sockaddr;
+ sockaddr.sin_family = PF_INET;
+ inet_pton(AF_INET, hostaddr, &sockaddr.sin_addr);
+ sockaddr.sin_port = htons(port);
+
+ /* Set SO_REUSEADDR t make the IP address available for reuse */
+ int optval = true;
+ setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
+
+ if(bind(sockfd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) < 0)
+ return false;
+
+ if(listen(sockfd, LISTEN_BACKLOG) < 0)
+ return false;
+
+ DBG("Listening on %s:%d...", hostaddr, port);
+ return true;
+ }
}
static void
View
@@ -0,0 +1,28 @@
+import bjoern
+from random import choice
+
+def app1(env, sr):
+ sr('200 ok', [('Foo', 'Bar'), ('Blah', 'Blubb'), ('Spam', 'Eggs'), ('Blurg', 'asdasjdaskdasdjj asdk jaks / /a jaksdjkas jkasd jkasdj '),
+ ('asd2easdasdjaksdjdkskjkasdjka', 'oasdjkadk kasdk k k k k k ')])
+ return ['hello', 'world']
+
+def app2(env, sr):
+ sr('200 ok', [])
+ return 'hello'
+
+def app3(env, sr):
+ sr('200 abc', [('Content-Length', '12')])
+ yield 'Hello'
+ yield ' World'
+ yield '\n'
+
+def app4(e, s):
+ s('200 ok', [])
+ return ['hello\n']
+
+apps = (app1, app2, app3, app4)
+
+def wsgi_app(env, start_response):
+ return choice(apps)(env, start_response)
+
+bjoern.run(wsgi_app, 'unix:@hello_unix', 0)

0 comments on commit 59c44f2

Please sign in to comment.