コード例 #1
0
ファイル: server.c プロジェクト: LinaDovhaniuk/lina
void server_deletePupil(socket_t * client,list_t * l,int id, db_t * self){
    if(db_deletePupilById(self,l,id) == 1){
        cJSON *jResp = cJSON_CreateObject();
        cJSON_AddItemToObject(jResp, "status", cJSON_CreateString("Ok"));
        cJSON_AddItemToObject(jResp, "description", cJSON_CreateString("Pupil with this Id deleted"));
        server_sendJson(client,jResp);
    }
    else server_sendWrongIndex(client);
}
コード例 #2
0
ファイル: server.c プロジェクト: LinaDovhaniuk/lina
void server_sendWrongInputData(socket_t * client){
    cJSON *jResp = cJSON_CreateObject();
    cJSON_AddItemToObject(jResp, "status", cJSON_CreateString("Error"));
    cJSON_AddItemToObject(jResp, "description", cJSON_CreateString("Wrong input data"));
	server_sendJson(client, jResp);
}
コード例 #3
0
ファイル: server.c プロジェクト: LinaDovhaniuk/lina
void server_sendWrongIndex(socket_t * client){
    cJSON *jResp = cJSON_CreateObject();
    cJSON_AddItemToObject(jResp, "status", cJSON_CreateString("Error"));
    cJSON_AddItemToObject(jResp, "description", cJSON_CreateString("Pupil with this Id does not exist"));
	server_sendJson(client, jResp);
}
コード例 #4
0
ファイル: server.c プロジェクト: Blackstee/Blackstee
void server_musicianById(socket_t * client, http_request_t * req, const char * pathBuf)
{
    const char * pattern = "/musicians/";
    char * c = NULL;
    const char * Text = " \0";
    char strbuf[10240];
    error_t * err = error_t_new ();
    int count = get_size_mus_arr ();
    long musIndex = strtol(pathBuf + strlen(pattern), &c, 10);
    if (musIndex < 0 || musIndex >= count) {
        error_t_add_err(err, NULL, "Wrong Index of musician");
        server_sendJson(client, &err);
        return;
    }
    if (strcmp (req->method, "GET") == 0)
    {
        const char * musJson = musician_toJSON(&musicians_arr[musIndex]);
        sprintf(strbuf,
        "HTTP/1.1 200 OK\n"
        "Content-Type: application/json\n"
        "Content-Length: %zu\n"
        "Connection: keep-alive\n"
        "\n%s", strlen(musJson), musJson);
        free(musJson);
    }
    else
        if (strcmp (req->method, "POST") == 0)
    {
        musicians_arr[musIndex] = musician_empty();
        musicians_arr[musIndex].id = musIndex;
        const char * str = http_request_getArg(req, "name");
        if (strlen(str) > 0 && strlen (str) < 20)
        {
            musicians_arr[musIndex].name = str;
            str = http_request_getArg(req, "surname");
            if (strlen(str) > 0 && strlen (str) < 20)
             {
                musicians_arr[musIndex].surname = str;
                str = http_request_getArg (req, "birthdate");
                if (strlen(str) > 0)
                {
                    if (atoi(str) > 1800 && atoi(str) < 2000)
                    {
                        char text [10] = " ";
                        strcpy(text, str);
                        char * string = strtok(text, "-");
                        string  = strtok(NULL, "-");
                        if (atoi(string) > 0 && atoi (string) < 32)
                        {
                           string = strtok (NULL, "-");
                           if (atoi(string) > 0 && atoi (string) < 13)
                              {
                                  musicians_arr [musIndex].birthdate = http_request_getArg(req, "birthdate");
                                  str = http_request_getArg(req, "year");
                                  if (strlen(str) > 0 && atoi(str) > 1850 && atoi (str) < 2016)
                                   {
                                       musicians_arr[musIndex].year = atoi(str);
                                       str = http_request_getArg (req, "rating");
                                       if (strlen(str) > 0 && atof (str) >= 0.0 && atof (str) <= 5.0)
                                       {
                                          musicians_arr[musIndex].rating = atof (str);
                                          str = http_request_getArg(req, "style");
                                          if (strlen(str) > 0 && strlen (str) < 20)
                                          {
                                              musicians_arr[musIndex].band.style = str;
                                              str = http_request_getArg(req, "role");
                                              if (strlen(str) > 0 && strlen (str) < 20)
                                            {
                                                   musicians_arr[musIndex].band.role = str;
                                                   Text = musician_toJSON(&musicians_arr);
                                                   printf("New musician:\n%s\n", Text);
                                                   sprintf(strbuf,
                                                   "HTTP/1.1 200 OK\n"
                                                   "Content-Type: application/json\n"
                                                   "Content-Length: %zu\n"
                                                   "Connection: keep-alive\n"
                                                   "\n%s", strlen(Text), Text);
                                                   free(Text);
                                            } else
                                            {
                                                error_t_add_err(err, NULL, "Wrong role");
                                                server_sendJson(client, &err);
                                            }
                                          } else
                                            {
                                                error_t_add_err(err, NULL, "Wrong style");
                                                server_sendJson(client, &err);
                                            }
                                       } else
                                        {
                                                error_t_add_err(err, NULL, "Wrong rating");
                                                server_sendJson(client, &err);
                                        }
                                   } else
                                       {
                                                error_t_add_err(err, NULL, "Wrong year of death");
                                                server_sendJson(client, &err);
                                       }
                              } else
                               {
                                                error_t_add_err(err, NULL, "Wrong month of birthdate");
                                                server_sendJson(client, &err);
                                }
                         } else
                         {
                                                error_t_add_err(err, NULL, "Wrong day of birthdate");
                                                server_sendJson(client, &err);
                         }
                    } else
                    {
                                                error_t_add_err(err, NULL, "Wrong year of birthdate");
                                                server_sendJson(client, &err);
                    }
                 } else
                 {
                                                error_t_add_err(err, NULL, "Wrong birthdate ");
                                                server_sendJson(client, &err);
                  }
              } else
                 {
                                                error_t_add_err(err, NULL, "Wrong surname");
                                                server_sendJson(client, &err);
                 }
            } else
            {
                                                error_t_add_err(err, NULL, "Wrong name");
                                                server_sendJson(client, &err);
            }
    } else
     {
        if (strcmp(req->method, "DELETE") == 0)
            {
                 musicians_arr[musIndex] = musician_empty();
		         musicians_arr[musIndex].id = musIndex;
		         if (musicians_arr[musIndex].year == 0)
		         error_t_add_err(err, NULL, "Deleted successfully, no errors");
		         else error_t_add_err (err, NULL, "Deleted unsuccessfully, Error");
                 server_sendJson(client, &err);
        	}
        	else
            {
                error_t_add_err(err, NULL, "Wrong method");
                server_sendJson(client, &err);
            }
    }
    socket_write_string(client, strbuf);
    socket_close(client);
}
コード例 #5
0
ファイル: main.c プロジェクト: LinaDovhaniuk/lina
int main(){
    lib_init();
    socket_t * server = socket_new();
    socket_bind(server, 5000);
    socket_listen(server);

    char buffer[10000];
    socket_t * client = NULL;

    list_t * pupils = list_new();
    char jsonText[10000];
    FILE * fp = fopen("pupil.json","r");
    fread(jsonText,sizeof(char),10000,fp);
    pupil_createPupilsList(pupils,cJSON_Parse(jsonText));

    while (1) {
		socket_t * client = socket_accept(server);
		socket_read(client, buffer, sizeof(buffer));
		printf("%s", buffer);
		server_request_t request = server_request_parse(buffer);
		if (strcmp(request.method, "GET") == 0){
            if(strcmp(request.uri,"/")==0){
                server_openPage(client);
            }
			else if (strcmp(request.uri,"/api/pupils")==0){
                server_sendJson(client,pupil_listToJson(pupils));
			}
			else if(strncmp(request.uri,"/api/pupils/",strlen("/api/pupils/"))==0){
			    int id;
			    int num = sscanf(request.uri, "/api/pupils/%d", &id);
                if(num != 0){
                        pupil_t * p = server_getPupilById(pupils,id);
                        if(p == NULL){
                            server_sendWrongIndex(client);

                        } else
                        server_sendJson(client,pupil_pupilToJson(p));
                }
			}
			else if(strcmp(request.uri, "/pupils")==0){
                server_sendListAsHtml(client,pupils);
			}
			else if(strncmp(request.uri,"/pupils/",strlen("/pupils/"))==0){
			    int id;
			    int num = sscanf(request.uri, "/pupils/%d", &id);
                if(num != 0){
                        pupil_t * p = server_getPupilById(pupils,id);
                        if(p == NULL){
                            server_wrongIndex_html(client);

                        } else
                        server_sendHtml(client,pupil_getHtml(p,pupil_getId(p)));
                }
			}
			else if(strcmp(request.uri, "/new-pupil")==0){
                server_addPupilHtml(client,pupils);
			}
		}
		else if (strcmp(request.method, "DELETE") == 0){
            if (strstr(request.uri, "/api/pupils/") != NULL){
				int id;
				int isNumber = sscanf(request.uri, "/api/pupils/%d", &id);
				if (isNumber){
                    server_deletePupil(client,pupils, id);
				}
				else{
					server_sendWrongIndex(client);
				}
            }
            else if (strstr(request.uri,"/pupils/")!= NULL){
                int Id;
                int num = sscanf(request.uri,"/pupils/%d", &Id);
                if(num){
                    server_deletePupil_html(client,pupils,Id);
                }
                else {
                    server_wrongIndex_html(client);
                }
            }
		}
		else if (strcmp(request.method, "POST") == 0){
            if (strcmp(request.uri,"/api/pupils")==0) {
                server_addPupil(client,pupils,buffer);
            }
            else if (strcmp(request.uri,"/pupils")==0) {
                server_newHtmlPupil(client,pupils,buffer);
            }
		}

		socket_free(client);

	}


    socket_free(client);
    socket_free(server);
    list_free(pupils);
    lib_free();
    return 0;
}