Beispiel #1
0
gboolean moloch_http_write_cb(gint UNUSED(fd), GIOCondition UNUSED(cond), gpointer data) {
    MolochConn_t        *conn = data;
    GError              *gerror = 0;

    /*struct timeval startTime;
    struct timeval endTime;
    gettimeofday(&startTime, 0); */
    if (!conn->request)
        return FALSE;

    int sent = g_socket_send(conn->conn, conn->request->data+conn->sent, conn->request->data_len-conn->sent, NULL, &gerror);
    conn->sent += sent;

    /*gettimeofday(&endTime, 0);
    LOG("%s WRITE %d %d %ldms", conn->line, sent, conn->sent,
       (endTime.tv_sec - startTime.tv_sec)*1000 + (endTime.tv_usec/1000 - startTime.tv_usec/1000));*/


    if (gerror) {
        /* Should free stuff here */
        LOG("ERROR: %p: Receive Error: %s", (void*)conn, gerror->message);
        return FALSE;
    }

    gboolean finished = conn->sent == conn->request->data_len;
    if (finished)
        moloch_http_finish(conn, FALSE);


    return !finished;
}
Beispiel #2
0
gboolean moloch_http_write_cb(gint UNUSED(fd), GIOCondition UNUSED(cond), gpointer data) {
    MolochConn_t        *conn = data;
    GError              *gerror = 0;

    if (!conn->request) {
        DEBUGCONN("AAA ww clear %s %p fd:%d,%d ww:%d", conn->server->names[0], conn, fd, g_socket_get_fd(conn->conn), conn->writeWatch);
        conn->writeWatch = 0;
        return FALSE;
    }

    int sent = g_socket_send(conn->conn, conn->request->data+conn->sent, conn->request->data_len-conn->sent, NULL, &gerror);
    conn->sent += sent;

    if (gerror) {
        /* Should free stuff here */
        LOG("ERROR: %p: Write Error: %s", (void*)conn, gerror->message);
        g_error_free(gerror);
        DEBUGCONN("AAA ww clear %s %p fd:%d,%d ww:%d", conn->server->names[0], conn, fd, g_socket_get_fd(conn->conn), conn->writeWatch);
        conn->writeWatch = 0;
        return FALSE;
    }

    gboolean finished = conn->sent == conn->request->data_len;
    if (finished) {
        moloch_http_finish(conn, FALSE);
        DEBUGCONN("AAA ww finish %s %p fd:%d ww:%d", conn->server->names[0], conn, g_socket_get_fd(conn->conn), conn->writeWatch);
        conn->writeWatch = 0;
    }

    return !finished;
}
Beispiel #3
0
gboolean moloch_http_write_cb(gint UNUSED(fd), GIOCondition UNUSED(cond), gpointer data) {
    MolochConn_t        *conn = data;
    GError              *gerror = 0;

    if (!conn->request)
        return FALSE;

    int sent = g_socket_send(conn->conn, conn->request->data+conn->sent, conn->request->data_len-conn->sent, NULL, &gerror);
    conn->sent += sent;

    if (gerror) {
        /* Should free stuff here */
        LOG("ERROR: %p: Receive Error: %s", (void*)conn, gerror->message);
        g_error_free(gerror);
        return FALSE;
    }

    gboolean finished = conn->sent == conn->request->data_len;
    if (finished)
        moloch_http_finish(conn, FALSE);


    return !finished;
}
Beispiel #4
0
gboolean moloch_http_process_send(MolochConn_t *conn, gboolean sync)
{
    char                 buffer[3000];
    uint32_t             len;
    GError              *gerror = 0;
    MolochRequest_t     *request = conn->request;

    if (conn->conn == 0) {
        if (moloch_http_connect(conn, moloch_http_get_name(conn->server), conn->server->port, TRUE)) {
            LOG("%p: Couldn't connect %s", (void*)conn, conn->name);
            return FALSE;
        }
    }

    len = snprintf(buffer, sizeof(buffer),
                          "%s %.*s HTTP/1.1\r\n"
                          "Host: this.host\r\n"
                          "Content-Type: application/json\r\n"
                          "%s"
                          "Content-Length: %d\r\n"
                          "Connection: keep-alive\r\n"
                          "\r\n",
                          request->method,
                          request->key_len,
                          request->key,
                          request->compress?"Content-Encoding: deflate\r\n":"",
                          (int)request->data_len);

    gettimeofday(&conn->startTime, NULL);
    snprintf(conn->line, sizeof(conn->line), "%15.15s %d/%d/%d %p %s %s %.*s %d",
           ctime(&conn->startTime.tv_sec)+4,
           conn->server->connQ.e_count,
           conn->server->requestQ[0].r_count,
           conn->server->requestQ[1].r_count,
           (void*)conn,
           request->method,
           sync?"SYNC":"ASYNC",
           request->key_len,
           request->key,
           request->data_len);

    uint32_t sent = 0;
    while (!gerror && sent < len) {
        sent += g_socket_send(conn->conn, buffer+sent, len-sent, NULL, &gerror);
    }


    /* If async and we have data to send, send it when writable */
    if (!sync && request->data_len) {
        conn->sent = 0;
        gettimeofday(&conn->sendTime, NULL);
        moloch_watch_fd(g_socket_get_fd(conn->conn), G_IO_OUT, moloch_http_write_cb, conn);
        return TRUE;
    }

    sent = 0;
    while (!gerror && sent < request->data_len) {
        sent += g_socket_send(conn->conn, request->data+sent, request->data_len-sent, NULL, &gerror);
    }

    gettimeofday(&conn->sendTime, NULL);

    if (gerror) {
        LOG("%p: Send Error: %d %s", (void*)conn, sync, gerror->message);
        conn->conn = 0;
        g_error_free(gerror);
        return FALSE;
    }

    moloch_http_finish(conn, sync);

    return TRUE;
}