Esempio n. 1
0
/*
 * `nread` (siz_w) is > 0 if there is data available, 0 if libuv is done reading for
 * now, or < 0 on error.
 *
 * The callee is responsible for closing the stream when an error happens
 * by calling uv_close(). Trying to read from the stream again is undefined.
 *
 * The callee is responsible for freeing the buffer, libuv does not reuse it.
 * The buffer may be a null buffer (where buf->base=NULL and buf->len=0) on
 * error.
 */
static void
_http_conn_read_cb(uv_stream_t* tcp_u,
                   ssize_t      siz_w,
                   const uv_buf_t *     buf_u)
{
  u3_hcon* hon_u = (u3_hcon*)(void*) tcp_u;

  u3_lo_open();
  {
    if ( siz_w == UV_EOF ) {
      _http_conn_dead(hon_u);      
    } else if ( siz_w < 0 ) {
      uL(fprintf(uH, "http: read: %s\n", uv_strerror(siz_w)));
      _http_conn_dead(hon_u);
    }
    else {
      if ( !hon_u->ruc_u ) {
        hon_u->ruc_u = _http_req_new(hon_u);
      }

      if ( siz_w != http_parser_execute(hon_u->ruc_u->par_u,
                                        &_http_settings,
                                        (c3_c*)buf_u->base,
                                        siz_w) )
      {
        uL(fprintf(uH, "http: parse error\n"));
        _http_conn_dead(hon_u);
      }
    }
    if ( buf_u->base ) {
      free(buf_u->base);
    }
  }
  u3_lo_shut(c3y);
}
Esempio n. 2
0
/* _http_conn_read_cb(): server read callback.
*/
static void
_http_conn_read_cb(uv_stream_t* tcp_u,
                   ssize_t      siz_i,
                   uv_buf_t     buf_u)
{
  u2_hcon* hon_u = (u2_hcon*)(void*) tcp_u;

  u2_lo_open();
  {
    if ( siz_i < 0 ) {
      uv_err_t las_u = uv_last_error(u2L);

      if ( UV_EOF != las_u.code ) {
        uL(fprintf(uH, "http: read: %s\n", uv_strerror(las_u)));
      }
      _http_conn_dead(hon_u);
    }
    else {
      if ( !hon_u->ruc_u ) {
        hon_u->ruc_u = _http_req_new(hon_u);
      }

      if ( siz_i != http_parser_execute(hon_u->ruc_u->par_u,
                                        &_http_settings,
                                        (c3_c*)buf_u.base,
                                        siz_i) )
      {
        uL(fprintf(uH, "http: parse error\n"));
        _http_conn_dead(hon_u);
      }
    }
    if ( buf_u.base ) {
      free(buf_u.base);
    }
  }
  u2_lo_shut(u2_yes);
}
Esempio n. 3
0
/* _http_respond_buf(): write back to http.
*/
static void
_http_respond_buf(u3_hreq* req_u, uv_buf_t buf_u)
{
  _u3_write_t* ruq_u;

  // don't respond to a dead connection
  if ( uv_is_closing((uv_handle_t*) &(req_u->hon_u->wax_u)) ) {
      free(buf_u.base);
      return;
  }

  ruq_u = (_u3_write_t*) c3_malloc(sizeof(_u3_write_t));

  ruq_u->buf_y = (c3_y*)buf_u.base;

  if ( 0 != uv_write(&ruq_u->wri_u,
                     (uv_stream_t*)&(req_u->hon_u->wax_u),
                     &buf_u, 1,
                     _http_write_cb) )
  {
    uL(fprintf(uH, "respond: ERROR\n"));
    _http_conn_dead(req_u->hon_u);
  }
}