int cg_socket_readline(CgSocket *sock, char *buffer, int bufferLen) { int readCnt; int readLen; char c; cg_log_debug_l4("Entering...\n"); readCnt = 0; while (readCnt < (bufferLen-1)) { readLen = cg_socket_read(sock, &buffer[readCnt], sizeof(char)); if (readLen <= 0) return -1; readCnt++; if (buffer[readCnt-1] == CG_SOCKET_LF) break; } buffer[readCnt] = '\0'; if (buffer[readCnt-1] != CG_SOCKET_LF) { do { readLen = cg_socket_read(sock, &c, sizeof(char)); if (readLen <= 0) break; } while (c != CG_SOCKET_LF); } cg_log_debug_l4("Leaving...\n"); return readCnt; }
size_t cg_http_packet_read_chunk(CgHttpPacket *httpPkt, CgSocket *sock, char *lineBuf, size_t lineBufSize) { ssize_t readLen = 0; ssize_t conLen = 0; int tries = 0; char *content = NULL; cg_log_debug_l4("Entering...\n"); /* Read chunk header */ readLen = cg_socket_readline(sock, lineBuf, lineBufSize); conLen = cg_strhex2long(lineBuf); if (conLen < 1) return 0; content = (char *)malloc(conLen+1); if (content == NULL) { cg_log_debug_s("Memory allocation problem!\n"); return 0; } content[conLen] = 0; readLen = 0; /* Read content until conLen is reached, or tired of trying */ while (readLen < conLen && tries < 20) { readLen += cg_socket_read(sock, (content+readLen), (conLen-readLen)); tries++; } /* Append content to packet */ cg_http_packet_appendncontent(httpPkt, content, readLen); free(content); content = NULL; if (readLen == conLen) { /* Read CRLF bytes */ cg_socket_readline(sock, lineBuf, lineBufSize); } cg_log_debug_l4("Leaving...\n"); return readLen; }
long cg_socket_skip(CgSocket *sock, long skipLen) { int readCnt; int readLen; char c; cg_log_debug_l4("Entering...\n"); readCnt = 0; while (readCnt < skipLen) { readLen = cg_socket_read(sock, &c, sizeof(char)); if (readLen <= 0) break; readCnt++; } return readCnt; cg_log_debug_l4("Leaving...\n"); }
BOOL cg_http_packet_read_body(CgHttpPacket *httpPkt, CgSocket *sock, char *lineBuf, size_t lineBufSize) { ssize_t readLen; ssize_t conLen; char *content; char readBuf[READBUF_LENGTH + 1]; int tries = 0; cg_log_debug_l4("Entering...\n"); conLen = cg_http_packet_getcontentlength(httpPkt); content = NULL; if (0 < conLen) { content = (char *)malloc(conLen+1); if (content == NULL) { cg_log_debug_s("Memory allocation problem!\n"); return FALSE; } content[0] = '\0'; readLen = 0; /* Read content until conLen is reached, or tired of trying */ while (readLen < conLen && tries < 20) { readLen += cg_socket_read(sock, (content+readLen), (conLen-readLen)); /* Fixed to increment the counter only when cg_socket_read() doesn't read data */ if (readLen <= 0) tries++; } if (readLen <= 0) return TRUE; content[readLen] = '\0'; cg_http_packet_setcontentpointer(httpPkt, content, readLen); } else if (cg_http_packet_getheadervalue(httpPkt, CG_HTTP_CONTENT_LENGTH) == NULL) { /* header existance must be checked! otherwise packets which rightly report 0 as content length, will jam the http */ /* Check if we read chunked encoding */ if (cg_http_packet_ischunked(httpPkt) == TRUE) { conLen = 0; do { readLen = cg_http_packet_read_chunk(httpPkt, sock, lineBuf, lineBufSize); conLen += readLen; } while (readLen > 0); cg_http_packet_setcontentlength(httpPkt,conLen); } else { readLen = 0; conLen = 0; while ((readLen = cg_socket_read(sock, readBuf, READBUF_LENGTH)) > 0) { cg_http_packet_appendncontent(httpPkt, readBuf, readLen); conLen += readLen; } cg_http_packet_setcontentlength(httpPkt, conLen); } } cg_log_debug_l4("Leaving...\n"); return TRUE; }