MolochConn_t * moloch_http_create(MolochHttp_t *server) { MolochConn_t *conn; conn = MOLOCH_TYPE_ALLOC0(MolochConn_t); conn->parser.data = conn; conn->server = server; if (moloch_http_connect(conn, server->name, server->port)) { printf("Couldn't connect to elastic search at '%s'", server->name); exit (1); } return conn; }
MolochConn_t * moloch_http_create(MolochHttp_t *server, int blocking) { MolochConn_t *conn; int tries = server->namesCnt; conn = MOLOCH_TYPE_ALLOC0(MolochConn_t); conn->parser.data = conn; conn->server = server; while (tries > 0) { if (!moloch_http_connect(conn, moloch_http_get_name(server), server->port, blocking)) { return conn; } tries--; LOG("Couldn't connect to '%s'", conn->name); } exit (1); }
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; }