+void client_poll_post_data(struct client *cl)
+{
+ struct dispatch *d = &cl->dispatch;
+ struct http_request *r = &cl->request;
+ char *buf;
+ int len;
+
+ if (cl->state == CLIENT_STATE_DONE)
+ return;
+
+ while (1) {
+ char *sep;
+ int offset = 0;
+ int cur_len;
+
+ buf = ustream_get_read_buf(cl->us, &len);
+ if (!buf || !len)
+ break;
+
+ if (!d->data_send)
+ return;
+
+ cur_len = min(r->content_length, len);
+ if (cur_len) {
+ if (d->data_blocked)
+ break;
+
+ if (d->data_send)
+ cur_len = d->data_send(cl, buf, cur_len);
+
+ r->content_length -= cur_len;
+ ustream_consume(cl->us, cur_len);
+ continue;
+ }
+
+ if (!r->transfer_chunked)
+ break;
+
+ if (r->transfer_chunked > 1)
+ offset = 2;
+
+ sep = strstr(buf + offset, "\r\n");
+ if (!sep)
+ break;
+
+ *sep = 0;
+
+ r->content_length = strtoul(buf + offset, &sep, 16);
+ r->transfer_chunked++;
+ ustream_consume(cl->us, sep + 2 - buf);
+
+ /* invalid chunk length */
+ if (sep && *sep) {
+ r->content_length = 0;
+ r->transfer_chunked = 0;
+ break;
+ }
+
+ /* empty chunk == eof */
+ if (!r->content_length) {
+ r->transfer_chunked = false;
+ break;
+ }
+ }
+
+ buf = ustream_get_read_buf(cl->us, &len);
+ if (!r->content_length && !r->transfer_chunked &&
+ cl->state != CLIENT_STATE_DONE) {
+ if (cl->dispatch.data_done)
+ cl->dispatch.data_done(cl);
+
+ cl->state = CLIENT_STATE_DONE;
+ }
+}
+