static int mailinfo(FILE *in, FILE *out, const char *msg, const char *patch) { int peek; fin = in; fout = out; cmitmsg = fopen(msg, "w"); if (!cmitmsg) { perror(msg); return -1; } patchfile = fopen(patch, "w"); if (!patchfile) { perror(patch); fclose(cmitmsg); return -1; } p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*p_hdr_data)); s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*s_hdr_data)); do { peek = fgetc(in); } while (isspace(peek)); ungetc(peek, in); /* process the email header */ while (read_one_header_line(&line, fin)) check_header(&line, p_hdr_data, 1); handle_body(); handle_info(); return 0; }
rss_body *parse_bencode_from_file(const char *filename) { be_node *node; char *contents = 0; unsigned int length = read_file(filename, &contents); if ((node = be_decoden(contents, length))) { return handle_body(node); } else { return NULL; } }
int mailinfo(struct mailinfo *mi, const char *msg, const char *patch) { FILE *cmitmsg; int peek; struct strbuf line = STRBUF_INIT; cmitmsg = fopen(msg, "w"); if (!cmitmsg) { perror(msg); return -1; } mi->patchfile = fopen(patch, "w"); if (!mi->patchfile) { perror(patch); fclose(cmitmsg); return -1; } mi->p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*(mi->p_hdr_data))); mi->s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*(mi->s_hdr_data))); do { peek = fgetc(mi->input); if (peek == EOF) { fclose(cmitmsg); return error("empty patch: '%s'", patch); } } while (isspace(peek)); ungetc(peek, mi->input); /* process the email header */ while (read_one_header_line(&line, mi->input)) check_header(mi, &line, mi->p_hdr_data, 1); handle_body(mi, &line); fwrite(mi->log_message.buf, 1, mi->log_message.len, cmitmsg); fclose(cmitmsg); fclose(mi->patchfile); handle_info(mi); strbuf_release(&line); return mi->input_error; }
static gboolean received_data(GIOChannel *channel, GIOCondition cond, gpointer user_data) { struct web_session *session = user_data; guint8 *ptr = session->receive_buffer; gsize bytes_read; GIOStatus status; if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) { session->transport_watch = 0; session->result.buffer = NULL; session->result.length = 0; call_result_func(session, 400); return FALSE; } status = g_io_channel_read_chars(channel, (gchar *) session->receive_buffer, session->receive_space - 1, &bytes_read, NULL); debug(session->web, "bytes read %zu", bytes_read); if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN) { session->transport_watch = 0; session->result.buffer = NULL; session->result.length = 0; call_result_func(session, 0); return FALSE; } session->receive_buffer[bytes_read] = '\0'; if (session->header_done == TRUE) { if (handle_body(session, session->receive_buffer, bytes_read) < 0) { session->transport_watch = 0; return FALSE; } return TRUE; } while (bytes_read > 0) { guint8 *pos; gsize count; char *str; pos = memchr(ptr, '\n', bytes_read); if (pos == NULL) { g_string_append_len(session->current_header, (gchar *) ptr, bytes_read); return TRUE; } *pos = '\0'; count = strlen((char *) ptr); if (count > 0 && ptr[count - 1] == '\r') { ptr[--count] = '\0'; bytes_read--; } g_string_append_len(session->current_header, (gchar *) ptr, count); bytes_read -= count + 1; if (bytes_read > 0) ptr = pos + 1; else ptr = NULL; if (session->current_header->len == 0) { char *val; session->header_done = TRUE; val = g_hash_table_lookup(session->result.headers, "Transfer-Encoding"); if (val != NULL) { val = g_strrstr(val, "chunked"); if (val != NULL) { session->result.use_chunk = TRUE; session->chunck_state = CHUNK_SIZE; session->chunk_left = 0; session->total_len = 0; } } if (handle_body(session, ptr, bytes_read) < 0) { session->transport_watch = 0; return FALSE; } break; } str = session->current_header->str; if (session->result.status == 0) { unsigned int code; if (sscanf(str, "HTTP/%*s %u %*s", &code) == 1) session->result.status = code; } debug(session->web, "[header] %s", str); /* handle multi-line header */ if (str[0] == ' ' || str[0] == '\t') handle_multi_line(session); else add_header_field(session); g_string_truncate(session->current_header, 0); } return TRUE; }