int WebServer_process_request(int socket, char* method, unsigned char method_size, char* filename, unsigned char filename_size, 
                              char* content, unsigned char content_size, char* auth, unsigned char auth_size){

    #define STATE_GET_METHOD    0
    #define STATE_GET_FILENAME  1
    #define STATE_GET_CONTENT   2

    unsigned long tmr = 0;
    char data = 0;
    char content_head[4];

    // A string fifo for parsing purpose.
    char str_fifo[50];
    int content_length = 0;

    unsigned char state = 0;

    char data_pool[100];
    int ret = 0;
    int i;

    unsigned char method_ptr = 0;
    unsigned char filename_ptr = 0;
    unsigned char content_ptr = 0;
    unsigned char auth_ptr = 0;

    memset(content_head, 0, sizeof(content_head));

    memset(str_fifo, ' ', sizeof(str_fifo));

    tmr = millis() + WEBSERVER_REQUEST_TIMEOUT;

    while(millis() < tmr){
        //if(WebServer_request_available(socket) == 0){
            // Get char
            //WebServer_get_request(socket, &data, 1);
            memset(data_pool, 0, sizeof(data_pool));
            ret = WebServer_get_request(socket, data_pool, sizeof(data_pool));
            if(ret > 0){
                tmr = millis() + WEBSERVER_REQUEST_TIMEOUT;
                for(i = 0; i < ret; i++){
                    data = data_pool[i];
                    // Check state
                    switch(state){

                        case STATE_GET_METHOD:
                            if(data != ' '){
                                if(method_ptr < method_size){
                                    method[method_ptr] = data;
                                    method_ptr++;
                                }
                            }
                            else{
                                // Get blank
                                state = STATE_GET_FILENAME;
                            }
                        break;

                        case STATE_GET_FILENAME:
                            if(data != ' '){
                                if(filename_ptr < filename_size){
                                    filename[filename_ptr] = data;
                                    filename_ptr++;
                                }
                            }
                            else{
                                // Get blank, change state
                                state = STATE_GET_CONTENT;
                            }
                        break;

                        case STATE_GET_CONTENT:

                            str_fifo[(sizeof(str_fifo) - 1)] = data;

                            if((str_fifo[sizeof(str_fifo) - 2] == '\r') && (str_fifo[sizeof(str_fifo) - 1] == '\n')){
                                str_fifo[sizeof(str_fifo) - 2] = 0;
                                str_fifo[sizeof(str_fifo) - 1] = 0;
                                // Get line, parsing header.
                                char* str = 0;
                                // Parse http status;
                                
                                str = strstr(str_fifo, "Authorization: Basic ");
                                if(str != 0){
                                    sscanf(str, "Authorization: Basic %s", auth);
                                }
                                
                                str = strstr_P(str_fifo, PSTR("Content-Length:"));
                                if(str != 0){
                                    sscanf(str, "Content-Length:%d", &content_length);
                                }
                                memset(str_fifo, ' ', sizeof(str_fifo));
                            }
                            else{
                                // Read line
                                memcpy(&str_fifo[0], &str_fifo[1], (sizeof(str_fifo) - 1));
                            }

                            if((content_head[0] == '\r') && (content_head[1] == '\n') && (content_head[2] == '\r') && (content_head[3] == '\n')){
                                
                                if((content_ptr < content_size) && (content_ptr < content_length)){
                                    content[content_ptr] = data;
                                    content_ptr++;
                                }
                                else{
                                    return 0;
                                }
                            }else{
                                memcpy(content_head, &content_head[1], (sizeof(content_head) - 1));
                                content_head[3] = data;
                            }
                        break;

                        default:
                        break;
                    }

                }
            }
            else if(ret < 0) return ret;
        //}
    }
    return 0;
}
Esempio n. 2
0
int WebServer_process_request(int socket, char* method, unsigned char method_size, char* filename, unsigned char filename_size, 
                              char* content, unsigned char content_size, char* auth, unsigned char auth_size){

    #define STATE_GET_METHOD    0
    #define STATE_GET_FILENAME  1
    #define STATE_GET_CONTENT   2

    unsigned long tmr = 0;
    char data = 0;
    char content_head[4];
    char auth_head[15];
    unsigned char state = 0;

    char data_pool[100];
    int ret = 0;
    int i;

    unsigned char method_ptr = 0;
    unsigned char filename_ptr = 0;
    unsigned char content_ptr = 0;
    unsigned char auth_ptr = 0;

    memset(content_head, 0, sizeof(content_head));
    memset(auth_head, 0, sizeof(auth_head));


    tmr = millis() + WEBSERVER_REQUEST_TIMEOUT;

    while(millis() < tmr){
        //if(WebServer_request_available(socket) == 0){
            // Get char
            //WebServer_get_request(socket, &data, 1);
            ret = WebServer_get_request(socket, data_pool, sizeof(data_pool));
            if(ret > 0){
                tmr = millis() + WEBSERVER_REQUEST_TIMEOUT;
                for(i = 0; i < ret; i++){
                    data = data_pool[i];

                    // Find Authorization.
                    if(strncmp(auth_head, "Authorization:", 14) == 0){
                        if(auth_ptr < auth_size){

                            //Check \r\n or \n\r
                            if( ((data == '\r') && (auth[(auth_ptr - 1)] == '\n')) ||
                                ((data == '\n') && (auth[(auth_ptr - 1)] == '\r')) ){
                                auth[(auth_ptr - 1)] = 0;
                                memset(auth_head, 0, sizeof(auth_head));
                            }
                            else{
                                auth[auth_ptr] = data;
                                auth_ptr++;
                            }
                        }
                        else{
                            // Set auth_head to 0 to exit if condition
                            memset(auth_head, 0, sizeof(auth_head));
                        }
                    }
                    else{
                        memcpy(auth_head, &auth_head[1], (sizeof(auth_head) - 1));
                        auth_head[14] = data;
                    }

                    // Check state
                    switch(state){

                        case STATE_GET_METHOD:
                            if(data != ' '){
                                if(method_ptr < method_size){
                                    method[method_ptr] = data;
                                    method_ptr++;
                                }
                            }
                            else{
                                // Get blank
                                state = STATE_GET_FILENAME;
                            }
                        break;

                        case STATE_GET_FILENAME:
                            if(data != ' '){
                                if(filename_ptr < filename_size){
                                    filename[filename_ptr] = data;
                                    filename_ptr++;
                                }
                            }
                            else{
                                // Get blank, change state
                                state = STATE_GET_CONTENT;
                            }
                        break;

                        case STATE_GET_CONTENT:
                            if((content_head[0] == '\r') && (content_head[1] == '\n') && (content_head[2] == '\r') && (content_head[3] == '\n')){
                                if(content_ptr < content_size){
                                    content[content_ptr] = data;
                                    content_ptr++;
                                }
                            }else{
                                memcpy(content_head, &content_head[1], (sizeof(content_head) - 1));
                                content_head[3] = data;
                            }
                        break;

                        default:
                        break;
                    }

                }
            }
        //}
    }
    return 0;
}