Esempio n. 1
0
static void _reply_XML(Web_t self, HttpRequest_t httpRequest, socket_t * clientSock){
    const char * uri = HttpRequest_getURI(httpRequest);
    HTTP_REQUEST_METHOD method = HttpRequest_getMethod(httpRequest);

    char child1[128], child2[128];
    getTok(uri, 1, "/", child1);
    getTok(uri, 2, "/", child2);

    if (!strcmp(child1, "investors")) {
        if(!strcmp(child2, "")){
            if (method == HTTP_GET){
                char * charP = HttpRequest_getArgsVal(httpRequest, "gtinvestment");
                double minInvestment = !strcmp(charP, "[empty]") ? MIN_DOUBLE : atof(charP);

                charP = HttpRequest_getArgsVal(httpRequest, "ltinvestment");
                double maxInvestment = !strcmp(charP, "[empty]") ? MAX_DOUBLE : atof(charP);

                charP = HttpRequest_getArgsVal(httpRequest, "gtprojects");
                int minProjectsNumber = !strcmp(charP, "[empty]") ? MIN_INT : atoi(charP);

                charP = HttpRequest_getArgsVal(httpRequest, "ltprojects");
                int maxProjectsNumber = !strcmp(charP, "[empty]") ? MAX_INT : atoi(charP);

                _validateArgsVals(&minInvestment, &maxInvestment, &minProjectsNumber, &maxProjectsNumber);
                _writeInvestors_XML(self, clientSock, minInvestment, maxInvestment, minProjectsNumber, maxProjectsNumber);
            }else if (method == HTTP_POST){
                _addInvestorIfPossible(self, clientSock, httpRequest);
            }else{
               _replyInvalidMethod(clientSock);
            }
        }else if (isNonNegatInteger(child2)){
            int index = atoi(child2);

            if(_checkIndex(self, index) == SERVER_INDEX_ERROR){
                _replyInvalidIndex(clientSock, index);
            }else{
                if (method == HTTP_GET){
                    _writeInvestor_XML(self, clientSock, index);
                } else if(method == HTTP_OPTIONS){
                    _optionsOkReply(clientSock);
                } else if (method == HTTP_DELETE){
                    DataHandler_deleteInvestor(self->dataHandler, index);
                    _successDeleteReply(clientSock, index);
                }else{
                    _replyInvalidMethod(clientSock);
                }
            }
        }else{
            _replyInvalidUri(clientSock);
        }
    }else{
        _replyInvalidUri(clientSock);
    }
}
Esempio n. 2
0
static void _reply_HTML(server_t self, http_t httpRequest, socket_t * clientSock){
    const char * uri = http_getURI(httpRequest);
    HTTP_REQUEST_TYPE type = http_getType(httpRequest);

    char child0[128], child1[128];
    getTok(uri, 0, "/", child0);
    getTok(uri, 1, "/", child1);

    if (!strcmp(child0, "")) {
        if (type == HTTP_GET){
            _replyHomepage_HTML(self, clientSock);
        }else{
            _replyInvalidMethod(clientSock);
        }
    }else if (!strcmp(child0, "patients")){
        if(!strcmp(child1, "")){
            if (type == HTTP_GET){
                _writePatients_HTML(self, clientSock);
            }else if (type == HTTP_POST){
                _addPatientIfPossible(self, clientSock, httpRequest);
            }else{
               _replyInvalidMethod(clientSock);
            }
        }else if(isNonNegatInteger(child1)){
            int index = atoi(child1);

            if(_checkIndex(self, index) == SERVER_INDEX_ERROR){
                _replyInvalidIndex(clientSock, index);
            }else{
                if (type == HTTP_GET){
                    _writePatient_HTML(self, clientSock, index);
                }else if(type == HTTP_OPTIONS){
                    _optionsOkReply(clientSock);
                }else if (type == HTTP_DELETE){
                    DataHandler_deletePatient(self->dataHandler, index);
                    _successDeleteReply(clientSock, index);
                }else{
                   _replyInvalidMethod(clientSock);
                }
            }
        }else{
            _replyInvalidUri(clientSock);
        }
    }else if (!strcmp(child0, "new-patient")){
        if (type == HTTP_GET){
            _replyNewPatient_HTML(self, clientSock);
        }else{
            _replyInvalidMethod(clientSock);
        }
    }else{
        _replyInvalidUri(clientSock);
    }
}