Skip to content

Commit a288036

Browse files
authored
Add files via upload
log file generator added
1 parent 7170eb9 commit a288036

File tree

6 files changed

+151
-0
lines changed

6 files changed

+151
-0
lines changed

conf/http.conf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
$ ===================================================== $
2+
$ Welcome to Hidalgo Server Configuration $
3+
$ '$' represent comment in hidalgo Configuration files $
4+
$ $
5+
$ # # # #### # # ##### ##### | @------@ $
6+
$ # # # # ## # # # # # # | | | | | $
7+
$ #### # # # # # # # ## # # | | +--+ | $
8+
$ # # # # ## ##### # # # # # | | | | | $
9+
$ # # # #### # # ##### ##### ##### | @------@ $
10+
$ $
11+
$ Hidalga is Multi-threaded Server build on Unix-C. $
12+
$ Server useing TCP/IP and Network Programming $
13+
$ This Configuration file contain GET, HEAD, POST conf $
14+
$ Supporting Html, Css, js and much more.. $
15+
$ ===================================================== $
16+
17+
$ IP Address is used to identify the Host PC.
18+
$ e.g IP:xxx.xxx.xxx.xxx
19+
20+
IP:127.0.0.1
21+
22+
$ Port Number is used to identify the Running Service.
23+
$ (NOTE :: only less then or equal 4 digit for Port)
24+
$ e.g PORT:xxxx
25+
26+
PORT:1500
27+

config.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#define CHUNK 50
2+
#define HTTP_conf "conf/http.conf"
3+
4+
int get_http_(char match){
5+
6+
char port[CHUNK], read, *line=NULL;
7+
FILE *http_port_fd;
8+
size_t len;
9+
int xport;
10+
11+
http_port_fd = fopen(HTTP_conf, "r");
12+
if (http_port_fd) {
13+
while ((read = getline(&line, &len, http_port_fd)) != -1){
14+
if(line[0] != '$'){
15+
if(line[0] == match){
16+
xport = get_integerFs(line, 0, 0);
17+
printf("SET :: %s", line);
18+
return xport;
19+
}
20+
}
21+
}
22+
23+
if (ferror(http_port_fd)){
24+
printf("\n$ Configuration file error\n");
25+
}
26+
fclose(http_port_fd);
27+
}
28+
}
29+
int get_integerFs(char *line, int num, int count){
30+
int i=0;
31+
32+
while (sscanf(line, "%*[^0-9]%d%n", &num, &i)==1){
33+
line+=i;
34+
count++;
35+
}
36+
return num;
37+
}

hidalga.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include<netinet/in.h>
2+
#include<stdio.h>
3+
#include<stdlib.h>
4+
#include<sys/socket.h>
5+
#include<sys/stat.h>
6+
#include<sys/types.h>
7+
#include<unistd.h>
8+
#include<string.h>
9+
#include<stdbool.h>
10+
11+
#define Hid_Port 'P'
12+
#define Hid_IP 'I'

httpserver

13.5 KB
Binary file not shown.

httpserver.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "hidalga.h"
2+
#include "config.c"
3+
#include "log_generator.c"
4+
//Hidalgo Server Socket MainStream
5+
//(c) - 2018 | ALL RIGHT RESERVED
6+
7+
int main() {
8+
int port = get_http_(Hid_Port);
9+
int ip = get_http_(Hid_IP);
10+
int create_socket, new_socket;
11+
socklen_t addrlen;
12+
int bufsize = 1024;
13+
char *buffer = malloc(bufsize);
14+
struct sockaddr_in address;
15+
static char* page =
16+
"HTTP/1.1 200 OK\n"
17+
"Content-type: text/html\n"
18+
"\n"
19+
"<html>\n"
20+
" <body>\n"
21+
" <h1>WebPAge</h1>\n"
22+
" <p>The requested URL was <?php echo'disk'; ?> on this server.</p>\n"
23+
" </body>\n"
24+
"</html>\n";
25+
26+
if ((create_socket = socket(AF_INET, SOCK_STREAM, 0)) > 0){
27+
printf("The socket was created\n");
28+
}
29+
30+
address.sin_family = AF_INET;
31+
address.sin_addr.s_addr = INADDR_ANY;
32+
address.sin_port = htons(port);
33+
34+
if (bind(create_socket, (struct sockaddr *) &address, sizeof(address)) == 0){
35+
printf("Binding Socket\n");
36+
}
37+
38+
39+
while (1) {
40+
if (listen(create_socket, 10) < 0) {
41+
perror("server: listen");
42+
exit(1);
43+
}
44+
45+
if ((new_socket = accept(create_socket, (struct sockaddr *) &address, &addrlen)) < 0) {
46+
perror("server: accept");
47+
exit(1);
48+
}
49+
50+
if (new_socket > 0){
51+
printf("The Client is connected...\n");
52+
}
53+
54+
recv(new_socket, buffer, bufsize, 0);
55+
//printf("%s\n", buffer);
56+
if(guest_logs(buffer)) printf("\nLogs Saved\n");
57+
else printf("\nLog Writing Err :: log_generator.c\n");
58+
write(new_socket, page, strlen(page));
59+
close(new_socket);
60+
}
61+
close(create_socket);
62+
return 0;
63+
}

log_generator.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
bool guest_logs(char *data){
2+
bool val = false;
3+
FILE *guest_log;
4+
guest_log = fopen("visitors.log", "a");
5+
6+
if(guest_log){
7+
puts(data);
8+
fwrite(data, sizeof(char), sizeof(data), guest_log);
9+
return (val=true);
10+
}
11+
else return val;
12+
}

0 commit comments

Comments
 (0)