int hc_post_request(struct hc * hc, int (*callback)(unsigned char *, size_t, void *), void * v) { char buffer[1024]; size_t l; struct hc_headers * h; l = strlen(hc->localpart) + strlen("POST HTTP/1.0\r\n"); for (h = hc->req_headers; h; h = h->next) l += strlen(h->tag) + strlen(h->value) + 4; l += 3; if (l > sizeof buffer) { fprintf(stderr, "ERROR: hc_post_request: buffer too small; need %lu\n", (unsigned long)l); return -1; } l = sprintf(buffer, "POST %s HTTP/1.0\r\n", hc->localpart); for (h = hc->req_headers; h; h = h->next) l += sprintf(buffer+l, "%s: %s\r\n", h->tag, h->value); l += sprintf(buffer+l, "\r\n"); nc_write(hc->nc, buffer, l); while ((l = callback(buffer, sizeof buffer, v)) != 0) { int r; r = nc_write(hc->nc, buffer, l); // fprintf(stderr, "%d\n", r); } if (nc_read_line(hc->nc, buffer, sizeof buffer) <= 0) { perror("ERROR: hc_post_request nc_read_line"); return -1; } hc->status = strdup(buffer); while (nc_read_line(hc->nc, buffer, sizeof buffer) > 0) { char * e; e = strchr(buffer, ':'); if (e) { *e = '\0'; e++; while (isspace(*e)) e++; h = hc_h_make(buffer, e); h->next = hc->rsp_headers; hc->rsp_headers = h; } else { fprintf(stderr, "ERROR: hc_post_request got invalid header line :%s:\n", buffer); } } return 0; }
int hc_add_req_header(struct hc * hc, const char * tag, const char * value) { struct hc_headers * h; h = hc_h_make(tag, value); if (!h) goto error; h->next = hc->req_headers; hc->req_headers = h; return 0; error: return -1; }