Skip to content

Instantly share code, notes, and snippets.

@methane
Last active June 25, 2017 16:58
Show Gist options
  • Save methane/2e35cf585a939c1dfa3e to your computer and use it in GitHub Desktop.
Save methane/2e35cf585a939c1dfa3e to your computer and use it in GitHub Desktop.
Hello, World! from h2o
/*
* Copyright (c) 2014 DeNA Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include "h2o.h"
#include "h2o/http1.h"
static void register_handler(h2o_hostconf_t *host_config, int (*on_req)(h2o_handler_t *, h2o_req_t *))
{
h2o_handler_t *handler = h2o_create_handler(host_config, sizeof(*handler));
handler->on_req = on_req;
}
static int hello(h2o_handler_t *self, h2o_req_t *req)
{
static h2o_generator_t generator = { NULL, NULL };
static h2o_iovec_t body = {H2O_STRLIT("Hello, World!\n")};
req->res.status = 200;
req->res.reason = "OK";
req->res.content_length = body.len;
h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, H2O_STRLIT("text/plain; charset=utf-8"));
h2o_start_response(req, &generator);
h2o_send(req, &body, 1, 1);
return 0;
}
static h2o_globalconf_t config;
static h2o_context_t ctx;
static void on_accept(uv_stream_t *listener, int status)
{
uv_tcp_t *conn;
h2o_socket_t *sock;
if (status != 0)
return;
conn = h2o_malloc(sizeof(*conn));
uv_tcp_init(listener->loop, conn);
if (uv_accept(listener, (uv_stream_t*)conn) != 0) {
uv_close((uv_handle_t*)conn, (uv_close_cb)free);
return;
}
sock = h2o_uv_socket_create((uv_stream_t*)conn, NULL, 0, (uv_close_cb)free);
h2o_http1_accept(&ctx, sock);
}
static int create_listener(void)
{
static uv_tcp_t listener;
struct sockaddr_in addr;
int r;
uv_tcp_init(ctx.loop, &listener);
uv_ip4_addr("127.0.0.1", 7890, &addr);
if ((r = uv_tcp_bind(&listener, (struct sockaddr*)&addr, 0)) != 0) {
fprintf(stderr, "uv_tcp_bind:%s\n", uv_strerror(r));
goto Error;
}
if ((r = uv_listen((uv_stream_t*)&listener, 128, on_accept)) != 0) {
fprintf(stderr, "uv_listen:%s\n", uv_strerror(r));
goto Error;
}
return 0;
Error:
uv_close((uv_handle_t*)&listener, NULL);
return r;
}
int main(int argc, char **argv)
{
h2o_hostconf_t *hostconf;
signal(SIGPIPE, SIG_IGN);
h2o_config_init(&config);
hostconf = h2o_config_register_host(&config, "default");
register_handler(hostconf, hello);
uv_loop_t loop;
uv_loop_init(&loop);
h2o_context_init(&ctx, &loop, &config);
/* disabled by default: uncomment the line below to enable access logging */
/* h2o_access_log_register(&config.default_host, "/dev/stdout", NULL); */
if (create_listener() != 0) {
fprintf(stderr, "failed to listen to 127.0.0.1:7890:%s\n", strerror(errno));
goto Error;
}
uv_run(ctx.loop, UV_RUN_DEFAULT);
Error:
return 1;
}
@hbowden
Copy link

hbowden commented Jun 25, 2017

No Longer compiles with h2o.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment