Ejemplo n.º 1
0
void
hio_response_http_set_header(HioResponseHttp *http,
                             HrtBuffer       *name,
                             HrtBuffer       *value)
{
    g_return_if_fail(hrt_buffer_is_locked(name));
    g_return_if_fail(hrt_buffer_is_locked(value));

    g_mutex_lock(http->headers_lock);

    if (http->headers_sent) {
        g_mutex_unlock(http->headers_lock);
        hrt_message("Attempt to set http header after we already sent the headers, ignoring");
        return;
    }

    /* FIXME overwrite a previous duplicate header if any */
    http->headers = g_slist_prepend(http->headers,
                                    header_new(name, value));
    g_mutex_unlock(http->headers_lock);
}
Ejemplo n.º 2
0
static gboolean
on_new_connections(GIOChannel   *source,
                   GIOCondition  condition,
                   gpointer      data)
{
    HioServer *hio_server = HIO_SERVER(data);
    struct sockaddr addr;
    socklen_t addrlen;
    int client_fd;
    gboolean accepted;

    if (!(condition & G_IO_IN))
        return TRUE;

    addrlen = sizeof(addr);

    /* accept as many things as we can in a loop, this reduces
     * main loop iterations
     */
    while (TRUE) {
        int listen_fd = g_atomic_int_get(&hio_server->fd);
        if (listen_fd < 0) {
            /* socket was closed */
            break;
        }

        client_fd = accept4(listen_fd,
                            &addr, &addrlen, SOCK_CLOEXEC | SOCK_NONBLOCK);

        if (client_fd < 0) {
            if (errno == EINTR) {
                continue;
            } else if (errno == EWOULDBLOCK || errno == EAGAIN) {
                /* nothing else to accept */
                break;
            } else if (errno == EBADF) {
                /* we closed the socket */
                hrt_debug("socket was invalid when we called accept4()");
                break;
            } else {
                hrt_message("accept4(): %s", strerror(errno));
                break;
            }
        }

        hrt_debug("accepted new socket %d", client_fd);

        accepted = FALSE;
        g_signal_emit(G_OBJECT(hio_server),
                      signals[SOCKET_ACCEPTED],
                      0, client_fd, &accepted);
        /* If nobody else has returned true, we just close the new connection. */
        if (!accepted) {
            shutdown(client_fd, SHUT_RDWR);
            close(client_fd);
            hrt_debug("nobody wanted new socket %d so we closed it", client_fd);
        }
    }

    return TRUE;
}