In order to be compatible with different editor, convert all Tab to spaces

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
main
Jianhui Zhao 2017-11-13 20:51:42 +08:00
parent 8eb57bef1e
commit ee31e3f1fa
11 changed files with 747 additions and 747 deletions

View File

@ -4,67 +4,67 @@
static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
{
printf("Got signal: %d\n", w->signum);
ev_break(loop, EVBREAK_ALL);
printf("Got signal: %d\n", w->signum);
ev_break(loop, EVBREAK_ALL);
}
void route_test(struct uh_connection *con)
{
struct uh_value *url = uh_get_url(con);
struct uh_value *header_host = uh_get_header(con, "Host");
struct uh_value *header_ua = uh_get_header(con, "User-Agent");
uh_send_head(con, UH_STATUS_OK, -1, NULL);
uh_printf_chunk(con, "<h1>Hello World</h1>");
uh_printf_chunk(con, "<h1>Libuhttp v%s</h1>", uh_version());
uh_printf_chunk(con, "<h1>Url: %.*s</h1>", (int)url->len, url->at);
struct uh_value *url = uh_get_url(con);
struct uh_value *header_host = uh_get_header(con, "Host");
struct uh_value *header_ua = uh_get_header(con, "User-Agent");
if (header_host)
uh_printf_chunk(con, "<h1>Host: %.*s</h1>", (int)header_host->len, header_host->at);
uh_send_head(con, UH_STATUS_OK, -1, NULL);
uh_printf_chunk(con, "<h1>Hello World</h1>");
uh_printf_chunk(con, "<h1>Libuhttp v%s</h1>", uh_version());
uh_printf_chunk(con, "<h1>Url: %.*s</h1>", (int)url->len, url->at);
if (header_ua)
uh_printf_chunk(con, "<h1>User-Agent: %.*s</h1>", (int)header_ua->len, header_ua->at);
uh_send_chunk(con, NULL, 0);
if (header_host)
uh_printf_chunk(con, "<h1>Host: %.*s</h1>", (int)header_host->len, header_host->at);
if (header_ua)
uh_printf_chunk(con, "<h1>User-Agent: %.*s</h1>", (int)header_ua->len, header_ua->at);
uh_send_chunk(con, NULL, 0);
}
int main(int argc, char **argv)
{
struct ev_loop *loop = EV_DEFAULT;
ev_signal *sig_watcher = NULL;
struct uh_server *srv = NULL;
struct ev_loop *loop = EV_DEFAULT;
ev_signal *sig_watcher = NULL;
struct uh_server *srv = NULL;
uh_log_info("libuhttp version: %s\n", uh_version());
uh_log_info("libuhttp version: %s\n", uh_version());
sig_watcher = calloc(1, sizeof(ev_signal));
if (!sig_watcher)
return -1;
ev_signal_init(sig_watcher, signal_cb, SIGINT);
ev_signal_start(loop, sig_watcher);
sig_watcher = calloc(1, sizeof(ev_signal));
if (!sig_watcher)
return -1;
ev_signal_init(sig_watcher, signal_cb, SIGINT);
ev_signal_start(loop, sig_watcher);
srv = uh_server_new(loop, "0.0.0.0", 8000);
if (!srv) {
uh_log_err("uh_server_new failed\n");
goto err;
}
srv = uh_server_new(loop, "0.0.0.0", 8000);
if (!srv) {
uh_log_err("uh_server_new failed\n");
goto err;
}
#if (UHTTP_SSL_ENABLED)
if (uh_ssl_init(srv, "server-cert.pem", "server-key.pem") < 0)
goto err;
if (uh_ssl_init(srv, "server-cert.pem", "server-key.pem") < 0)
goto err;
#endif
uh_register_route(srv, "/test", route_test);
uh_log_info("Listen on 8000...\n");
ev_run(loop, 0);
uh_register_route(srv, "/test", route_test);
uh_log_info("Listen on 8000...\n");
ev_run(loop, 0);
err:
free(sig_watcher);
uh_server_free(srv);
return 0;
free(sig_watcher);
uh_server_free(srv);
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@ -8,73 +8,73 @@
/* HTTP Status Codes */
enum uh_status {
UH_STATUS_CONTINUE = 100,
UH_STATUS_SWITCHING_PROTOCOLS = 101,
UH_STATUS_PROCESSING = 102,
UH_STATUS_OK = 200,
UH_STATUS_CREATED = 201,
UH_STATUS_ACCEPTED = 202,
UH_STATUS_NON_AUTHORITATIVE_INFORMATION = 203,
UH_STATUS_NO_CONTENT = 204,
UH_STATUS_RESET_CONTENT = 205,
UH_STATUS_PARTIAL_CONTENT = 206,
UH_STATUS_MULTI_STATUS = 207,
UH_STATUS_ALREADY_REPORTED = 208,
UH_STATUS_IM_USED = 226,
UH_STATUS_MULTIPLE_CHOICES = 300,
UH_STATUS_MOVED_PERMANENTLY = 301,
UH_STATUS_FOUND = 302,
UH_STATUS_SEE_OTHER = 303,
UH_STATUS_NOT_MODIFIED = 304,
UH_STATUS_USE_PROXY = 305,
UH_STATUS_TEMPORARY_REDIRECT = 307,
UH_STATUS_PERMANENT_REDIRECT = 308,
UH_STATUS_BAD_REQUEST = 400,
UH_STATUS_UNAUTHORIZED = 401,
UH_STATUS_PAYMENT_REQUIRED = 402,
UH_STATUS_FORBIDDEN = 403,
UH_STATUS_NOT_FOUND = 404,
UH_STATUS_METHOD_NOT_ALLOWED = 405,
UH_STATUS_NOT_ACCEPTABLE = 406,
UH_STATUS_PROXY_AUTHENTICATION_REQUIRED = 407,
UH_STATUS_REQUEST_TIMEOUT = 408,
UH_STATUS_CONFLICT = 409,
UH_STATUS_GONE = 410,
UH_STATUS_LENGTH_REQUIRED = 411,
UH_STATUS_PRECONDITION_FAILED = 412,
UH_STATUS_PAYLOAD_TOO_LARGE = 413,
UH_STATUS_URI_TOO_LONG = 414,
UH_STATUS_UNSUPPORTED_MEDIA_TYPE = 415,
UH_STATUS_RANGE_NOT_SATISFIABLE = 416,
UH_STATUS_EXPECTATION_FAILED = 417,
UH_STATUS_MISDIRECTED_REQUEST = 421,
UH_STATUS_UNPROCESSABLE_ENTITY = 422,
UH_STATUS_LOCKED = 423,
UH_STATUS_FAILED_DEPENDENCY = 424,
UH_STATUS_UPGRADE_REQUIRED = 426,
UH_STATUS_PRECONDITION_REQUIRED = 428,
UH_STATUS_TOO_MANY_REQUESTS = 429,
UH_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
UH_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS = 451,
UH_STATUS_INTERNAL_SERVER_ERROR = 500,
UH_STATUS_NOT_IMPLEMENTED = 501,
UH_STATUS_BAD_GATEWAY = 502,
UH_STATUS_SERVICE_UNAVAILABLE = 503,
UH_STATUS_GATEWAY_TIMEOUT = 504,
UH_STATUS_HTTP_VERSION_NOT_SUPPORTED = 505,
UH_STATUS_VARIANT_ALSO_NEGOTIATES = 506,
UH_STATUS_INSUFFICIENT_STORAGE = 507,
UH_STATUS_LOOP_DETECTED = 508,
UH_STATUS_NOT_EXTENDED = 510,
UH_STATUS_NETWORK_AUTHENTICATION_REQUIRED = 511
UH_STATUS_CONTINUE = 100,
UH_STATUS_SWITCHING_PROTOCOLS = 101,
UH_STATUS_PROCESSING = 102,
UH_STATUS_OK = 200,
UH_STATUS_CREATED = 201,
UH_STATUS_ACCEPTED = 202,
UH_STATUS_NON_AUTHORITATIVE_INFORMATION = 203,
UH_STATUS_NO_CONTENT = 204,
UH_STATUS_RESET_CONTENT = 205,
UH_STATUS_PARTIAL_CONTENT = 206,
UH_STATUS_MULTI_STATUS = 207,
UH_STATUS_ALREADY_REPORTED = 208,
UH_STATUS_IM_USED = 226,
UH_STATUS_MULTIPLE_CHOICES = 300,
UH_STATUS_MOVED_PERMANENTLY = 301,
UH_STATUS_FOUND = 302,
UH_STATUS_SEE_OTHER = 303,
UH_STATUS_NOT_MODIFIED = 304,
UH_STATUS_USE_PROXY = 305,
UH_STATUS_TEMPORARY_REDIRECT = 307,
UH_STATUS_PERMANENT_REDIRECT = 308,
UH_STATUS_BAD_REQUEST = 400,
UH_STATUS_UNAUTHORIZED = 401,
UH_STATUS_PAYMENT_REQUIRED = 402,
UH_STATUS_FORBIDDEN = 403,
UH_STATUS_NOT_FOUND = 404,
UH_STATUS_METHOD_NOT_ALLOWED = 405,
UH_STATUS_NOT_ACCEPTABLE = 406,
UH_STATUS_PROXY_AUTHENTICATION_REQUIRED = 407,
UH_STATUS_REQUEST_TIMEOUT = 408,
UH_STATUS_CONFLICT = 409,
UH_STATUS_GONE = 410,
UH_STATUS_LENGTH_REQUIRED = 411,
UH_STATUS_PRECONDITION_FAILED = 412,
UH_STATUS_PAYLOAD_TOO_LARGE = 413,
UH_STATUS_URI_TOO_LONG = 414,
UH_STATUS_UNSUPPORTED_MEDIA_TYPE = 415,
UH_STATUS_RANGE_NOT_SATISFIABLE = 416,
UH_STATUS_EXPECTATION_FAILED = 417,
UH_STATUS_MISDIRECTED_REQUEST = 421,
UH_STATUS_UNPROCESSABLE_ENTITY = 422,
UH_STATUS_LOCKED = 423,
UH_STATUS_FAILED_DEPENDENCY = 424,
UH_STATUS_UPGRADE_REQUIRED = 426,
UH_STATUS_PRECONDITION_REQUIRED = 428,
UH_STATUS_TOO_MANY_REQUESTS = 429,
UH_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
UH_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS = 451,
UH_STATUS_INTERNAL_SERVER_ERROR = 500,
UH_STATUS_NOT_IMPLEMENTED = 501,
UH_STATUS_BAD_GATEWAY = 502,
UH_STATUS_SERVICE_UNAVAILABLE = 503,
UH_STATUS_GATEWAY_TIMEOUT = 504,
UH_STATUS_HTTP_VERSION_NOT_SUPPORTED = 505,
UH_STATUS_VARIANT_ALSO_NEGOTIATES = 506,
UH_STATUS_INSUFFICIENT_STORAGE = 507,
UH_STATUS_LOOP_DETECTED = 508,
UH_STATUS_NOT_EXTENDED = 510,
UH_STATUS_NETWORK_AUTHENTICATION_REQUIRED = 511
};
struct uh_server;
struct uh_connection;
struct uh_value {
const char *at;
size_t len;
const char *at;
size_t len;
};
typedef void (*uh_route_handler_t)(struct uh_connection *con);
@ -131,9 +131,9 @@ void uh_redirect(struct uh_connection *con, int code, const char *location);
* to tell the client that everything was sent.
*
* Example:
* char data[] = "Hello World";
* uh_send_chunk(con, data, strlen(data));
* uh_send_chunk(con, NULL, 0); // Tell the client we're finished
* char data[] = "Hello World";
* uh_send_chunk(con, data, strlen(data));
* uh_send_chunk(con, NULL, 0); // Tell the client we're finished
*/
int uh_send_chunk(struct uh_connection *con, const char *buf, int len);

View File

@ -5,64 +5,64 @@
int uh_buf_init(struct uh_buf *buf, size_t initial_size)
{
buf->len = buf->size = 0;
buf->len = buf->size = 0;
if (buf->base) {
free(buf->base);
buf->base = NULL;
}
if (buf->base) {
free(buf->base);
buf->base = NULL;
}
if (initial_size > 0) {
buf->base = malloc(initial_size);
if (!buf->base)
return -1;
buf->size = initial_size;
}
if (initial_size > 0) {
buf->base = malloc(initial_size);
if (!buf->base)
return -1;
buf->size = initial_size;
}
return 0;
return 0;
}
int uh_buf_grow(struct uh_buf *buf, size_t size)
{
void *base = realloc(buf->base, buf->size + size);
if (!base)
return -1;
buf->base = base;
buf->size += size;
void *base = realloc(buf->base, buf->size + size);
if (!base)
return -1;
buf->base = base;
buf->size += size;
uh_log_debug("uh_buf_grow:%p +%d", buf, size);
return 0;
uh_log_debug("uh_buf_grow:%p +%d", buf, size);
return 0;
}
void uh_buf_free(struct uh_buf *buf)
{
uh_buf_init(buf, 0);
uh_buf_init(buf, 0);
}
size_t uh_buf_append(struct uh_buf *buf, const void *data, size_t len)
{
assert(buf);
assert(buf);
if (!data)
return 0;
if (!data)
return 0;
if (buf->len + len > buf->size) {
if (uh_buf_grow(buf, len * UH_BUF_SIZE_MULTIPLIER) == -1)
len = buf->size - buf->len;
}
if (buf->len + len > buf->size) {
if (uh_buf_grow(buf, len * UH_BUF_SIZE_MULTIPLIER) == -1)
len = buf->size - buf->len;
}
memcpy(buf->base + buf->len, data, len);
buf->len += len;
memcpy(buf->base + buf->len, data, len);
buf->len += len;
return len;
return len;
}
void uh_buf_remove(struct uh_buf *buf, size_t n)
{
if (n > 0 && n <= buf->len) {
memmove(buf->base, buf->base + n, buf->len - n);
buf->len -= n;
}
if (n > 0 && n <= buf->len) {
memmove(buf->base, buf->base + n, buf->len - n);
buf->len -= n;
}
}

View File

@ -8,9 +8,9 @@
#define UH_BUF_SIZE_MULTIPLIER 1.5
struct uh_buf {
char *base; /* Buffer pointer */
size_t len; /* Data length */
size_t size; /* Buffer size */
char *base; /* Buffer pointer */
size_t len; /* Data length */
size_t size; /* Buffer size */
};
#define uh_buf_available(b) ((b)->size - (b)->len)

View File

@ -1,9 +1,9 @@
#ifndef _UHTTP_CONFIG_H
#define _UHTTP_CONFIG_H
#define UHTTP_VERSION_MAJOR @UHTTP_VERSION_MAJOR@
#define UHTTP_VERSION_MINOR @UHTTP_VERSION_MINOR@
#define UHTTP_VERSION_STRING "@UHTTP_VERSION_MAJOR@.@UHTTP_VERSION_MINOR@"
#define UHTTP_VERSION_MAJOR @UHTTP_VERSION_MAJOR@
#define UHTTP_VERSION_MINOR @UHTTP_VERSION_MINOR@
#define UHTTP_VERSION_STRING "@UHTTP_VERSION_MAJOR@.@UHTTP_VERSION_MINOR@"
#define UHTTP_DEBUG @UHTTP_DEBUG_CONFIG@

View File

@ -10,65 +10,65 @@
#define UH_CONNECTION_TIMEOUT 30
#define UH_MAX_HTTP_HEAD_SIZE 1024
#define UH_MAX_HTTP_BODY_SIZE (2 * 1024 * 1024)
#define UH_MAX_HTTP_HEADERS 20
#define UH_MAX_HTTP_HEADERS 20
#define UH_CON_CLOSE (1 << 0)
#define UH_CON_SSL_HANDSHAKE_DONE (1 << 1) /* SSL hanshake has completed */
#define UH_CON_PARSERING (1 << 2) /* Whether executed http_parser_execute() */
#define UH_CON_CLOSE (1 << 0)
#define UH_CON_SSL_HANDSHAKE_DONE (1 << 1) /* SSL hanshake has completed */
#define UH_CON_PARSERING (1 << 2) /* Whether executed http_parser_execute() */
#define likely(x) (__builtin_expect(!!(x), 1))
#define unlikely(x) (__builtin_expect(!!(x), 0))
#define likely(x) (__builtin_expect(!!(x), 1))
#define unlikely(x) (__builtin_expect(!!(x), 0))
#define ev_timer_mode(l,w,after,repeat) do { \
ev_timer_stop(l, w); \
ev_timer_init(w, ev_cb(w), after, repeat); \
ev_timer_start(l, w); \
} while (0)
ev_timer_stop(l, w); \
ev_timer_init(w, ev_cb(w), after, repeat); \
ev_timer_start(l, w); \
} while (0)
struct uh_route {
char *path;
uh_route_handler_t cb;
struct list_head list;
char *path;
uh_route_handler_t cb;
struct list_head list;
};
struct uh_server {
int sock;
#if (UHTTP_SSL_ENABLED)
void *ssl_ctx;
int sock;
#if (UHTTP_SSL_ENABLED)
void *ssl_ctx;
#endif
ev_io read_watcher;
struct ev_loop *loop;
struct list_head routes;
struct list_head connections;
ev_io read_watcher;
struct ev_loop *loop;
struct list_head routes;
struct list_head connections;
};
struct uh_header {
struct uh_value field;
struct uh_value value;
struct uh_value field;
struct uh_value value;
};
struct uh_request {
struct uh_value url;
struct uh_value body;
int header_num;
struct uh_header header[UH_MAX_HTTP_HEADERS];
struct uh_value url;
struct uh_value body;
int header_num;
struct uh_header header[UH_MAX_HTTP_HEADERS];
};
struct uh_connection {
int sock;
#if (UHTTP_SSL_ENABLED)
void *ssl;
struct uh_connection {
int sock;
#if (UHTTP_SSL_ENABLED)
void *ssl;
#endif
unsigned char flags;
struct uh_buf read_buf;
struct uh_buf write_buf;
ev_io read_watcher;
ev_io write_watcher;
ev_timer timer_watcher;
struct uh_request req;
http_parser parser;
struct list_head list;
struct uh_server *srv;
unsigned char flags;
struct uh_buf read_buf;
struct uh_buf write_buf;
ev_io read_watcher;
ev_io write_watcher;
ev_timer timer_watcher;
struct uh_request req;
http_parser parser;
struct list_head list;
struct uh_server *srv;
};
#endif

View File

@ -2,29 +2,29 @@
void __uh_log(const char *filename, int line, int priority, const char *format, ...)
{
va_list ap;
static char buf[128];
va_list ap;
static char buf[128];
snprintf(buf, sizeof(buf), "(%s:%d) ", filename, line);
va_start(ap, format);
vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), format, ap);
va_end(ap);
snprintf(buf, sizeof(buf), "(%s:%d) ", filename, line);
va_start(ap, format);
vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), format, ap);
va_end(ap);
if (priority == LOG_ERR && errno > 0) {
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ":%s", strerror(errno));
errno = 0;
}
syslog(priority, "%s", buf);
if (priority == LOG_ERR && errno > 0) {
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ":%s", strerror(errno));
errno = 0;
}
syslog(priority, "%s", buf);
#if (UHTTP_DEBUG)
fprintf(stderr, "%s\n", buf);
fprintf(stderr, "%s\n", buf);
#else
if (priority == LOG_ERR)
fprintf(stderr, "%s\n", buf);
if (priority == LOG_ERR)
fprintf(stderr, "%s\n", buf);
#endif
}

