#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <dirent.h>
#define PORT 1200
int i = 0;
#define MAX_FILE_SIZE 2024
#define MAX_LINES 100
#define MAX_LINE_LENGTH 80
char lines[MAX_LINES][MAX_LINE_LENGTH];
char *run_2(int a)
{
FILE *fp = popen("pwd", "r");
if (fp == NULL)
{
perror("fopen");
return NULL;
}
int i = 0;
// argv=0;
while (fgets(lines[i], MAX_LINE_LENGTH, fp) != NULL && i < MAX_LINES)
{
lines[i][strcspn(lines[i], "\n")] = '\0';
i++;
}
fclose(fp);
// printf("%s",lines[a]);
return lines[a];
}
char *loadFile(char *file2)
{
char *buffer = (char *)malloc(MAX_FILE_SIZE + 1);
if (!buffer)
{
perror("Error allocating memory");
return NULL;
}
FILE *file = fopen(file2, "r");
if (!file)
{
perror("Error opening file");
free(buffer);
return NULL;
}
size_t len = fread(buffer, 1, MAX_FILE_SIZE, file);
if (ferror(file))
{
perror("Error reading file");
fclose(file);
free(buffer);
return NULL;
}
fclose(file);
buffer[len] = '\0';
return buffer;
free(buffer);
}
int map_method_to_int(const char *method)
{
if (strcmp(method, "GET") == 0)
{
return 1;
}
else if (strcmp(method, "POST") == 0)
{
return 2;
}
else if (strcmp(method, "PUT") == 0)
{
return 3;
}
else
{
return 0; // Method not implemented or unknown
}
}
void saveFile(char *data)
{
FILE *fp = fopen("tmp.html", "w");
if (fp != NULL)
{
fprintf(fp, "%s", data);
fclose(fp);
}
else
{
perror("fopen");
}
}
void parse_request_line(char *req, char *method, char *uri)
{
sscanf(req, "%s %s", method, uri);
}
// Send a basic HTTP response
/* to jest potrzebne zeby wyslac list do przegladarki odpowiedz serwera */
void send_response(int connfd, const char *status, const char *content_type, char *body)
{
char response[1024];
snprintf(response, sizeof(response), "HTTP/1.1 %s\r\nContent-Type: %s\r\n\r\n%s",
status, content_type, body);
send(connfd, response, strlen(response), 0);
}
void handle_post(int connfd, char *data)
{
// Przetwarzanie danych z POST
// printf("Received POST ata: %s\n", data);
// ... (zapis do bazy danych, itp.)
// send_response(connfd, "200 Created", "text/plain", tmp);
}
char *create_index_html()
{
DIR *dir;
struct dirent *entry;
char *path = run_2(0);
dir = opendir(path);
if (dir == NULL)
{
perror("opendir");
return NULL;
}
FILE *fp = fopen("index.html", "w");
if (fp == NULL)
{
perror("fopen");
closedir(dir);
return NULL;
}
fprintf(fp, "<html><body><h1>Lista plików w katalogu</h1><ul>");
while ((entry = readdir(dir)) != NULL)
{
fprintf(fp, "<li><a href=%s>%s</a></li>", entry->d_name, entry->d_name);
}
fprintf(fp, "</ul></body></html>");
fclose(fp);
closedir(dir);
return NULL;
}
void *create_console_html()
{
FILE *fp = fopen("console.html", "w");
if (fp == NULL)
{
perror("fopen");
}
fprintf(fp,
"<!DOCTYPE html><html lang=en><head><meta charset=UTF-8><meta name=viewportcontent=width=device-width, initial-scale=1.0><title>Simple Console</title></head><body><h1>Simple Server Console</h1><form method=POST action=/console><label for=command>Enter command:</label><br><input type=text id=command name=command size=50><br><br><input type=submit value=Execute> </form> <h2>Output:</h2><pre id=output><!-- Output from the command will be displayed here --></pre></body></html>");
fclose(fp);
}
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
struct sockaddr_in cliaddr;
socklen_t cliaddrlen = sizeof(cliaddr);
char buffer[1024];
/****************************/
/* obsluga polaczenoa tcp */
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
printf("Socket created successfully\n");
/* familiada tcp */
serv_addr.sin_family = AF_INET;
/* ip 127.0.0.1= inaddr_any */
serv_addr.sin_addr.s_addr = INADDR_ANY;
/* port */
serv_addr.sin_port = htons(PORT);
/* bind to pobicie ip sprawdza czy mozna utwozyc serwer na danym ip jesli
tak to go tworzy */
if (bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
printf("Binding successful\n");
/* nasluch max 10 klijentow */
if (listen(listenfd, 10) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
/* printf console viewer conect */
printf("==> click here==> http://127.0.0.1:1200\n");
printf("Listening...\n");
/*****************************/
/* engine- silnik */
while (1)
{
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddrlen);
if (connfd < 0)
{
perror("accept");
continue;
}
if (recv(connfd, buffer, sizeof(buffer), 0) < 0)
{
perror("recv");
close(connfd);
continue;
}
char method[20], uri[1024];
parse_request_line(buffer, method, uri);
i++;
int method_code = map_method_to_int(method);
switch (method_code)
{
case 1:
{
char *page = (char *)malloc(120);
char index[11] = { "index.html" };
create_index_html();
if (strcmp(uri, "/") == 0)
{
/* info in terminal */
printf("%s", buffer);
/* index on browser */
send_response(connfd, "200 OK", "text/html", loadFile(index));
}
else if (strcmp(uri, "/console") == 0)
{
create_console_html();
char console[15] = { "console.html" };
char *content = loadFile(console);
if (content)
{
send_response(connfd, "200 OK", "text/html", content);
}
}
else if (page)
{
strcpy(page, ".");
strcat(page, uri);
/* try this "/etc/passwd" */
printf("%s", page);
send_response(connfd, "200 OK", "text/html", loadFile(page));
free(page);
}
else
{
char error[20] = { "Page not found" };
send_response(connfd, "404 Not Found", "text/plain", error);
}
break;
}
case 2:
{ // POST Method
if (strcmp(uri, "/console") == 0)
{
char *command = strstr(buffer, "\r\n\r\n");
if (command)
{
command += 4; // Przesuń wskaźnik na właściwą
// treść po nagłówkach
handle_post(connfd, command);
}
else
{
char error[20] = { "Invalid command" };
send_response(connfd, "400 Bad Request", "text/plain", error);
}
}
else
{
char error[20] = { "Page not found" };
send_response(connfd, "404 Not Found", "text/plain", error);
}
break;
}
case 3:
{ // PUT Method
// Implement PUT logic here
break;
}
default:
{ // Method not implemented
char error[24] = { "Method not implemented" };
send_response(connfd, "501 Not Implemented", "text/plain", error);
break;
}
}
close(connfd);
}
close(listenfd);
return 0;
}
No comments:
Post a Comment