Skip to content
Snippets Groups Projects
Commit e3d1e54b authored by Romain Gabet's avatar Romain Gabet :speech_balloon:
Browse files

site web ok

parent 788ad4f8
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
File deleted
No preview for this file type
File deleted
......@@ -50,7 +50,7 @@ FILE* check_and_open(const char* target, const char *document_root){
char* rewrite_target(char *target){
char* ret = strrchr(target, '?');
printf("rewrite target ret : %s\n",ret);
// printf("rewrite target ret : %s\n",ret);
if(ret != NULL){
char* rep = malloc(strlen(target)*sizeof(char));
size_t i = (int)(strlen(target)-strlen(ret));
......@@ -58,8 +58,8 @@ char* rewrite_target(char *target){
return rep;
}else{
char* rep = malloc(strlen(target)*sizeof(char));
size_t i = (int)(strlen(target));
rep = strncpy(rep,target+1,i);
size_t i = (int)(strlen(target));
rep = strncpy(rep,target+1,i);
return rep;
}
}
......@@ -110,7 +110,7 @@ void send_response(FILE *client, int code, const char *reason_phrase, const char
int traitementClient(int socket_client){
/* On peut maintenant dialoguer avec le client */
char *motd="Votre requète est bonne";
// char *motd="Votre requète est bonne";
printf("test\n");
char str[8000];
http_request request;
......@@ -130,28 +130,29 @@ int traitementClient(int socket_client){
}else{
send_response(fd, 400, "Bad Request", "Bad request");
}
}else if (strcmp(request.target, "/") == 0){
send_response(fd, 200, "OK", motd);
}else if (strcmp(request.target, "/html") == 0){
//send_response(fd, 200, "OK", motd);
//---
char* absolute_path = rewrite_target(request.target);
// printf("test - - - - - - - - %s\n",absolute_path);
FILE* fichier = check_and_open(absolute_path, "index.html");
if(fichier == NULL){
send_response(fd, 404, "Not Found", "Not Found");
}else{
int size = get_file_size(fileno(fichier));
send_status(fd, 200, "OK");
fprintf(fd,"Content-Lenght : %d\r\n\r\n",size);
if(copy(fichier,fd)==-1){
//error
}
}
}else{
send_response(fd, 404, "Not Found", "Not Found");
}
fflush(fd);
//partie 7
char* absolute_path = rewrite_target(request.target);
printf("test - - - - - - - - %s\n",absolute_path);
FILE* fichier = check_and_open(absolute_path, "document.txt");
fprintf(fd,"fichier : \n");
if(fichier == NULL){
send_response(fd, 404, "Not Found2", "Not Found2");
}else{
int size = get_file_size(fileno(fichier));
if(copy(fichier,fd)==-1){
//error
}
fprintf(fd,"Size : %d\n",size);
}
fclose(fd);
return 0;
}
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include "socket.h"
#include <netinet/in.h>
#include <sys/wait.h>
#include <stdlib.h>
#include "client.h"
#include "http_parse.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int copy(FILE* in, FILE* out){
int ch;
if(in && out){
ch = fgetc(in);
while(ch != EOF ){
fputc(ch,out);
ch=fgetc(in);
}
}else{
//error reading
exit(1);
}
return 0;
}
int get_file_size(int fd){
struct stat sb;
if(fstat(fd,&sb)==-1){
perror("stat");
exit(1);
}
return sb.st_size;
return 0;
}
FILE* check_and_open(const char* target, const char *document_root){
printf("check and open test\n");
char tmp[300]="";
strcat(tmp,target);
strcat(tmp,"/");
strcat(tmp,document_root);
printf("testons");
printf("check and open tmp : %s/%s \n tmp : %s \n",target,document_root,tmp);
return fopen(tmp,"r");
}
char* rewrite_target(char *target){
char* ret = strrchr(target, '?');
// printf("rewrite target ret : %s\n",ret);
if(ret != NULL){
char* rep = malloc(strlen(target)*sizeof(char));
size_t i = (int)(strlen(target)-strlen(ret));
rep = strncpy(rep,target+1,i-1);
return rep;
}else{
char* rep = malloc(strlen(target)*sizeof(char));
size_t i = (int)(strlen(target));
rep = strncpy(rep,target+1,i);
return rep;
}
}
char* fgets_or_exit(char * buffer, int size, FILE *fd){
char * ret = fgets(buffer,size,fd);
if(ret){
return ret;
} else {
exit(1);
}
}
int is_crlf(const char *str){
return (str[0] == '\r' && str[1] == '\n') || str[0] =='\n';
}
void skip_headers(FILE *client){
//passer les headers files
char buffer[8000];
while(1){
fgets_or_exit(buffer, sizeof(buffer), client);
if (is_crlf(buffer))
break;
}
}
void send_status(FILE *client, int code, const char *reason_phrase){
//transmet le status au client (1ère ligne)
fprintf(client, "HTTP/1.1 %d %s\r\n",code,reason_phrase);
}
void send_response(FILE *client, int code, const char *reason_phrase, const char *message_body){
send_status(client,code,reason_phrase);
//transmet la réponse complète (la suite)
fprintf(client,"Content-Lenght: %u\r\n",(unsigned int)strlen(message_body));
fprintf(client,"\r\n");
fprintf(client,"%s\r\n",message_body);
fflush(client);
}
int traitementClient(int socket_client){
/* On peut maintenant dialoguer avec le client */
char *motd="Votre requète est bonne";
printf("test\n");
char str[8000];
http_request request;
FILE *fd;
fd = fdopen(socket_client, "a+");
if(fd == NULL) {
perror("Error opening file\n");
exit(1);
}
//fprintf(fd,"pas d'erreurs d'ouverture\n");
fgets_or_exit(str,8000,fd);
int parse_ret = parse_http_request(str,&request);
skip_headers(fd);
if (parse_ret == -1) {
if (request.method == HTTP_UNSUPPORTED){
send_response(fd, 405, "Method Not Allowed", "Method Not Allowed");
}else{
send_response(fd, 400, "Bad Request", "Bad request");
}
}else if (strcmp(request.target, "/html") == 0){
send_response(fd, 200, "OK", motd);
//---
}else{
send_response(fd, 404, "Not Found", "Not Found");
}
fflush(fd);
//partie 7
char* absolute_path = rewrite_target(request.target);
printf("test - - - - - - - - %s\n",absolute_path);
FILE* fichier = check_and_open(absolute_path, "index.html");
// fprintf(fd,"fichier : \n");
if(fichier == NULL){
send_response(fd, 404, "Not Found2", "Not Found2");
}else{
int size = get_file_size(fileno(fichier));
if(copy(fichier,fd)==-1){
//error
}
//fprintf(fd,"Size : %d\n",size);
}
fclose(fd);
return 0;
}
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include "socket.h"
#include <netinet/in.h>
#include <sys/wait.h>
#include <stdlib.h>
#include "client.h"
#include "http_parse.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int copy(FILE* in, FILE* out){
int ch;
if(in && out){
while((ch =( fgetc(in))!= EOF) ){
fputc(ch,out);
}
}else{
//error reading
exit(1);
}
return 0;
}
int get_file_size(int fd){
struct stat sb;
if(fstat(fd,&sb)==-1){
perror("stat");
exit(1);
}
return sb.st_size;
return 0;
}
FILE* check_and_open(const char* target, const char *document_root){
char*tmp="";
strcat(tmp,target);
strcat(tmp,document_root);
strcat(tmp,"\n");
return fopen(tmp,"r");
}
char* rewrite_target(char *target){
char* ret = strrchr(target, '?');
char* rep = malloc(strlen(target)*sizeof(char));
if (ret!= NULL){
size_t i = (int)(strlen(target)-strlen(ret));
rep = strncpy(rep,target,i);
}
return rep;
}
char* fgets_or_exit(char * buffer, int size, FILE *fd){
char * ret = fgets(buffer,size,fd);
if(ret){
return ret;
} else {
exit(1);
}
}
int is_crlf(const char *str){
return (str[0] == '\r' && str[1] == '\n') || str[0] =='\n';
}
void skip_headers(FILE *client){
//passer les headers files
char buffer[8000];
while(1){
fgets_or_exit(buffer, sizeof(buffer), client);
if (is_crlf(buffer))
break;
}
}
void send_status(FILE *client, int code, const char *reason_phrase){
//transmet le status au client (1ère ligne)
fprintf(client, "HTTP/1.1 %d %s\r\n",code,reason_phrase);
}
void send_response(FILE *client, int code, const char *reason_phrase, const char *message_body){
send_status(client,code,reason_phrase);
//transmet la réponse complète (la suite)
fprintf(client,"Content-Lenght: %u\r\n",(unsigned int)strlen(message_body));
fprintf(client,"\r\n");
fprintf(client,"%s\r\n",message_body);
fflush(client);
}
int traitementClient(int socket_client){
/* On peut maintenant dialoguer avec le client */
char *motd="Votre requète est bonne";
printf("test\n");
char str[8000];
http_request request;
FILE *fd;
fd = fdopen(socket_client, "a+");
if(fd == NULL) {
perror("Error opening file\n");
exit(1);
}
//fprintf(fd,"pas d'erreurs d'ouverture\n");
fgets_or_exit(str,8000,fd);
int parse_ret = parse_http_request(str,&request);
skip_headers(fd);
if (parse_ret == -1) {
if (request.method == HTTP_UNSUPPORTED){
send_response(fd, 405, "Method Not Allowed", "Method Not Allowed");
}else{
send_response(fd, 400, "Bad Request", "Bad request");
}
}else if (strcmp(request.target, "/") == 0){
send_response(fd, 200, "OK", motd);
//---
}else{
send_response(fd, 404, "Not Found", "Not Found");
}
//fflush(fd);
//partie 7
char* absolute_path = rewrite_target(request.target);
printf("test - - - - - - - - %s\n",absolute_path);
FILE* fichier = check_and_open(absolute_path, "document.txt");
if(fichier == NULL){
send_response(fd, 404, "Not Found", "Not Found");
}
int size = get_file_size(fileno(fichier));
if(copy(fichier,fd)==-1){
//error
}
fprintf(fd,"Size : %d\n",size);
fclose(fd);
return 0;
}
No preview for this file type
<html>
<head>
<title> </title>
<style type="text/css">
<!--
h1 {text-align:center;
font-family:Arial, Helvetica, Sans-Serif;
}
p {text-indent:20px;
}
-->
</style>
</head>
<body bgcolor = "#ffffcc" text = "#000000">
<h1>Hello, World!</h1>
<p>You can modify the text in the box to the left any way you like, and
then click the "Show Page" button below the box to display the
result here. Go ahead and do this as often and as long as you like.</p>
<p>You can also use this page to test your Javascript functions and local
style declarations. Everything you do will be handled entirely by your own
browser; nothing you type into the text box will be sent back to the
server.</p>
<p>When you are satisfied with your page, you can select all text in the
textarea and copy it to a new text file, with an extension of
either <b>.htm</b> or <b>.html</b>, depending on your Operating System.
This file can then be moved to your Web server.</p>
</body>
</html>
No preview for this file type
......@@ -55,7 +55,7 @@ int creer_serveur(int port){
perror("fork fail");
exit(1);
}else if(pid > 0) {
//close(socket_client); fait une erreur si on le met
close(socket_client);// fait une erreur si on le met
}else{ // à déplacer ailleurs pour une meilleure lisibilité
//traitement final
traitementClient(socket_client);
......
No preview for this file type
testons
le
fopen
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment