コード例 #1
0
ファイル: server_function.c プロジェクト: ztmhsj/webserver
void process_cmd(int sockcnfd,char *cmd)
{
	char url[MAXSIZE]="./www";
	char buf[MAXSIZE];
	if(get_url(sockcnfd,cmd,url)!=-1){
		printf("get url is %s\n",url);
		if(is_noexit(url))
			send_404(sockcnfd);
		else if(is_dir(url))
			re_ls(sockcnfd,url);
		else
			re_file(sockcnfd,url);
	}
	close(sockcnfd);
}
コード例 #2
0
ファイル: web.c プロジェクト: IcarusEngineering/ARPilot
int handle_web(int http_fd)
{
	char buffer[4096];
	int len = 0;

    bzero(buffer,4096);
    len = read(http_fd, buffer, 4096);

    if (strncmp(buffer,"\n\n",len) < 0)
    	goto end_close;

    if (strncmp(buffer,"GET ",4))
		goto end_close;

    /* Look for known documents */
    if (!strncmp(buffer,"GET /index.html",15)){
    	send_index(http_fd);
    	goto end_close;
    }

    if (!strncmp(buffer,"GET /stat.html",14)){
    	send_status(http_fd);
    	goto end_close;
    }

    if (!strncmp(buffer,"GET /video.html",15)){
    	send_video(http_fd);
    	goto end_leave;
    }

    if (!strncmp(buffer,"GET /command.html",17)){
        send_command(http_fd);
        goto end_close;
    }

    if (!strncmp(buffer,"GET /log.html",13)){
        send_log(http_fd);
        goto end_leave;
    }

    if (!strncmp(buffer,"GET /cmd.html?",14)){
    	printf("WEB-CMD\n");
        send_cmd(http_fd, buffer+14);
        goto end_close;
    }

    if (!strncmp(buffer,"GET /emer.html",14)){
        send_emergency(http_fd);
        goto end_close;
    }

    /* Send 404 error */
    send_404(http_fd);


end_close:
	close(http_fd);

end_leave:
	return 0;
}
コード例 #3
0
ファイル: user_main.c プロジェクト: withmaia/maia-attinytemp
void ICACHE_FLASH_ATTR server_recv_cb(void *arg, char *http_raw, unsigned short length) {
    struct espconn *pespconn = (struct espconn *)arg;
    //print("[server_recv_cb] Received data:");

    char method[10];
    char path[60];
    char headers[60];
    char body[256];
    parse_http(http_raw, length, method, path, headers, body);

    int GET = (os_strcmp(method, "GET") == 0);
    int POST = (os_strcmp(method, "POST") == 0);

    if (GET) { // No body if not [post/put/patch]ing

        // Static files

        if (os_strcmp(path, "/base.css") == 0) { send_ok(pespconn, base_css); }
        else if (os_strcmp(path, "/connect.js") == 0) { send_ok(pespconn, connect_js); }
        else if (os_strcmp(path, "/register.js") == 0) { send_ok(pespconn, register_js); }

        // JSON responses

        else if (os_strcmp(path, "/connection.json") == 0) {
            int station_connect_status = wifi_station_get_connect_status();

            if (station_connect_status == STATION_GOT_IP) {
                struct ip_info ipConfig;
                wifi_get_ip_info(STATION_IF, &ipConfig);
                char json_str[54];
                os_sprintf(json_str, "{\"status\": \"connected\", \"ip\": \"%d.%d.%d.%d\"}", IP2STR(&ipConfig.ip));
                send_json(pespconn, json_str);
            }

            else {
                char *status_str;

                if (connection_status == CONNECTION_UNCONFIGURED) status_str = "unconfigured";
                else
                switch (station_connect_status) {
                    case STATION_CONNECTING: status_str = "connecting"; break;
                    case STATION_WRONG_PASSWORD: status_str = "failed"; break;
                    case STATION_NO_AP_FOUND: status_str = "failed"; break;
                    case STATION_CONNECT_FAIL: status_str = "failed"; break;
                }

                char json_str[54];
                os_sprintf(json_str, "{\"status\": \"%s\"}", status_str);
                send_json(pespconn, json_str);
            }

        }

        else if (os_strcmp(path, "/registration.json") == 0) {
            char *status_str;

            switch (registration_status) {
                case REGISTER_UNREGISTERED: status_str = "unregistered"; break;
                case REGISTER_REGISTERING: status_str = "registering"; break;
                case REGISTER_REGISTERED: status_str = "registered"; break;
                case REGISTER_FAILED: status_str = "failed"; break;
            }

            char json_str[54];
            os_sprintf(json_str, "{\"status\": \"%s\"}", status_str);
            send_json(pespconn, json_str);
        }

        // HTML pages

        else if (os_strcmp(path, "/read") == 0) {
            if (registration_status == REGISTER_REGISTERED) {
                char temp_json_str[128];
                char hum_json_str[128];
                measurement_json(temp_json_str, "temperature", "F", last_temp);
                measurement_json(hum_json_str, "humidity", "%", last_hum);
                char full_json_str[256] = "";
                strcat(full_json_str, temp_json_str);
                strcat(full_json_str, hum_json_str);
                full_json_str[os_strlen(temp_json_str)+os_strlen(hum_json_str)] = 0;
                send_ok_templated(pespconn, full_json_str);
            } else {
                send_ok_templated(pespconn, last_unknown);
            }

        }

        else if (os_strcmp(path, "/register") == 0) {
            send_ok_templated(pespconn, register_html);
        }

        else if (os_strcmp(path, "/scan.json") == 0) {
            char json_str[256] = "[";
            
            int si = 0;
            for (; si < n_scanned; si++) {
                char json_obj[100];
                os_sprintf(json_obj, "{\"ssid\": \"%s\", \"rssi\": %d}", scanned_stations[si], scanned_dbs[si]);
                os_strcat(json_str, json_obj);
                if (si < n_scanned - 1) {
                    os_strcat(json_str, ",");
                } else {
                    os_strcat(json_str, "]");
                }
            }

            send_json(pespconn, json_str);
        }

        else if (os_strcmp(path, "/") == 0) {
            send_ok_templated(pespconn, index_html);
        }

        else {
            send_404(pespconn);
        }

        return;
    }

    else if (POST) {

        // Parse JSON with jsmn
        jsmn_parser parser;
        jsmn_init(&parser);
        jsmntok_t tokens[32];
        jsmnerr_t r;
        r = jsmn_parse(&parser, body, 1024, tokens, 256);
        if (r < 0) {
            //print("JSON Parse error?");
            return;
        }

        // Look for ssid and pass
        char station_ssid[20];
        char station_pass[20];

        //print("JSON Parse success?");

        if (os_strcmp(path, "/connect.json") == 0) {
            // Parse ssid and pass from JSON
            int ti = 0;
            int has_ssid = 0;
            int has_pass = 0;
            int on_ssid = 0;
            int on_pass = 0;
            for(; tokens[ti].end; ti++) {
                char tv[256];
                token_string(tv, body, tokens[ti]);
                if (on_ssid) {
                    //print("Found ssid");
                    on_ssid = 0;
                    os_strcpy(station_ssid, tv);
                    has_ssid = 1;
                }
                if (on_pass) {
                    //print("Found pass");
                    on_pass = 0;
                    os_strcpy(station_pass, tv);
                    has_pass = 1;
                    if (has_ssid) { break; }
                }
                on_ssid = ti % 2 == 1 && os_strcmp(tv, "ssid") == 0;
                on_pass = ti % 2 == 1 && os_strcmp(tv, "pass") == 0;
            }

            //ets_uart_printf("Hopefully ssid=%s and pass=%s\r\n", station_ssid, station_pass);
            send_ok(pespconn, "<h1>maia</h1><p>OK</p>");
            setup_station(station_ssid, station_pass);
        }

        else if (os_strcmp(path, "/register.json") == 0) {
            // Parse email and password from JSON
            int ti = 0;
            char user_email[64];
            char user_password[64];
            int has_email = 0;
            int has_password = 0;
            int on_email = 0;
            int on_password = 0;
            for(; tokens[ti].end; ti++) {
                char tv[256];
                token_string(tv, body, tokens[ti]);
                if (on_email) {
                    //print("Found email");
                    on_email = 0;
                    os_strcpy(user_email, tv);
                    has_email = 1;
                }
                if (on_password) {
                    //print("Found password");
                    on_password = 0;
                    os_strcpy(user_password, tv);
                    has_password = 1;
                    if (has_email) { break; }
                }
                on_email = ti % 2 == 1 && os_strcmp(tv, "email") == 0;
                on_password = ti % 2 == 1 && os_strcmp(tv, "password") == 0;
            }

            char register_response[256];
            os_sprintf(register_response, "Registering as %d...", DEVICE_ID);
            send_ok_templated(pespconn, register_response);
            char register_json[256];
            os_sprintf(register_json, "{"
                "\"device_id\": \"0x%x\","
                "\"kind\": \"%s\","
                "\"email\": \"%s\","
                "\"password\": \"%s\""
            "}", DEVICE_ID, DEVICE_KIND, user_email, user_password);
            registration_status = REGISTER_REGISTERING;
            post_json(API_BASE "/devices.json", register_json);
        }

        else {
            send_404(pespconn);
        }

        return;
    }

    send_404(pespconn);
    return;
}