http.c
3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "http.h"
#include <stdlib.h>
void freePage(struct PageWeb** page)
{
if(*page!=NULL)
{
if((*page)->html_contents!=NULL) free((*page)->html_contents);
free(*page);
}
}
void getWeb(char* pageName, struct PageWeb** page)
{
*page=malloc(sizeof(struct PageWeb));
FILE* pageFile=fopen(pageName, "r+");
if(pageFile!=NULL)
{
fseek(pageFile, 0L, SEEK_END);
long nbr_char=ftell(pageFile);
fseek(pageFile, 0L, SEEK_SET);
(*page)->html_contents=malloc(nbr_char*sizeof(char));
fread(((*page)->html_contents),1,nbr_char,pageFile);
fclose(pageFile);
}
else
{
char erreur[]="<h1> 404 PAGE NOT FOUND </h1>\r\n\r\n La page que vous cherchez n'a pas été trouvée, sans doute parce que vous êtes nul.";
(*page)->html_contents=malloc(strlen(erreur)*sizeof(char));
strcpy((*page)->html_contents, erreur);
}
(*page)->size=strlen(((*page)->html_contents));
}
void createPage(struct PageWeb** page, struct interface_info* interfaces[20])
{
*page=malloc(sizeof(struct PageWeb));
(*page)->html_contents=malloc(BUFF_SIZE*sizeof(char));
strcpy((*page)->html_contents,"");
//Header de la page
strcat((*page)->html_contents,"<!doctype html>\r\n<html>\r\n<head>\r\n<meta charset =\"utf-8\">\r\n<title> Interfaces Tangibles </title>\r\n</meta>");
// Intégration de la fiche de style
FILE* css_file=fopen("../Sioux/Tableau.css", "r+");
if(css_file!=NULL)
{
char style_str[BUFF_SIZE];
strcat((*page)->html_contents,"<style>");
fseek(css_file, 0L, SEEK_END);
long nbr_char = ftell(css_file);
fseek(css_file, 0L, SEEK_SET);
fread(style_str,1,nbr_char,css_file);
strcat((*page)->html_contents,style_str);
strcat((*page)->html_contents,"</style>");
fclose(css_file);
}
strcat((*page)->html_contents,"</head><body><div class=\"container\"><h2>Interfaces Tangibles</h2><ul class=\"tableau\">");
strcat((*page)->html_contents,"<li class=\"table-entete\"><div class=\"col nom\">Interfaces</div><div class=\"col etat\">Etat</div><div class=\"col commande\">Commande</div></li>");
int nbr_interfaces=0;
while(interfaces[nbr_interfaces]!=NULL) nbr_interfaces++;
if(nbr_interfaces==0)
strcat((*page)->html_contents,"<li class=\"table-ligne\">\n<div class=\"no_inter\" data-label=\"NoInter\"> Aucune interface </div>");
else
{
for(int i=0; i<nbr_interfaces; i++)
{
if(interfaces[i]->status >= 0)
{
// Si on a le status de l'interface on l'affiche
strcat((*page)->html_contents,"<li class=\"table-ligne\">\n<div class=\"col nom\" data-label=\"Nom\">");
strcat((*page)->html_contents,interfaces[i]->adresse);
switch(interfaces[i]->status)
{
case(0):
strcat((*page)->html_contents,"</div>\n<div class=\"col etat\" style=\"color: red\" data-label=\"Etat\">Mode Sommeil</div>\n");
break;
case(1):
strcat((*page)->html_contents,"</div>\n<div class=\"col etat\" style=\"color: green\" data-label=\"Etat\">Mode Eveillé</div>\n");
break;
}
if(interfaces[i]->commande>=0)
{
// Si on a la valeur de la commande de l'interface on l'affiche
char tmp[10];
sprintf(tmp, "%d", interfaces[i]->commande);
strcat((*page)->html_contents,"<div class=\"col commande\" data-label=\"Etat\">");
strcat((*page)->html_contents,tmp);
strcat((*page)->html_contents,"%</div>\n");
}
else
{
// Sinon inconnu
strcat((*page)->html_contents,"<div class=\"col commande\" style=\"color: grey\" data-label=\"Etat\">Inconnu</div>\n");
}
strcat((*page)->html_contents,"</li>");
}
}
}
strcat((*page)->html_contents,"</ul>\n</div>\n</body>\n</html>");
(*page)->size=strlen(((*page)->html_contents));
}