View File

@ -16,13 +16,13 @@
#define uh_log(priority, format...) __uh_log(__FILENAME__, __LINE__, priority, format)
#if (UHTTP_DEBUG)
#define uh_log_debug(format...) uh_log(LOG_DEBUG, format)
#define uh_log_debug(format...) uh_log(LOG_DEBUG, format)
#else
#define uh_log_debug(format...)
#endif
#define uh_log_info(format...) uh_log(LOG_INFO, format)
#define uh_log_err(format...) uh_log(LOG_ERR, format)
#define uh_log_info(format...) uh_log(LOG_INFO, format)
#define uh_log_err(format...) uh_log(LOG_ERR, format)
void __uh_log(const char *filename, int line, int priority, const char *format, ...);

View File

@ -5,196 +5,196 @@
#if (UHTTP_SSL_ENABLED)
int uh_ssl_init(struct uh_server *srv, const char *cert, const char *key)
{
SSL_CTX *ctx = NULL;
SSL_CTX *ctx = NULL;
SSL_library_init();
SSL_library_init();
/* registers the error strings for all libssl functions */
SSL_load_error_strings();
/* creates a new SSL_CTX object */
ctx = SSL_CTX_new(SSLv23_server_method());
if (!ctx) {
uh_log_err("Failed to create SSL context");
return -1;
}
/* registers the error strings for all libssl functions */
SSL_load_error_strings();
/* creates a new SSL_CTX object */
ctx = SSL_CTX_new(SSLv23_server_method());
if (!ctx) {
uh_log_err("Failed to create SSL context");
return -1;
}
/* loads the first certificate stored in file into ctx */
if (SSL_CTX_use_certificate_file(ctx, cert, SSL_FILETYPE_PEM) != SSL_SUCCESS) {
uh_log_err("OpenSSL Error: loading certificate file failed");
goto err;
}
/*
* adds the first private RSA key found in file to ctx.
*
* checks the consistency of a private key with the corresponding
* certificate loaded into ctx. If more than one key/certificate
* pair (RSA/DSA) is installed, the last item installed will be checked.
*/
if (SSL_CTX_use_RSAPrivateKey_file(ctx, key, SSL_FILETYPE_PEM) != SSL_SUCCESS) {
uh_log_err("OpenSSL Error: loading key failed");
goto err;
}
/* loads the first certificate stored in file into ctx */
if (SSL_CTX_use_certificate_file(ctx, cert, SSL_FILETYPE_PEM) != SSL_SUCCESS) {
uh_log_err("OpenSSL Error: loading certificate file failed");
goto err;
}
/*
* adds the first private RSA key found in file to ctx.
*
* checks the consistency of a private key with the corresponding
* certificate loaded into ctx. If more than one key/certificate
* pair (RSA/DSA) is installed, the last item installed will be checked.
*/
if (SSL_CTX_use_RSAPrivateKey_file(ctx, key, SSL_FILETYPE_PEM) != SSL_SUCCESS) {
uh_log_err("OpenSSL Error: loading key failed");
goto err;
}
srv->ssl_ctx = ctx;
return 0;
srv->ssl_ctx = ctx;
return 0;
err:
SSL_CTX_free(ctx);
return -1;
SSL_CTX_free(ctx);
return -1;
}
#endif
void uh_ssl_ctx_free(struct uh_server *srv)
{
#if (UHTTP_SSL_ENABLED)
if (!srv->ssl_ctx)
return;
SSL_CTX_free(srv->ssl_ctx);
if (!srv->ssl_ctx)
return;
SSL_CTX_free(srv->ssl_ctx);
#endif
}
void uh_ssl_free(struct uh_connection *con)
{
#if (UHTTP_SSL_ENABLED)
if (!con->ssl)
return;
SSL_shutdown(con->ssl);
SSL_free(con->ssl);
if (!con->ssl)
return;
SSL_shutdown(con->ssl);
SSL_free(con->ssl);
#endif
}
#if (UHTTP_SSL_ENABLED)
static int uh_ssl_err(struct uh_connection *con, int ret, const char *fun)
{
int err;
err = SSL_get_error(con->ssl, ret);
if (err == SSL_ERROR_ZERO_RETURN || ERR_peek_error()) {
con->flags |= UH_CON_CLOSE;
return 0;
}
int err;
err = SSL_get_error(con->ssl, ret);
if (err == SSL_ERROR_ZERO_RETURN || ERR_peek_error()) {
con->flags |= UH_CON_CLOSE;
return 0;
}
#if (UHTTP_USE_OPENSSL)
if (ret == 0) {
con->flags |= UH_CON_CLOSE;
return 0;
}
if (ret == 0) {
con->flags |= UH_CON_CLOSE;
return 0;
}
#endif
if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
return -1;
if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
return -1;
if (err == SSL_ERROR_SYSCALL) {
if (errno > 0)
uh_log_err("%s", fun);
con->flags |= UH_CON_CLOSE;
return -1;
}
if (err == SSL_ERROR_SYSCALL) {
if (errno > 0)
uh_log_err("%s", fun);
con->flags |= UH_CON_CLOSE;
return -1;
}
con->flags |= UH_CON_CLOSE;
uh_log_err("%s() Error: %s", fun, ERR_reason_error_string(err));
return -1;
con->flags |= UH_CON_CLOSE;
uh_log_err("%s() Error: %s", fun, ERR_reason_error_string(err));
return -1;
}
#endif
int uh_ssl_read(struct uh_connection *con, void *buf, int count)
{
int ret = -1;
int ret = -1;
#if (UHTTP_SSL_ENABLED)
if (!con->ssl)
goto no_ssl;
if (!con->ssl)
goto no_ssl;
ret = SSL_read(con->ssl, buf, count);
if (ret > 0)
return ret;
ret = SSL_read(con->ssl, buf, count);
if (ret > 0)
return ret;
return uh_ssl_err(con, ret, "SSL_read");
return uh_ssl_err(con, ret, "SSL_read");
no_ssl:
#endif
ret = read(con->sock, buf, count);
if (ret <= 0) {
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
return ret;
if (ret != 0) {
con->flags |= UH_CON_CLOSE;
uh_log_err("read");
}
}
return ret;
ret = read(con->sock, buf, count);
if (ret <= 0) {
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
return ret;
if (ret != 0) {
con->flags |= UH_CON_CLOSE;
uh_log_err("read");
}
}
return ret;
}
int uh_ssl_write(struct uh_connection *con, void *buf, int count)
{
int ret = -1;
int ret = -1;
#if (UHTTP_SSL_ENABLED)
if (!con->ssl)
goto no_ssl;
if (!con->ssl)
goto no_ssl;
ret = SSL_write(con->ssl, buf, count);
if (ret > 0)
return ret;
ret = SSL_write(con->ssl, buf, count);
if (ret > 0)
return ret;
return uh_ssl_err(con, ret, "SSL_write");
return uh_ssl_err(con, ret, "SSL_write");
no_ssl:
#endif
ret = write(con->sock, buf, count);
if (ret <= 0) {
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
return ret;
if (ret != 0) {
con->flags |= UH_CON_CLOSE;
uh_log_err("write");
}
}
return ret;
ret = write(con->sock, buf, count);
if (ret <= 0) {
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
return ret;
if (ret != 0) {
con->flags |= UH_CON_CLOSE;
uh_log_err("write");
}
}
return ret;
}
int uh_ssl_accept(struct uh_connection *con)
{
int sock = -1;
struct uh_server *srv = con->srv;
int sock = -1;
struct uh_server *srv = con->srv;
sock = accept4(srv->sock, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC);
if (unlikely(sock < 0)) {
if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK)
uh_log_err("accept4");
return -1;
}
con->sock = sock;
sock = accept4(srv->sock, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC);
if (unlikely(sock < 0)) {
if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK)
uh_log_err("accept4");
return -1;
}
con->sock = sock;
#if (UHTTP_SSL_ENABLED)
if (!srv->ssl_ctx)
return sock;
if (!srv->ssl_ctx)
return sock;
con->ssl = SSL_new(srv->ssl_ctx);
if (!con->ssl)
return -1;
if (!SSL_set_fd(con->ssl, sock)) {
uh_log_err("SSL_set_fd() failed");
return -1;
}
SSL_set_accept_state(con->ssl);
con->ssl = SSL_new(srv->ssl_ctx);
if (!con->ssl)
return -1;
if (!SSL_set_fd(con->ssl, sock)) {
uh_log_err("SSL_set_fd() failed");
return -1;
}
SSL_set_accept_state(con->ssl);
#endif
return sock;
return sock;
}
void uh_ssl_handshake(struct uh_connection *con)
{
#if (UHTTP_SSL_ENABLED)
int ret = SSL_accept(con->ssl);
if (ret == 1) {
con->flags |= UH_CON_SSL_HANDSHAKE_DONE;
return;
}
int ret = SSL_accept(con->ssl);
if (ret == 1) {
con->flags |= UH_CON_SSL_HANDSHAKE_DONE;
return;
}
uh_ssl_err(con, ret, "SSL_accept");
uh_ssl_err(con, ret, "SSL_accept");
#endif
}

View File

@ -8,7 +8,7 @@
#include <openssl/err.h>
#ifndef SSL_SUCCESS
#define SSL_SUCCESS 1
#define SSL_SUCCESS 1
#endif
#elif (UHTTP_USE_CYASSL)