static void FUNCTION_ATTRIBUTE disconnect_callback(void * arg) { PRINTF("http disconnected\n"); if(http_buf != NULL) { if((http_buf->buffer_size > 1)&&(http_buf->buffer != NULL)) { return http_exit(HTTP_OK); } } return http_exit(HTTP_ERR); }
static void FUNCTION_ATTRIBUTE dns_callback(const char * hostname, ip_addr_t * addr, void * arg) { //request_args * req = (request_args *)arg; if (addr == NULL) { PRINTF("DNS failed %s\n", hostname); http_exit(DNS_FAIL); } else { PRINTF("DNS found %s " IPSTR "\n", hostname, IP2STR(addr)); conn = (struct espconn *)os_malloc(sizeof(struct espconn)); conn->type = ESPCONN_TCP; conn->state = ESPCONN_NONE; conn->proto.tcp = (esp_tcp *)os_malloc(sizeof(esp_tcp)); conn->proto.tcp->local_port = espconn_port(); conn->proto.tcp->remote_port = http_port; os_memcpy(conn->proto.tcp->remote_ip, addr, 4); espconn_regist_connectcb(conn, connect_callback); espconn_regist_disconcb(conn, disconnect_callback); espconn_regist_reconcb(conn, reconnect_callback); PRINTF("start connect http server\n"); // TODO: consider using espconn_regist_reconcb (for timeouts?) // cf esp8266_sdk_v0.9.1/examples/at/user/at_ipCmd.c (TCP ARQ retransmission?) espconn_secure_connect(conn); } }
static void FUNCTION_ATTRIBUTE reconnect_callback(void *arg, sint8 err) { PRINTF("http reconnected, error:%d\n", err); if(-61 == err) { if(http_buf != NULL) { if((http_buf->buffer_size > 1)&&(http_buf->buffer != NULL)) { return http_exit(HTTP_OK); } } } http_exit(HTTP_ERR); }
static void http_error(WEBBLK *webblk, char *err, char *header, char *info) { hprintf(webblk->sock,"HTTP/1.0 %s\n%sConnection: close\n" "Content-Type: text/html\n\n" "<HTML><HEAD><TITLE>%s</TITLE></HEAD>" "<BODY><H1>%s</H1><P>%s</BODY></HTML>\n\n", err, header, err, err, info); http_exit(webblk); }
static void http_download(WEBBLK *webblk, char *filename) { char buffer[HTTP_PATH_LENGTH]; char tbuf[80]; int fd, length; char *filetype; char fullname[HTTP_PATH_LENGTH]; struct stat st; MIMETAB *mime_type = mime_types; strlcpy( fullname, http_serv.httproot, sizeof(fullname) ); strlcat( fullname, filename, sizeof(fullname) ); http_verify_path(webblk,fullname); if(stat(fullname,&st)) http_error(webblk, "404 File Not Found","", strerror(errno)); if(!S_ISREG(st.st_mode)) http_error(webblk, "404 File Not Found","", "The requested file is not a regular file"); fd = HOPEN(fullname,O_RDONLY|O_BINARY,0); if (fd == -1) http_error(webblk, "404 File Not Found","", strerror(errno)); hprintf(webblk->sock,"HTTP/1.0 200 OK\n"); if ((filetype = strrchr(filename,'.'))) for(mime_type++;mime_type->suffix && strcasecmp(mime_type->suffix,filetype + 1); mime_type++); if(mime_type->type) hprintf(webblk->sock,"Content-Type: %s\n", mime_type->type); hprintf(webblk->sock,"Expires: %s\n", http_timestring(tbuf,sizeof(tbuf),time(NULL)+HTML_STATIC_EXPIRY_TIME)); hprintf(webblk->sock,"Content-Length: %d\n\n", (int)st.st_size); while ((length = read(fd, buffer, sizeof(buffer))) > 0) hwrite(webblk->sock,buffer, length); close(fd); http_exit(webblk); }
/* Sets up the object. */ int http_init(http_t *client, char *msg) { int rc = 0; do { TRY(local_set_params(client)); TRY(ssl_open(client, msg)); } while (0); if (rc) { http_exit(client); return rc; } client->initialized = 1; return 0; }
/* Sets up the object. */ int http_init(http_t *client, char *msg) { int rc = 0; if (client->ssl_enabled) client->tcp.ip.port = HTTPS_DEFAULT_PORT; do { TRY(local_set_params(client)); TRY(tcp_init(&client->tcp, msg, client->verbose)); if (client->ssl_enabled) TRY(ssl_init(client, msg)); } while (0); if (rc) { http_exit(client); return rc; } client->initialized = 1; return 0; }
static void *http_request(void* arg) { WEBBLK *webblk; int authok = !http_serv.httpauth; char line[HTTP_PATH_LENGTH]; char *url = NULL; char *pointer; char *strtok_str = NULL; CGITAB *cgient; int content_length = 0; int sock = (int) (uintptr_t) arg; if(!(webblk = malloc(sizeof(WEBBLK)))) http_exit(webblk); memset(webblk,0,sizeof(WEBBLK)); webblk->sock = sock; while (hgets(line, sizeof(line), webblk->sock)) { if (*line == '\r' || *line == '\n') break; if((pointer = strtok_r(line," \t\r\n",&strtok_str))) { if(!strcasecmp(pointer,"GET")) { if((pointer = strtok_r(NULL," \t\r\n",&strtok_str))) { webblk->request_type = REQTYPE_GET; url = strdup(pointer); } } else if(!strcasecmp(pointer,"POST")) { if((pointer = strtok_r(NULL," \t\r\n",&strtok_str))) { webblk->request_type = REQTYPE_POST; url = strdup(pointer); } } else if(!strcasecmp(pointer,"PUT")) { http_error(webblk,"400 Bad Request", "", "This server does not accept PUT requests"); } else if(!strcasecmp(pointer,"Authorization:")) { if((pointer = strtok_r(NULL," \t\r\n",&strtok_str))) authok = http_authenticate(webblk,pointer, strtok_r(NULL," \t\r\n",&strtok_str)); } else if(!strcasecmp(pointer,"Cookie:")) { if((pointer = strtok_r(NULL,"\r\n",&strtok_str))) http_interpret_variable_string(webblk, pointer, VARTYPE_COOKIE); } else if(!strcasecmp(pointer,"Content-Length:")) { if((pointer = strtok_r(NULL," \t\r\n",&strtok_str))) content_length = atoi(pointer); } } } webblk->request = url; if(webblk->request_type == REQTYPE_POST && content_length != 0) { char *post_arg; if((pointer = post_arg = malloc(content_length + 1))) { int i; for(i = 0; i < content_length; i++) { *pointer = hgetc(webblk->sock); if(*pointer != '\n' && *pointer != '\r') pointer++; } *pointer = '\0'; http_interpret_variable_string(webblk, post_arg, VARTYPE_POST); free(post_arg); } } if (!authok) { http_error(webblk, "401 Authorization Required", "WWW-Authenticate: Basic realm=\"HERCULES\"\n", "You must be authenticated to use this service"); } if (!url) { http_error(webblk,"400 Bad Request", "", "You must specify a GET or POST request"); } /* anything following a ? in the URL is part of the get arguments */ if ((pointer=strchr(url,'?'))) { *pointer++ = 0; http_interpret_variable_string(webblk, pointer, VARTYPE_GET); } while(url[0] == '/' && url[1] == '/') url++; webblk->baseurl = url; if(!strcasecmp("/",url)) url = HTTP_WELCOME; if(strncasecmp("/cgi-bin/",url,9)) http_download(webblk,url); else url += 9; while(*url == '/') url++; #if 0 http_dump_cgi_variables(webblk); #endif for(cgient = cgidir; cgient->path; cgient++) { if(!strcmp(cgient->path, url)) { char tbuf[80]; hprintf(webblk->sock,"HTTP/1.0 200 OK\nConnection: close\n"); hprintf(webblk->sock,"Date: %s\n", http_timestring(tbuf,sizeof(tbuf),time(NULL))); (cgient->cgibin) (webblk); http_exit(webblk); } } #if defined(OPTION_DYNAMIC_LOAD) { zz_cgibin dyncgi; if( (dyncgi = HDL_FINDSYM(webblk->baseurl)) ) { char tbuf[80]; hprintf(webblk->sock,"HTTP/1.0 200 OK\nConnection: close\n"); hprintf(webblk->sock,"Date: %s\n", http_timestring(tbuf,sizeof(tbuf),time(NULL))); dyncgi(webblk); http_exit(webblk); } } #endif /*defined(OPTION_DYNAMIC_LOAD)*/ http_error(webblk, "404 File Not Found","", "The requested file was not found"); return NULL; }
static int request(ddns_t *ctx, ddns_info_t *info, ddns_alias_t *alias) { int i, rc = 0; http_t client; http_trans_t trans; char *buf, *tmp, *line, *hash = NULL; char host[256], updateurl[256]; char buffer[256]; char digeststr[SHA1_DIGEST_BYTES * 2 + 1]; unsigned char digestbuf[SHA1_DIGEST_BYTES]; do { TRY(http_construct(&client)); http_set_port(&client, info->server_name.port); http_set_remote_name(&client, info->server_name.name); http_set_bind_iface(&client, ctx->bind_interface); client.ssl_enabled = info->ssl_enabled; if (client.ssl_enabled) /* XXX: Fix this better, possibly in http_init() */ client.tcp.ip.port = 443; TRY(http_init(&client, "Sending update URL query")); snprintf(buffer, sizeof(buffer), "%s|%s", info->creds.username, info->creds.password); sha1((unsigned char *)buffer, strlen(buffer), digestbuf); for (i = 0; i < SHA1_DIGEST_BYTES; i++) sprintf(&digeststr[i * 2], "%02x", digestbuf[i]); snprintf(buffer, sizeof(buffer), "/api/?action=getdyndns&sha=%s", digeststr); trans.req_len = snprintf(ctx->request_buf, ctx->request_buflen, GENERIC_HTTP_REQUEST, buffer, info->server_name.name); trans.p_req = ctx->request_buf; trans.p_rsp = ctx->work_buf; trans.max_rsp_len = ctx->work_buflen - 1; /* Save place for a \0 at the end */ rc = http_transaction(&client, &trans); rc |= http_exit(&client); http_destruct(&client, 1); if (rc) break; TRY(http_status_valid(trans.status)); tmp = buf = strdup(trans.p_rsp_body); for (line = strsep(&tmp, "\n"); line; line = strsep(&tmp, "\n")) { int num; num = sscanf(line, "%255[^|\r\n]|%*[^|\r\n]|%255[^|\r\n]", host, updateurl); if (*line && num == 2 && !strcmp(host, alias->name)) { hash = strstr(updateurl, "?"); break; } } free(buf); if (!hash) rc = RC_DYNDNS_RSP_NOTOK; else hash++; } while (0); if (rc) { logit(LOG_INFO, "Update URL query failed"); return 0; } return snprintf(ctx->request_buf, ctx->request_buflen, FREEDNS_UPDATE_IP_REQUEST, info->server_url, hash, alias->address, info->server_name.name); }
void ExitDll(HMODULE hDll) { DeleteCriticalSection(&cs_update); http_exit(); }