-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_response.c
87 lines (67 loc) · 2.57 KB
/
http_response.c
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
/* Define the helper functions to generate http_response in this file */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
extern int idle_history [3600];
extern int count;
extern double usage_max;
extern double usage_avg;
extern double usage_latest;
extern pthread_mutex_t lock;
extern pthread_mutex_t lock_2;
extern pthread_mutex_t lock_3;
/* Helper function to generate JSON update HTTP response */
char* initial_http_update(char* response){
char response_header [] = "HTTP/1.1 200 OK\nContent-Type: application/JSON\n\n";
strcpy(response, response_header);
char response_json [100];
strcat(response, "{");
pthread_mutex_lock( &lock );
sprintf(response_json, "\"%s\" : %d", "count", count);
pthread_mutex_unlock( &lock );
strcat(response, response_json);
strcat(response, " , ");
pthread_mutex_lock( &lock );
sprintf(response_json, "\"%s\" : %f", "usage_max", usage_max);
strcat(response, response_json);
strcat(response, " , ");
sprintf(response_json, "\"%s\" : %f", "usage_avg", usage_avg);
strcat(response, response_json);
strcat(response, " , ");
sprintf(response_json, "\"%s\" : %f", "usage_latest", usage_latest);
strcat(response_json, "}");
strcat(response, response_json);
pthread_mutex_unlock( &lock );
return response;
}
/* Helper function to generate initial full HTTP response */
char* initial_http_response(char* response){
// Generate header
char response_header [] = "HTTP/1.1 200 OK\nContent-Type: text/html\n\n";
// Include interface.html
int LIMIT = 10000;
char* response_html = (char *) malloc(LIMIT * sizeof(char));
FILE* fp = fopen("interface.html", "r");
int html_length = fread(response_html, sizeof(char), LIMIT-1, fp);
response_html[html_length] = '\0';
fclose(fp);
// Include webpage.js
char* response_javascript = (char *) malloc(LIMIT * sizeof(char));
char* content_js = (char *) malloc(LIMIT * sizeof(char));
fp = fopen("webpage.js", "r");
int len = fread(content_js, sizeof(char), LIMIT-1, fp);
content_js[len] = '\0';
// int len = sprintf(response_javascript, "<html>\n<body>\n<script>\n%s\n</script>\n</body>\n</html>", content_js);
len = sprintf(response_javascript, "<script>\n%s\n</script>\n</body>\n</html>", content_js);
response_javascript[len] = '\0';
fclose(fp);
// Combine three components together
len = sprintf(response, "%s%s%s", response_header, response_html, response_javascript);
response[len] = '\0';
// sprintf(response, "%s%s", response_header, response_html);
free(response_html);
free(response_javascript);
free(content_js);
return response;
}