kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

nohttp.c (3161B)



      1 /*
      2  * Copyright (c) 2015 Joris Vink <joris@coders.se>
      3  *
      4  * Permission to use, copy, modify, and distribute this software for any
      5  * purpose with or without fee is hereby granted, provided that the above
      6  * copyright notice and this permission notice appear in all copies.
      7  *
      8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15  */
     16 
     17 /*
     18  * Example of using Kore as a network application server.
     19  *
     20  * We will get called for every new connection that has been established.
     21  * For TLS connections we will get called after the TLS handshake completed.
     22  *
     23  * From the setup we can queue up our own read commands and do whatever we
     24  * like with the newly connected client.
     25  */
     26 
     27 #include <kore/kore.h>
     28 
     29 void		connection_setup(struct connection *);
     30 int		connection_handle(struct connection *);
     31 int		connection_recv_data(struct netbuf *);
     32 
     33 void
     34 connection_setup(struct connection *c)
     35 {
     36 	kore_log(LOG_NOTICE, "%p: new connection", c);
     37 
     38 	/*
     39 	 * Setup a read command that will read up to 128 bytes and will
     40 	 * always call the callback connection_recv_data even if not all
     41 	 * 128 bytes were read.
     42 	 */
     43 	net_recv_queue(c, 128, NETBUF_CALL_CB_ALWAYS, connection_recv_data);
     44 
     45 	/* We are responsible for setting the connection state. */
     46 	c->state = CONN_STATE_ESTABLISHED;
     47 
     48 	/* Override the handle function, called when new events occur. */
     49 	c->handle = connection_handle;
     50 }
     51 
     52 /*
     53  * This function is called every time a new event is triggered on the
     54  * connection. In this demo we just use it as a stub for the normal
     55  * callback kore_connection_handle().
     56  *
     57  * In this callback you would generally look at the state of the connection
     58  * in c->state and perform the required actions like writing / reading using
     59  * net_send_flush() or net_recv_flush() if KORE_EVENT_WRITE or
     60  * KORE_EVENT_READ are set respectively in c->evt.flags.
     61  * Returning KORE_RESULT_ERROR from this callback will disconnect the
     62  * connection altogether.
     63  */
     64 int
     65 connection_handle(struct connection *c)
     66 {
     67 	kore_log(LOG_NOTICE, "connection_handle: %p", c);
     68 	return (kore_connection_handle(c));
     69 }
     70 
     71 /*
     72  * This function is called every time we get up to 128 bytes of data.
     73  * The connection can be found under nb->owner.
     74  * The data received can be found under nb->buf.
     75  * The length of the received data can be found under s_off.
     76  */
     77 int
     78 connection_recv_data(struct netbuf *nb)
     79 {
     80 	struct connection	*c = (struct connection *)nb->owner;
     81 
     82 	kore_log(LOG_NOTICE, "%p: received %zu bytes", (void *)c, nb->s_off);
     83 
     84 	/* We will just dump these back to the client. */
     85 	net_send_queue(c, nb->buf, nb->s_off);
     86 	net_send_flush(c);
     87 
     88 	/* Now reset the receive command for the next one. */
     89 	net_recv_reset(c, 128, connection_recv_data);
     90 
     91 	return (KORE_RESULT_OK);
     92 }