Ejemplo n.º 1
0
static void prepare_lisp_request (server *srv, handler_ctx *hctx)
{
  size_t i;
  buffer *buf;
  connection *con = hctx->connection;
  chunkqueue *hr_cq = hctx->request_queue;

  buf = chunkqueue_get_append_buffer(hr_cq);

#define APPEND_HEADER(k, vt, v)            \
  BUFFER_APPEND_STRING_CONST(buf, k);      \
  BUFFER_APPEND_STRING_CONST(buf, "\n");   \
  buffer_append_##vt(buf, v);              \
  BUFFER_APPEND_STRING_CONST(buf, "\n")

#define KEY_IS(string) \
  (buffer_caseless_compare(CONST_BUF_LEN(ds->key), CONST_STR_LEN(string)) == 0)

#if 0
  for (i = 0; i < srv->srv_sockets.used; i++) {
      log_error_write(srv, __FILE__, __LINE__, "sd<S>", 
                      "srv_sockets", i,
                      inet_ntop_cache_get_ip(srv, &(srv->srv_sockets.ptr[i]->addr)));
  }
#endif

  /* Mod_lisp configuration and connection info. */
  APPEND_HEADER("server-id", string_buffer, hctx->socket_data->id);
  APPEND_HEADER("server-baseversion", string, PACKAGE_STRING);
  APPEND_HEADER("modlisp-version", string, MOD_LISP_VERSION);
  /* Server/connection configuration info. */
  APPEND_HEADER("url", string_buffer, con->request.uri);
  APPEND_HEADER("method", string, get_http_method_name(con->request.http_method));
  APPEND_HEADER("script-filename", string_buffer, con->physical.path);
  APPEND_HEADER("server-protocol", string, get_http_version_name(con->request.http_version));
  APPEND_HEADER("remote-ip-port", long, get_remote_port(srv, con));
  APPEND_HEADER("server-ip-port", long, srv->srvconf.port);
  APPEND_HEADER("remote-ip-addr", string, inet_ntop_cache_get_ip(srv, &(con->dst_addr)));
  APPEND_HEADER("server-ip-addr", string, get_local_ip(srv, con));
  if (con->request.http_content_type) {
    APPEND_HEADER("content-type", string, con->request.http_content_type);
  }
  if (con->request.content_length) {
    APPEND_HEADER("content-length", long, con->request.content_length);
  }
Ejemplo n.º 2
0
static h2o_iovec_t build_request(struct st_h2o_http1client_t *client, h2o_iovec_t method, h2o_url_t url, h2o_iovec_t connection,
                                 h2o_header_t *headers, size_t num_headers)
{
    h2o_iovec_t buf;
    size_t offset = 0;

    buf.len = method.len + url.path.len + url.authority.len + 512;
    buf.base = h2o_mem_alloc_pool(client->super.pool, char, buf.len);

#define RESERVE(sz)                                                                                                                \
    do {                                                                                                                           \
        size_t required = offset + sz + 4 /* for "\r\n\r\n" */;                                                                    \
        if (required > buf.len) {                                                                                                  \
            do {                                                                                                                   \
                buf.len *= 2;                                                                                                      \
            } while (required > buf.len);                                                                                          \
            char *newp = h2o_mem_alloc_pool(client->super.pool, char, buf.len);                                                    \
            memcpy(newp, buf.base, offset);                                                                                        \
            buf.base = newp;                                                                                                       \
        }                                                                                                                          \
    } while (0)
#define APPEND(s, l)                                                                                                               \
    do {                                                                                                                           \
        memcpy(buf.base + offset, (s), (l));                                                                                       \
        offset += (l);                                                                                                             \
    } while (0)
#define APPEND_STRLIT(lit) APPEND((lit), sizeof(lit) - 1)
#define APPEND_HEADER(h)                                                                                                           \
    do {                                                                                                                           \
        RESERVE((h)->name->len + (h)->value.len + 4);                                                                              \
        APPEND((h)->orig_name ? (h)->orig_name : (h)->name->base, (h)->name->len);                                                 \
        buf.base[offset++] = ':';                                                                                                  \
        buf.base[offset++] = ' ';                                                                                                  \
        APPEND((h)->value.base, (h)->value.len);                                                                                   \
        buf.base[offset++] = '\r';                                                                                                 \
        buf.base[offset++] = '\n';                                                                                                 \
    } while (0)

    APPEND(method.base, method.len);
    buf.base[offset++] = ' ';
    APPEND(url.path.base, url.path.len);
    APPEND_STRLIT(" HTTP/1.1\r\nhost: ");
    APPEND(url.authority.base, url.authority.len);
    buf.base[offset++] = '\r';
    buf.base[offset++] = '\n';
    assert(offset <= buf.len);

    if (connection.base != NULL) {
        h2o_header_t h = (h2o_header_t){&H2O_TOKEN_CONNECTION->buf, NULL, connection};
        APPEND_HEADER(&h);
    }

    h2o_header_t *h, *h_end;
    for (h = (h2o_header_t *)headers, h_end = h + num_headers; h != h_end; ++h)
        APPEND_HEADER(h);

    APPEND_STRLIT("\r\n");

    /* set the length */
    assert(offset <= buf.len);
    buf.len = offset;

    return buf;

#undef RESERVE
#undef APPEND
#undef APPEND_STRLIT
}