void skip_headers(FILE * fichier_client){ char data[2048]; fgets_or_exit(data,2048,fichier_client); while(!(data[0]=='\n' || (data[0]=='\r' && data[1]=='\n'))){ fgets_or_exit(data,2048,fichier_client); printf(data); } }
int check_client_header(FILE *file) { char buffer[1024]; http_request request; fgets_or_exit(buffer, 1024, file); if (parse_http_request(buffer, &request) == 1) { char *url = rewrite_url(request.url); if (request.method != HTTP_GET) { skip_headers(file); send_response(file, 400, "Bad request", "Bad request\r\n"); free(url); return 400; } else if (strcmp(url, "/")) { skip_headers(file); send_response(file, 404, "Not Found", "Not Found\r\n"); free(url); return 404; } else { skip_headers(file); free(url); return 200; } } else { skip_headers(file); send_response(file, 400, "Bad Request", "Bad request\r\n"); return 400; } }
int main (void) { int socket_serveur = creer_serveur(8080); initialiser_signaux(); int statut; while (1) { int socket_client = accept ( socket_serveur , NULL , NULL ); if ( socket_client == -1){ perror ( " accept " ); /* traitement d ’ erreur */ } int pid; if ((pid = fork()) == 0) { char *buf = malloc(255); /* On peut maintenant dialoguer avec le client */ //sleep(1); /*const char * message_bienvenue = "Mesdames, messieurs,\n client, cliente\nJe suis heureux de vous annoncer que moi, serveur, s'est connecté à vous meme.\nPuisse le lien qui nous unis etre aussi fort que l'amour liant Mr java et Madame Eclipse.\nCe message sera envoyé tant que vous me recevrez, tant que vous m'ecouterez de votre oreille attentive...\nPaix et amour, guerre et haine, ne sont que futilitées, nous vivons notre passion comme un arbre perds ses feuilles en automne...\nVive les programmes ! Pro comme professionel, gramme comme le peu qu'il nous faut pour nous faire plaisir. Ajoutez a ces éléments, un peu de feuille marron et blanche afin d'obtenir un moment de paix et d'armonie autour de vous.\nA tout de suite pour un nouveau message !\n" ; write ( socket_client , message_bienvenue , strlen ( message_bienvenue ));*/ FILE *f = fdopen(socket_client, "w+"); fgets_or_exit(buf,255,f); /*write ( 1 , buf , strlen (buf)); write (socket_client, buf, strlen(buf));*/ free(buf); fclose(f); return 1; } printf("connexion acceptée\n") ; if (waitpid(pid, &statut, WNOHANG) == -1) perror("erreur waitpid"); close(socket_client); } return 0; }
void skip_headers( FILE * f) { char str[1024]; while (1){ fgets_or_exit(str, sizeof(str), f); if ((str[0] == '\r' && str[1] == '\n') || str[0] =='\n') break; } }
void skip_headers(FILE *file){ char buffer[1024]; while (1) { fgets_or_exit(buffer, 1024, file); if (strcmp(buffer, "\r\n") == 0 || strcmp(buffer, "\n") == 0) { return; } } }
int main() { char buf[BUF_SIZE]; int socket_serveur, socket_client; if((socket_serveur = creer_serveur(8080)) == EXIT_FAILURE) { return EXIT_FAILURE; } initialiser_signaux(); const char * message_bienvenue = "Bonjour, bienvenue sur le serveur\nle plus parfait du monde.\nSur votre droite, vous pourrez voir\nrien qui n\'est plus parfait que\nle serveur qui defie la perfection.\nSur votre gauche, pareil.\nNenufar.\nOgnon.\r\n"; while (1) { socket_client = accept(socket_serveur, NULL, NULL); if (socket_client == -1) { perror("accept"); return EXIT_FAILURE; } FILE *fsocket = fdopen(socket_client, "w+"); switch (fork()) { case -1: perror("fork"); return EXIT_FAILURE; case 0: /* On peut maintenant dialoguer avec le client */ fgets_or_exit(buf, BUF_SIZE, fsocket); http_request request; int bad_request = parse_http_request(buf, &request); skip_header(fsocket); if (bad_request == 0) { send_response(fsocket, 400, "Bad Request", "Bad Request\r\n"); } else if (request.method == HTTP_UNSUPPORTED) { send_response(fsocket, 405, "Method not allowed", "Method not allowed\r\n"); } else if (strcmp(request.url, "/") == 0) { send_response(fsocket, 200, "OK", message_bienvenue); } else { send_response(fsocket, 404, "Not Found", "Not Found\r\n"); } fclose(fsocket); return EXIT_SUCCESS; default: fclose(fsocket); close(socket_client); } } return EXIT_SUCCESS; }
void skip_headers(FILE *client){ char cumul[1024]; while(cumul[0] != '\r' && cumul[1] != '\n') { fgets_or_exit(cumul, sizeof(cumul), client); } }
void skip_headers(FILE *client) { char buf[BUFFER_SIZE]; while((fgets_or_exit(buf, sizeof(buf), client) != NULL && strcmp(buf, "\n") && strcmp(buf, "\r\n") != 0)); }
int main(){ int taille=0; int socket_client, pid, erreur = 0; char str[1024]; http_request req; FILE *f = NULL; const char *message_bienvenue = "Bonjour, c'est un grand honneur de vous avoir sur notre serveur. Bienvenue à bord !\n"; int socket_serveur = creer_serveur(8080); message_bienvenue +=0; taille+=0; initialiser_signaux(); while((socket_client = accept(socket_serveur , NULL , NULL)) != -1){ if((pid = fork()) == -1){ perror("Erreur fork"); /* traitement d’erreur */ } if(pid == 0){ sleep(1); f = fdopen(socket_client, "w+"); /* if(write(socket_client, message_bienvenue, strlen(message_bienvenue)) == -1){ perror("Erreur write bienvenu"); traitement d’erreur }*/ fgets_or_exit(str, sizeof(str), f); skip_headers(f); erreur=parse_http_request(str,&req); //erreur = 1; /*strcpy(s, str); char* token = strtok(s, " "); while (token) { tokenCounter++; if(tokenCounter == 1 && !strcmp(token,"GET") == 0){ erreur = 0; } if(tokenCounter ==2 && !strcmp(token, "/") ==0){ erreur=0; } if(tokenCounter == 3 && (strncmp(token, "HTTP/1.1",8) != 0 && strncmp(token, "HTTP/1.0",8) !=0)){ erreur = 0; } if(tokenCounter >=4){ printf("%s\n", token); } token = strtok(NULL, " "); }*/ if(erreur == 404){ send_response(f,404,"Page not found","Page not found\r\n"); }else if(erreur == 400){ send_response(f,400,"Bad Request","Bad Request\r\n"); }else{ fprintf(f, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s\r\n", (int)strlen(message_bienvenue)+2, message_bienvenue); } fflush(f); fclose(f); /*while((taille = read(socket_client, buffer, 256)) != 0){ if(write(socket_client, buffer, taille)== -1){ perror("Erreur write retour de message"); } }*/ exit(EXIT_SUCCESS); }else{ close(socket_client); } } return 1; }
int init_socket_tcp(int port){ //IpV4 - TCP Socket int server_socket = socket(AF_INET, SOCK_STREAM, 0); int server_init_state = -1; if (server_socket == (-1)){ //Error while creating tcp socket perror("init_socket_tcp"); //Cancelling next operations return server_socket; } //Binding socket if ((server_init_state = bind_socket_tcp(server_socket, port)) == -1) //Cancelling next operations return server_init_state; //Listening if ((server_init_state = listen_socket_tcp(server_socket)) == -1) //Cancelling next operations return server_init_state; //LOG struct tm * timeinfo; timeinfo = time_get_time(); printf("[%d:%d:%d] Server started on port %d\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, port); //Accepting connections (Server loop process) if (!fork()){ //Init signals init_signals(); //Clients accept() loop while(1){ //Client information struct sockaddr_in client_addr; //Accepting client int client_socket = accept_socket_tcp(server_socket, &client_addr); if (client_socket != (-1)){ //LOG _client_idata client_information; get_client_information(&client_addr, &client_information); printf("Request from [%s:%d] !\n", client_information.client_ip, client_information.client_port); //New instance for client if(!fork()){ //Client pipe FILE * clnt_sd = fdopen(client_socket, "w+"); //Receive buffer char rcv_buff[SERVER_RECEIVE_BUFFER_SIZE]; //HTTP header http_request http_req; //Header check vars int header_valid = 0, url_valid = 0; //Reading client request fgets_or_exit(rcv_buff, sizeof(rcv_buff), clnt_sd); //Parsing http header_valid = parse_http_request(rcv_buff, &http_req); //Checking header header_valid = (((http_req.method == HTTP_GET) && (http_req.major_version == 1) && ((http_req.minor_version == 1) || (http_req.minor_version = 0))) ? 1 : 0); //Valid URL url_valid = ((strcmp(http_req.url,"/") == 0) ? 1 : 0); //Skipping headers skip_headers(clnt_sd); //Header validation if (http_req.method == HTTP_UNSUPPORTED) //Method not supported send_response (clnt_sd, 405, "Method Not Allowed", "Method Not Allowed\r\n"); else if ((header_valid == 1) && (url_valid == 1)) //Welcome message send_response(clnt_sd, 200 , "OK" , SERVER_WELCOME_MSG ); else if ((header_valid == 1) && (url_valid == 0)) //404 send_response(clnt_sd, 404,"Not Found", "Not Found\r\n"); else //Bad request send_response(clnt_sd, 400, "Bad Request" , "Bad request\r\n"); //Disconnected fclose(clnt_sd); //Client child-process ending exit(0); } //Closing client socket close(client_socket); } } } //Success return server_socket; }