static int response_la_do_header(struct http_state *http) { struct response_la_priv_s *priv = http->response_priv; if (priv->command != 2) return httpd_do_std_header(http); const char *t=0, *o=0; char buf[32]; switch (priv->hdr_state) { case 0: t = "Content-Type"; o = "application/binary"; break; case 1: t = "Content-Length"; sprintf(buf, "%d", priv->len); o = buf; break; case 2: return httpd_do_std_header(http); } int av = httpd_available_sendbuffer(http); if (av < (strlen(t) + strlen(o) + 4)) return 1; httpd_put_sendbuffer_string(http, t); httpd_put_sendbuffer_string(http, ": "); httpd_put_sendbuffer_string(http, o); httpd_put_sendbuffer_string(http, "\r\n"); ++priv->hdr_state; return 2; }
static int response_vfs_do_header(struct http_state *http) { struct response_vfs_priv_s *priv = http->response_priv; const char *t=0, *o=0; char buf[32]; switch (priv->hdr_state) { case 0: t = "Content-Type"; o = priv->f->mime_type; break; case 1: if (!(priv->f->flags & 1)) { t = "Content-Length"; sprintf(buf, "%d", priv->f->len); o = buf; break; } case 2: return httpd_do_std_header(http); } int av = httpd_available_sendbuffer(http); if (av < (strlen(t) + strlen(o) + 4)) return 1; httpd_put_sendbuffer_string(http, t); httpd_put_sendbuffer_string(http, ": "); httpd_put_sendbuffer_string(http, o); httpd_put_sendbuffer_string(http, "\r\n"); ++priv->hdr_state; return 2; }
int httpd_do_std_header(struct http_state *http) { const char *s=0; switch (http->std_header_state) { case 0: s = "Server: yahd v1.0\r\n"; break; case 1: s = "\r\n"; break; case 2: s = 0; break; } if (s) { int av = httpd_available_sendbuffer(http); if (av < strlen(s)) return 1; httpd_put_sendbuffer_string(http, s); ++http->std_header_state; return 2; } else return 0; }
static int response_vfs_do_dynamic(struct http_state *http, const char *cmd, int len) { int av = httpd_available_sendbuffer(http); char *res; len++; if (len == 1) res = "%"; else res = "illegal dhtml primitive"; if (av < strlen(res)) return 1; httpd_put_sendbuffer_string(http, res); return 0; }
static int response_la_do_data(struct http_state *http) { struct response_la_priv_s *priv = http->response_priv; if (priv->command != 2) { httpd_put_sendbuffer_string(http, "OK\n"); return 0; } int av = httpd_available_sendbuffer(http); if (!av) return 1; if (av > (priv->size - priv->ptr)) av = priv->size - priv->ptr; if (av > priv->len) av = priv->len; while (av) { int maxread = 1024; if (maxread > av) maxread = av; microblaze_init_dcache_range((int)(priv->ptr + priv->base), maxread); hdprintf("sending %08x..%08x, len %08x, size %08x\n", priv->ptr, priv->ptr+maxread, priv->len, priv->size); httpd_put_sendbuffer(http, (void*)(priv->ptr + priv->base), maxread); priv->ptr += maxread; priv->len -= maxread; av -= maxread; } if (priv->ptr == priv->size) { /* wrap */ priv->ptr = 0; } if (!priv->len) return 0; return 1; }
static void httpd_handle_server(struct http_state *http) { int done = 0, busy = 0; while (!done) { switch (http->state_server) { case HTTPD_SERVER_IDLE: done = 1; break; case HTTPD_SERVER_RESPONSE: { hdprintf("send response\n"); if (!http->code_descr) httpd_get_descr(http); int reslen = 13 + strlen(http->code_descr); // HTTP/1.0 200 OK -> 8 + 1 + 3 + 1 + strlen(code_descr); if (httpd_available_sendbuffer(http) < reslen) { busy = 1; break; } char code[5]; code[0] = (http->code / 100) + '0'; code[1] = (http->code / 10) % 10 + '0'; code[2] = http->code % 10 + '0'; code[3] = ' '; code[4] = 0; httpd_put_sendbuffer_string(http, "HTTP/1.0 "); httpd_put_sendbuffer_string(http, code); httpd_put_sendbuffer_string(http, http->code_descr); httpd_put_sendbuffer_string(http, "\r\n"); http->state_server = HTTPD_SERVER_HEADER; hdprintf("now in header state!\n"); break; } case HTTPD_SERVER_HEADER: { int r = 1; if (http->handler->do_header) r = http->handler->do_header(http); else r = httpd_do_std_header(http); if (r == 0) http->state_server = HTTPD_SERVER_DATA; else if (r == 1) busy = 1; break; } case HTTPD_SERVER_DATA: { int r = 1; if (http->handler->do_data) r = http->handler->do_data(http); if (r == 0) http->state_server = HTTPD_SERVER_CLOSE; else if (r == 1) busy = 1; break; } case HTTPD_SERVER_CLOSE: if (http->handler) { if (http->handler->finish) http->handler->finish(http); } http->handler = 0; done = 1; break; } #ifdef UNIX if (http->sendbuffer_read != http->sendbuffer_write) httpd_try_flush_sendbuffer(http); #else if (busy) return; #endif } }