-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmime_type_handler.c
129 lines (105 loc) · 3.88 KB
/
mime_type_handler.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
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
//
// Created by sams on 08/06/2023.
//
#include "mime_type_handler.h"
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
char* get_content_type(const char* file_path) {
char* content_type = "text/plain"; // Default content type
char* extension = strrchr(file_path, '.');
if (extension != NULL) {
if (strcmp(extension, ".html") == 0) {
content_type = "text/html";
} else if (strcmp(extension, ".jpeg") == 0 || strcmp(extension, ".jpg") == 0) {
content_type = "image/jpeg";
} else if (strcmp(extension, ".png") == 0) {
content_type = "image/png";
} else if (strcmp(extension, ".gif") == 0) {
content_type = "image/gif";
} else if (strcmp(extension, ".txt") == 0) {
content_type = "text/plain";
} else if (strcmp(extension, ".css") == 0) {
content_type = "text/css";
} else if (strcmp(extension, ".js") == 0) {
content_type = "application/javascript";
} else if (strcmp(extension, ".json") == 0) {
content_type = "application/json";
} else if (strcmp(extension, ".xml") == 0) {
content_type = "application/xml";
} else if (strcmp(extension, ".pdf") == 0) {
content_type = "application/pdf";
} else if (strcmp(extension, ".zip") == 0) {
content_type = "application/zip";
}
// Add more cases for other file types if needed
}
return content_type;
}
bool check_php_file(const char* file_path) {
// Get the file extension
const char* extension = strrchr(file_path, '.');
if (extension != NULL) {
// Compare the file extension to ".php"
if (strcmp(extension, ".php") == 0) {
return true; // It's a PHP file
}
}
return false; // Not a PHP file
}
void handle_php_file(FILE *file, int *client_socket, const char* response_header) {
// Get the process ID
pid_t pid = getpid();
// Generate a unique temporary file name using the process ID
char temp_file_path[200];
snprintf(temp_file_path, sizeof(temp_file_path), "/tmp/php_script_%d.php", pid);
// Open the temporary file for writing
FILE *temp_file = fopen(temp_file_path, "w");
if (temp_file == NULL) {
perror("Failed to create temporary file");
return;
}
// Write the contents of the PHP script to the temporary file
char file_buffer[1024];
size_t bytes_read;
while ((bytes_read = fread(file_buffer, 1, sizeof(file_buffer), file)) > 0) {
fwrite(file_buffer, 1, bytes_read, temp_file);
}
// Close the temporary file
fclose(temp_file);
// Get the path to the PHP interpreter
char interpreter_path[200] = "/opt/homebrew/bin/php";
// Construct the command to execute the PHP script
char command[256];
snprintf(command, sizeof(command), "%s %s", interpreter_path, temp_file_path);
// Create a pipe for communication with the PHP interpreter
FILE *php_output = popen(command, "r");
if (php_output == NULL) {
perror("Failed to execute PHP script");
return;
}
// Send the HTTP response header
if (send(*client_socket, response_header, strlen(response_header), 0) < 0) {
perror("Failed to send response header");
fclose(file);
return;
}
// Read the output from the PHP interpreter and send it to the client
char php_buffer[1024];
size_t php_bytes_read;
while ((php_bytes_read = fread(php_buffer, 1, sizeof(php_buffer), php_output)) > 0) {
if (send(*client_socket, php_buffer, php_bytes_read, 0) < 0) {
perror("Failed to send PHP output");
return;
}
}
// Close the file
fclose(file);
// Remove the temporary file
remove(temp_file_path);
}