Server 0.0.1
HTTP/REST server implementation

src/server/handle_accept.c

Go to the documentation of this file.
00001 
00023 #include <errno.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 
00027 #include <openssl/ssl.h>
00028 
00029 #include "http/worker.h"
00030 #include "server.h"
00031 #include "class.h"
00032 #include "logger.h"
00033 #include "stream.h"
00034 
00035 int
00036 serverHandleAccept(Server this, unsigned int i)
00037 {
00038         char   remoteAddr[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
00039         Sock   acc = NULL;
00040         Stream st;
00041 
00042         if (this->nfds >= this->max_fds) {
00043                 return -1;
00044         }
00045 
00046         acc = socketAccept((0 == i)? this->sock : this->sockSSL, &remoteAddr);
00047 
00048         if (-1 != acc->handle) {
00049         switch(i) {
00050                 case 0:
00051                         // no SSL
00052                         st = new(Stream, STREAM_FD, acc->handle);
00053                         break;
00054 
00055                 case 1:
00056                         // SSL
00057                         {
00058                                 SSL * ssl = SSL_new(this->ctx);
00059                                 SSL_set_fd(ssl, acc->handle);
00060                                 SSL_accept(ssl);
00061                                 st = new(Stream, STREAM_SSL, ssl);
00062                         }
00063                         break;
00064 
00065                 default:
00066                         break;
00067         }
00068 
00069                 // save the socket handle
00070                 (this->conns)[acc->handle].sock   = acc; 
00071 
00072                 // clone worker
00073                 (this->conns)[acc->handle].worker = clone(this->worker);
00074                 (this->conns)[acc->handle].stream = st;
00075 
00076                 (this->fds)[this->nfds].fd        = acc->handle;
00077                 (this->fds)[this->nfds].events    = POLLIN;
00078                 this->nfds++;
00079         } else {
00080                 delete(acc);
00081 
00082                 switch(errno) {
00083                         case EAGAIN:
00084                                 loggerLog(this->logger,
00085                                                 LOGGER_DEBUG,
00086                                                 "server accept blocks");
00087                                 break;
00088 
00089                         default:
00090                                 loggerLog(this->logger,
00091                                                 LOGGER_DEBUG,
00092                                                 "server accept error");
00093                                 break;
00094                 }
00095         }
00096 
00097         return (acc)? acc->handle : -1;
00098 }
00099 
00100 // vim: set ts=4 sw=4:
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines