int ICACHE_FLASH_ATTR write_json_array_end(http_connection *c){ return http_write(c,"]"); }
int ICACHE_FLASH_ATTR write_json_int(http_connection *c,int value){ char buf[10]; os_sprintf(buf,"%d",value); return http_write(c,buf); }
int ICACHE_FLASH_ATTR write_json_object_separator(http_connection *c){ return http_write(c,":"); }
int ICACHE_FLASH_ATTR write_json_array_start(http_connection *c){ return http_write(c,"["); }
//json int ICACHE_FLASH_ATTR write_json_object_start(http_connection *c){ return http_write(c,"{"); }
int ICACHE_FLASH_ATTR write_json_object_end(http_connection *c){ return http_write(c,"}"); }
static int http_connect(URLContext *h, const char *path, const char *hoststr) { HTTPContext *s = h->priv_data; int post, err, ch; char line[1024], *q; /* send http header */ post = h->flags & URL_WRONLY; snprintf(s->buffer, sizeof(s->buffer), "%s %s HTTP/1.0\r\n" "User-Agent: %s\r\n" "Accept: */*\r\n" "Host: %s\r\n" "\r\n", post ? "POST" : "GET", path, LIBAVFORMAT_IDENT, hoststr); if (http_write(h, s->buffer, strlen(s->buffer)) < 0) return AVERROR_IO; /* init input buffer */ s->buf_ptr = s->buffer; s->buf_end = s->buffer; s->line_count = 0; s->location[0] = '\0'; if (post) { sleep(1); return 0; } /* wait for header */ q = line; for(;;) { ch = http_getc(s); if (ch < 0) return AVERROR_IO; if (ch == '\n') { /* process line */ if (q > line && q[-1] == '\r') q--; *q = '\0'; #ifdef DEBUG printf("header='%s'\n", line); #endif err = process_line(s, line, s->line_count); if (err < 0) return err; if (err == 0) return 0; s->line_count++; q = line; } else { if ((q - line) < sizeof(line) - 1) *q++ = ch; } } }
static int http_connect(URLContext *h, const char *path, const char *hoststr, const char *auth, int *new_location) { HTTPContext *s = h->priv_data; int post, err, ch; char line[1024], *q; char *auth_b64; int auth_b64_len = strlen(auth)* 4 / 3 + 12; offset_t off = s->off; /* send http header */ post = h->flags & URL_WRONLY; auth_b64 = av_malloc(auth_b64_len); av_base64_encode(auth_b64, auth_b64_len, (uint8_t *)auth, strlen(auth)); snprintf(s->buffer, sizeof(s->buffer), "%s %s HTTP/1.1\r\n" "User-Agent: %s\r\n" "Accept: */*\r\n" "Range: bytes=%"PRId64"-\r\n" "Host: %s\r\n" "Authorization: Basic %s\r\n" "Connection: close\r\n" "\r\n", post ? "POST" : "GET", path, LIBAVFORMAT_IDENT, s->off, hoststr, auth_b64); av_freep(&auth_b64); if (http_write(h, s->buffer, strlen(s->buffer)) < 0) return AVERROR_IO; /* init input buffer */ s->buf_ptr = s->buffer; s->buf_end = s->buffer; s->line_count = 0; s->off = 0; s->filesize = -1; if (post) { sleep(1); return 0; } /* wait for header */ q = line; for(;;) { ch = http_getc(s); if (ch < 0) return AVERROR_IO; if (ch == '\n') { /* process line */ if (q > line && q[-1] == '\r') q--; *q = '\0'; #ifdef DEBUG printf("header='%s'\n", line); #endif err = process_line(h, line, s->line_count, new_location); if (err < 0) return err; if (err == 0) break; s->line_count++; q = line; } else { if ((q - line) < sizeof(line) - 1) *q++ = ch; } } return (off == s->off) ? 0 : -1; }
static void cmd_http_txreq(CMD_ARGS) { struct http *hp; const char *req = "GET"; const char *url = "/"; const char *proto = "HTTP/1.1"; const char *body = NULL; (void)cmd; (void)vl; CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); ONLY_CLIENT(hp, av); assert(!strcmp(av[0], "txreq")); av++; vsb_clear(hp->vsb); for(; *av != NULL; av++) { if (!strcmp(*av, "-url")) { url = av[1]; av++; } else if (!strcmp(*av, "-proto")) { proto = av[1]; av++; } else if (!strcmp(*av, "-req")) { req = av[1]; av++; } else break; } vsb_printf(hp->vsb, "%s %s %s%s", req, url, proto, nl); for(; *av != NULL; av++) { if (!strcmp(*av, "-hdr")) { vsb_printf(hp->vsb, "%s%s", av[1], nl); av++; } else break; } for(; *av != NULL; av++) { if (!strcmp(*av, "-body")) { AZ(body); body = av[1]; av++; } else if (!strcmp(*av, "-bodylen")) { AZ(body); body = synth_body(av[1]); av++; } else break; } if (*av != NULL) vtc_log(hp->vl, 0, "Unknown http txreq spec: %s\n", *av); if (body != NULL) vsb_printf(hp->vsb, "Content-Length: %d%s", strlen(body), nl); vsb_cat(hp->vsb, nl); if (body != NULL) { vsb_cat(hp->vsb, body); vsb_cat(hp->vsb, nl); } http_write(hp, 4, "txreq"); }
static int http_connect(URLContext *h, const char *path, const char *hoststr, const char *auth, int *new_location) { HTTPContext *s = h->priv_data; int post, err; char line[1024]; char *auth_b64; int auth_b64_len = (strlen(auth) + 2) / 3 * 4 + 1; int64_t off = s->off; /* send http header */ post = h->flags & URL_WRONLY; auth_b64 = av_malloc(auth_b64_len); av_base64_encode(auth_b64, auth_b64_len, auth, strlen(auth)); snprintf(s->buffer, sizeof(s->buffer), "%s %s HTTP/1.1\r\n" "User-Agent: %s\r\n" "Accept: */*\r\n" "Range: bytes=%"PRId64"-\r\n" "Host: %s\r\n" "Authorization: Basic %s\r\n" "Connection: close\r\n" "\r\n", post ? "POST" : "GET", path, LIBAVFORMAT_IDENT, s->off, hoststr, auth_b64); av_freep(&auth_b64); if (http_write(h, s->buffer, strlen(s->buffer)) < 0) return AVERROR(EIO); /* init input buffer */ s->buf_ptr = s->buffer; s->buf_end = s->buffer; s->line_count = 0; s->off = 0; if (s->filesize <= 0) s->filesize = -1; if (post) { return 0; } /* wait for header */ for(;;) { if (http_get_line(s, line, sizeof(line)) < 0) return AVERROR(EIO); dprintf(NULL, "header='%s'\n", line); err = process_line(h, line, s->line_count, new_location); if (err < 0) return err; if (err == 0) break; s->line_count++; } return (off == s->off) ? 0 : -1; }
static void cmd_http_txresp(CMD_ARGS) { struct http *hp; const char *proto = "HTTP/1.1"; const char *status = "200"; const char *msg = "Ok"; int bodylen = 0; char *b, *c; char *body = NULL, *nullbody; int nolen = 0; (void)cmd; (void)vl; CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); ONLY_SERVER(hp, av); assert(!strcmp(av[0], "txresp")); av++; vsb_clear(hp->vsb); /* send a "Content-Length: 0" header unless something else happens */ REPLACE(body, ""); nullbody = body; for(; *av != NULL; av++) { if (!strcmp(*av, "-proto")) { proto = av[1]; av++; } else if (!strcmp(*av, "-status")) { status = av[1]; av++; } else if (!strcmp(*av, "-msg")) { msg = av[1]; av++; continue; } else break; } vsb_printf(hp->vsb, "%s %s %s%s", proto, status, msg, nl); for(; *av != NULL; av++) { if (!strcmp(*av, "-nolen")) { nolen = 1; } else if (!strcmp(*av, "-hdr")) { vsb_printf(hp->vsb, "%s%s", av[1], nl); av++; } else break; } for(; *av != NULL; av++) { if (!strcmp(*av, "-body")) { assert(body == nullbody); REPLACE(body, av[1]); AN(body); av++; bodylen = strlen(body); for (b = body; *b != '\0'; b++) { if(*b == '\\' && b[1] == '0') { *b = '\0'; for(c = b+1; *c != '\0'; c++) { *c = c[1]; } b++; bodylen--; } } } else if (!strcmp(*av, "-bodylen")) { assert(body == nullbody); body = synth_body(av[1]); bodylen = strlen(body); av++; } else break; } if (*av != NULL) vtc_log(hp->vl, 0, "Unknown http txresp spec: %s\n", *av); if (body != NULL && !nolen) vsb_printf(hp->vsb, "Content-Length: %d%s", bodylen, nl); vsb_cat(hp->vsb, nl); if (body != NULL) vsb_bcat(hp->vsb, body, bodylen); http_write(hp, 4, "txresp"); }
void do_process(http_context *http) { http_join_head_body(http); http_write(http); }
static int http_connect(HTTPContext *s, const char *path, const char *hoststr, int flags, int wait) { int post, err, ch; char line[512], *q; /* send http header */ post = flags & O_WRONLY; s->len = 0; s->tread = 0; s->bpos = 0; s->bsz = 0; snprintf(line, sizeof(line), "%s %s HTTP/1.0\r\n" "User-Agent: %s\r\n" "Host: %s\r\n" "Accept: */*\r\n" "Connection: keep-alive\r\n" "\r\n", post ? "POST" : "GET", path, "DreamShell", hoststr); if (http_write((void*) s, line, (ssize_t)strlen(line)) < 0) return -1; #ifdef DEBUG dbglog(DBG_DEBUG, "http : sent header -->\n%s", line); #endif /* init input buffer */ s->line_count = 0; //s->location[0] = '\0'; if (post) { return 0; } /* wait for header */ q = line; if (wait) thd_sleep(wait); for(;;) { ch = http_getc((uint32) s); #ifdef DEBUG dbglog(DBG_DEBUG, "%c", ch); #endif if (ch < 0) { #ifdef DEBUG dbglog(DBG_DEBUG, "http header truncated\n"); #endif return -1; } if (ch == '\n') { /* process line */ if (q > line && q[-1] == '\r') q--; *q = '\0'; #ifdef DEBUG dbglog(DBG_DEBUG, "header='%s'\n", line); #endif err = process_line(s, line, s->line_count); if (err < 0) return err; if (err == 0) return 0; s->line_count++; q = line; } else { if ((q - line) < sizeof(line) - 1) *q++ = ch; } } }
int ICACHE_FLASH_ATTR write_json_float(http_connection *c,float value){ char buf[20]; c_sprintf(buf,"%f",value); return http_write(c,buf); }
//RESPONSE int ICACHE_FLASH_ATTR http_response_start(http_connection *c){ return http_write(c,"HTTP/") && http_write(c,HTTP_VERSION) && http_write(c," "); }
int ICACHE_FLASH_ATTR write_json_key(http_connection *c,const char *key){ return http_write(c,"\"") && http_write(c,key) && http_write(c,"\""); }
static int http_connect(URLContext *h, const char *path, const char *hoststr, const char *auth, int *new_location) { HTTPContext *s = h->priv_data; int post, err; char line[1024]; char *authstr = NULL; int64_t off = s->off; /* send http header */ post = h->flags & URL_WRONLY; authstr = ff_http_auth_create_response(&s->auth_state, auth, path, post ? "POST" : "GET"); snprintf(s->buffer, sizeof(s->buffer), "%s %s HTTP/1.1\r\n" "User-Agent: %s\r\n" "Accept: */*\r\n" "Range: bytes=%"PRId64"-\r\n" "Host: %s\r\n" "%s" "Connection: close\r\n" "%s" "\r\n", post ? "POST" : "GET", path, LIBAVFORMAT_IDENT, s->off, hoststr, authstr ? authstr : "", post ? "Transfer-Encoding: chunked\r\n" : ""); av_freep(&authstr); if (http_write(h, s->buffer, strlen(s->buffer)) < 0) return AVERROR(EIO); /* init input buffer */ s->buf_ptr = s->buffer; s->buf_end = s->buffer; s->line_count = 0; s->off = 0; s->filesize = -1; if (post) { /* always use chunked encoding for upload data */ s->chunksize = 0; return 0; } /* wait for header */ for(;;) { if (http_get_line(s, line, sizeof(line)) < 0) return AVERROR(EIO); dprintf(NULL, "header='%s'\n", line); err = process_line(h, line, s->line_count, new_location); if (err < 0) return err; if (err == 0) break; s->line_count++; } return (off == s->off) ? 0 : -1; }