Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

OS X support

* abstract sendfile() differences
* memset-zero sockaddr_in before using
* build with -fcommon -- let the linker strip duplicate symbols

Closes #60, #59 and #19.
  • Loading branch information...
commit 29da440e9b526fbdc7f8266db0bc2a0f38551398 1 parent f957bb6
Patrick Lucas authored February 27, 2012 jonashaag committed February 27, 2012
2  Makefile
@@ -15,7 +15,7 @@ objects		= $(HTTP_PARSER_OBJ) \
15 15
 CPPFLAGS	+= $(PYTHON_INCLUDE) -I . -I $(SOURCE_DIR) -I $(HTTP_PARSER_DIR)
16 16
 CFLAGS		+= $(FEATURES) -std=c99 -fno-strict-aliasing -Wall -Wextra \
17 17
 		   -Wno-unused -g -O0 -fPIC
18  
-LDFLAGS		+= $(PYTHON_LDFLAGS) -l ev -shared
  18
+LDFLAGS		+= $(PYTHON_LDFLAGS) -l ev -shared -fcommon
19 19
 
20 20
 ifneq ($(WANT_SENDFILE), no)
21 21
 FEATURES	+= -D WANT_SENDFILE
29  bjoern/portable_sendfile.c
... ...
@@ -0,0 +1,29 @@
  1
+#include <stdlib.h>
  2
+
  3
+#define SENDFILE_CHUNK_SIZE 16*1024
  4
+
  5
+#ifdef __APPLE__
  6
+
  7
+  /* OS X */
  8
+
  9
+  #include <sys/socket.h>
  10
+  #include <sys/types.h>
  11
+
  12
+  ssize_t portable_sendfile(int out_fd, int in_fd) {
  13
+    off_t len = SENDFILE_CHUNK_SIZE;
  14
+    if(sendfile(in_fd, out_fd, 0, &len, NULL, 0) == -1)
  15
+      return -1;
  16
+    return len;
  17
+  }
  18
+
  19
+#else
  20
+
  21
+  /* Linux */
  22
+
  23
+  #include <sys/sendfile.h>
  24
+
  25
+  ssize_t portable_sendfile(int out_fd, int in_fd) {
  26
+    return sendfile(out_fd, in_fd, NULL, SENDFILE_CHUNK_SIZE);
  27
+  }
  28
+
  29
+#endif
1  bjoern/portable_sendfile.h
... ...
@@ -0,0 +1 @@
  1
+ssize_t portable_sendfile(int out_fd, int in_fd);
13  bjoern/server.c
@@ -6,8 +6,8 @@
6 6
 #ifdef WANT_SIGINT_HANDLING
7 7
 # include <sys/signal.h>
8 8
 #endif
9  
-#include <sys/sendfile.h>
10 9
 #include <ev.h>
  10
+#include "portable_sendfile.h"
11 11
 #include "common.h"
12 12
 #include "wsgi.h"
13 13
 #include "server.h"
@@ -104,6 +104,7 @@ bool server_init(const char* hostaddr, const int port)
104 104
       return false;
105 105
 
106 106
     struct sockaddr_in sockaddr;
  107
+    memset(&sockaddr, 0, sizeof(sockaddr));
107 108
     sockaddr.sin_family = PF_INET;
108 109
     inet_pton(AF_INET, hostaddr, &sockaddr.sin_addr);
109 110
     sockaddr.sin_port = htons(port);
@@ -302,7 +303,6 @@ ev_io_on_write(struct ev_loop* mainloop, ev_io* watcher, const int events)
302 303
 static bool
303 304
 send_chunk(Request* request)
304 305
 {
305  
-  Py_ssize_t chunk_length;
306 306
   Py_ssize_t bytes_sent;
307 307
 
308 308
   assert(request->current_chunk != NULL);
@@ -327,15 +327,12 @@ send_chunk(Request* request)
327 327
   return true;
328 328
 }
329 329
 
330  
-#define SENDFILE_CHUNK_SIZE 16*1024
331  
-
332 330
 static bool
333 331
 do_sendfile(Request* request)
334 332
 {
335  
-  Py_ssize_t bytes_sent = sendfile(
336  
-    request->client_fd,
337  
-    request->current_chunk_p, /* current_chunk_p stores the file fd */
338  
-    NULL, SENDFILE_CHUNK_SIZE
  333
+  Py_ssize_t bytes_sent = portable_sendfile(
  334
+      request->client_fd,
  335
+      request->current_chunk_p /* current_chunk_p stores the file fd */
339 336
   );
340 337
   if(bytes_sent == -1)
341 338
     return handle_nonzero_errno(request);
2  setup.py
@@ -14,7 +14,7 @@
14 14
                      ('WANT_SIGINT_HANDLING', '1')],
15 15
     extra_compile_args = ['-std=c99', '-fno-strict-aliasing', '-Wall',
16 16
                           '-Wextra', '-Wno-unused', '-g', '-fPIC',
17  
-                          '-Wno-missing-field-initializers']
  17
+                          '-Wno-missing-field-initializers', '-fcommon']
18 18
 )
19 19
 
20 20
 setup(

0 notes on commit 29da440

Please sign in to comment.
Something went wrong with that request. Please try again.