Beispiel #1
0
static void server_getByIdJSON(http_request_t request, socket_t* client, worker_t** workers)
{
    int id;
    char* getId = strpbrk(request.uri, "0123456");
    if(getId)
    {
        id = atoi(getId);
        if(id>6 || id<0 || worker_getId(workers[id]) == -1)
        {
            socket_write_string(client, "Wrong ID");
            return;
        }
    }
    else
    {
        server_notFound(client);
        return;
    }
    char buffer[1000] = "";
    char* worker = worker_makeWorkerJSON(workers[id]);

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

    socket_write_string(client, buffer);
    socket_close(client);
}
Beispiel #2
0
static char* server_getAllWorkersJSON(worker_t** workers)
{
    int count = worker_workersCount(workers);
    char all[2000] = "";
    for(int i = 0; i < MAX_WORKERS; i++)
    {
        if(worker_getId(workers[i]) != -1)
            strcat(all, worker_makeWorkerJSON(workers[i]));
    }
    //puts(all);
    return all;
}
Beispiel #3
0
static char* server_getAllWorkersJSON(db_t* base)
{
    char allOfTHem[4000] = "";
    int count = db_countWorkers(base);
    worker_t** workers[count];

    for(int i = 0; i<count; i++)
        workers[i] = worker_new();

    db_fillWorkerArr(base, workers);

    for(int i = 0; i < count; i++)
    {
        strcat(allOfTHem, worker_makeWorkerJSON(workers[i]));
    }

    for(int i = 0; i<count; i++)
        worker_free(workers[i]);

    return allOfTHem;
}
Beispiel #4
0
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);
}