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); }
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); }
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_students(socket_t * client, http_request_t * req) { char strbuf[10240]; if (strcmp(req->method, "GET") == 0) { char allStudentsJson[10240] = ""; char buf[1000]; list_t persons = list_new(); FILE * file = fopen("Untitled1.xml", "r"); while(fgets(buf, 1000, file) != NULL) { strcat(allStudentsJson, buf); puts(buf); } db_t * db = db_new("worker.db"); for(int i = 1; i < 6; i++){ struct person * pers = malloc(sizeof(struct person)); pers = db_getWorkerById(db, i); list_add(persons, pers, list_size(persons)); } char xml_buf[10000]; strcpy(xml_buf, list_to_xml_string(persons)); //list_to_xml(persons); puts(xml_buf); sprintf(strbuf, "HTTP/1.1 200 OK\n" "Content-Type: text/xml\n" "Content-Length: %u\n" "Connection: keep-alive\n" "\n%s", strlen(xml_buf), xml_buf); fclose(file); } socket_write_string(client, strbuf); socket_close(client); }
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); }