예제 #1
0
파일: server.c 프로젝트: gribo4eg/repos1
static void server_getByIdHTML(http_request_t request, socket_t* client, db_t* base)
{
    int id;
    char* getId = strpbrk(request.uri, "0123456789");
    if(getId)
    {
        id = atoi(getId);
        if(id<=0 || !db_checkId(base, id))
        {
            socket_write_string(client, "<h1>Wrong ID</h1><p><a href=\"/workers/\">All workers</a></p>");
            return;
        }
    }
    else
    {
        server_notFound(client);
        return;
    }
    char toSend[2000];
    char buffer[2000] = "<head><title>Worker</title></head><h1>Worker</h1><p><a href=\"/workers/\">All workers</a></p><p>";
    worker_t* worker = db_getWorkerById(base, id);
    strcat(buffer, server_getWorkerHTML(worker));
    strcat(buffer, "</p>");

    sprintf(toSend,
            "HTTP/1.1 200 OK\n"
            "Content-Type: text/html\n"
            "Content-Length: %i\r\n\r\n"
            "\n%s", strlen(buffer), buffer);
    socket_write_string(client, toSend);
    worker_free(worker);
    socket_close(client);
}
void server_DELETE_id(http_request_t req, socket_t * clientSocket, sqlite3* db) {
    char buf[1000] = "";
    int id;

    if(strpbrk(req.uri,"-0123456789"))
    {
        id = atoi(strpbrk(req.uri,"-0123456789"));
        if(!db_checkId(db, id))
        {
            socket_write_string(clientSocket,"HTTP1.1 404 NOT FOUND\n"
            "Content-Type: json\n"
            "Content-Length: %i\r\n\r\n"
            "%s\n""<body>Id not found<br>"
                                  "<a href=\"/Teacher/\">To Teachers</a></body>");
            return;
        }
    }
    else
    {
        socket_write_string(clientSocket,"<body>Wrong id<br>"
                                  "<a href=\"/Teacher/\">To Teachers</a></body>");
        return;
    }
    db_deleteTeacherById (db, id);




    server_send(clientSocket, "<body>Delete success<br>"
                                  "<a href=\"/Teacher/\">To Teachers</a></body>");
}
void server_GET_id_html(http_request_t req, socket_t* clientSocket, sqlite3 * db) {
    int id;

    if(strpbrk(req.uri,"-0123456789"))
    {
        id = atoi(strpbrk(req.uri,"-0123456789"));
        if (!db_checkId (db, id))
        {
            socket_write_string(clientSocket,"Id not found");
            return;
        }
    }
    else
    {
        socket_write_string(clientSocket,"Wrong id");
        return;
    }
    char text[1000] = "";
    char buf[10000] = "<html>"
                    "<head>"
                    "<title>Lab4</title>"
                    "</head>"
                    "<body>"
                    "<h1>Teacher</h1>";

    teacher_t temp = db_getTeacherById(db, id);
    sprintf(text,"<p>Name:\t\t%s</p>"
            "<p>Birthdate:\t%s</p>"
            "<p>Years:\t\t%i</p>"
            "<p>Rate:\t\t%.2f</p>"
            "<p>\tSubjects:\t\t%i\n\n\n</p>"
            "<p><a href=""/Teacher/"">To prev page</a></p>",
            teacher_getName(temp),
            teacher_getBirthdate(temp),
            teacher_getYears(temp),
            teacher_getRate(temp),
            teacher_getSubjects(temp)
           );
    strcat(buf, text);
    sprintf(text, "<p><a href=\"/Teacher/\" onclick=\"doDelete()\">Delete Teacher</a></p>"
                 "<script>"
                 "function doDelete(){"
                 "var xhttp=new XMLHttpRequest();"
                 "xhttp.open(\"DELETE\",\"/api/Teacher/%i\",true);"
                 "xhttp.send();"
                 "}"
                 "</script>", id);
    strcat(buf, text);
    strcat(buf, "</body>"
           "</html>");
    socket_write_string(clientSocket, buf);
}
예제 #4
0
파일: server.c 프로젝트: gribo4eg/repos1
static void server_deleteByIdHTML(http_request_t request, socket_t* client, db_t* base)
{
    int id;
    int count = db_countWorkers(base);
    char* getId = strpbrk(request.uri, "0123456789");
    if(getId)
    {
        id = atoi(getId);
        if(id<=0 || !db_checkId(base, id))
        {
            socket_write_string(client, "<h1>Wrong ID</h1><p><a href=\"/workers/\">All workers</a></p>");
            return;
        }
    }
    else
    {
        server_notFound(client);
        return;
    }
    worker_t* worker = db_getWorkerById(base, id);
    char toSend[2000];
    char buffer[2000];
    sprintf(buffer,
            "<head><title>Delete</title></head>"
            "<h1>Success</h1><p><a href=\"/workers/\">All workers</a></p>"
            "<p>Worker %s successfully deleted!</p>", worker_getName(worker));

    db_deleteWorker(base, id);

    sprintf(toSend,
            "HTTP/1.1 200 OK\n"
            "Content-Type: text/html\n"
            "Content-Length: %i\r\n\r\n"
            "\n%s", strlen(buffer), buffer);

    socket_write_string(client, toSend);
    socket_close(client);
}
예제 #5
0
파일: server.c 프로젝트: gribo4eg/repos1
static void server_getByIdJSON(http_request_t request, socket_t* client, db_t* base)
{
    int id;
    char* getId = strpbrk(request.uri, "0123456789");
    if(getId)
    {
        id = atoi(getId);
        if(id <= 0 || !db_checkId(base, id))
        {
            socket_write_string(client, "Wrong ID");
            return;
        }
    }
    else
    {
        server_notFound(client);
        return;
    }
    char buffer[1000] = "";

    worker_t* worker = db_getWorkerById(base, id);
    char* workerJSON = worker_makeWorkerJSON(worker);
    if(workerJSON == NULL)
    {
        socket_write_string(client, "Wrong ID");
        return;
    }

    sprintf(buffer,
            "HTTP/1.1 200 OK\n"
            "Content-Type: application/json\n"
            "Content-Length: %i\r\n\r\n"
            "\n%s", strlen(workerJSON)+1, workerJSON);

    socket_write_string(client, buffer);
    worker_free(worker);
    socket_close(client);
}
void server_GET_id(http_request_t req, socket_t * clientSocket, sqlite3 * db) {
    char text[1000] = "";
    char * buf = NULL;
    int id, count = db_countTeachers(db);

    if(strpbrk(req.uri, "-0123456789")) {
        id = atoi(strpbrk(req.uri, "-0123456789"));
        if(!db_checkId (db, id)) {
            socket_write_string(clientSocket,"HTTP1.1 404 NOT FOUND\n"
            "Content-Type: json\n"
            "Content-Length: %i\r\n\r\n"
            "Id not found");
            return;
        }
    }
    else {
        socket_write_string(clientSocket,"Wrong id");
        return;
    }
    buf = teacher_toJSON(db_getTeacherById(db, id));
    strcat(text, buf);
    server_send(clientSocket, text);
}
예제 #7
0
파일: server.c 프로젝트: gribo4eg/repos1
static void server_deleteByIdJSON(http_request_t request, socket_t* client, db_t* base)
{
    int id;
    char* getId = strpbrk(request.uri, "0123456789");
    if(getId)
    {
        id = atoi(getId);
        if(id<=0 || !db_checkId(base, id))
        {
            socket_write_string(client, "{\nWrong ID\n}");
            return;
        }
    }
    else
    {
        server_notFound(client);
        return;
    }
    worker_t* worker = db_getWorkerById(base, id);
    char toSend[2000];
    char buffer[2000];
    sprintf(buffer,
            "Success!"
            "Worker %s successfully deleted!", worker_getName(worker));

    db_deleteWorker(base, id);

    sprintf(toSend,
            "HTTP/1.1 200 OK\n"
            "Content-Type: application/json\n"
            "Content-Length: %i\r\n\r\n"
            "\n%s", strlen(buffer), buffer);

    socket_write_string(client, toSend);
    socket_close(client);
}