Skip to content

Instantly share code, notes, and snippets.

@smvd
Created July 10, 2021 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smvd/d7b1fd8ac93876ad7b0ed2343047db6c to your computer and use it in GitHub Desktop.
Save smvd/d7b1fd8ac93876ad7b0ed2343047db6c to your computer and use it in GitHub Desktop.
Simple HTTP server code
/*
- Read code from files
- Show images
- Show json
- Show video
- Show audio
- Better debugging
*/
#include <stdio.h>
#include <winsock2.h>
/*
HTTP/1.1 > version
200 > error code > succes
404 > error code > not found
400 > error code > bad request
OK > succes
Content-Type: text/html > Sending HTML code
Content-Length: 69 > Byte size of the data
The rest is the byte version of the data you are sending
*/
#define MAIN_PAGE "HTTP/1.1 200 OK\nContent-Type: text/html\nContent-Length: 76\n\n<h1>Eclips-Coding</h1><script>window.alert(\"Like and subscribe\");</script>"
#define DEBUG_PAGE "HTTP/1.1 200 OK\nContent-Type: text/html\nContent-Length: 69\n\n<h1>DEBUG</h1><script>window.alert(\"This server is live\");</script>"
#define NOT_FOUND "HTTP/1.1 404 Not Found\n"
#define BAD_REQUEST "HTTP/1.1 400 Bad Request\n"
#define SERVER_IP "127.0.0.1" // Localhost IP
#define SERVER_PORT 80 // Port for websites
#define BUFFER_SIZE 1024
#define MAX_CLIENT_BACKLOG 0 // It can only have i connection
#define SENDING_DELAY 1000 // A bit of delay so the data fully gets sent
#define TRUE 1
#define FALSE 0
#define NO_SWITCHES 0
int main()
{
while (1)
{
WSADATA wsaData; // Winsock2 config
SOCKET clientSocket; // The socket the client will connect to
struct sockaddr_in address; // Variable for server socket config
struct sockaddr_in client; // Variable for client socket config
char buff[BUFFER_SIZE]; // Buffer for receiving
char pagePath[BUFFER_SIZE]; // Buffer for what page gets requested
char *token; // Variable used by strtok
int msgSize; // The byte size of the received data
int socketSize = sizeof(struct sockaddr_in); // A temp variable with the size of the struct
WSAStartup(MAKEWORD(2,2), &wsaData); // Start winsock
address.sin_family = AF_INET; // Internet protocol
address.sin_addr.s_addr = inet_addr(SERVER_IP); // The used IP
address.sin_port = htons(SERVER_PORT); // The used port
clientSocket = socket(AF_INET, SOCK_STREAM, NO_SWITCHES); // Setup the socket
bind(clientSocket, (struct sockaddr *)&address, sizeof(address)); // Bind the socket to a port and IP
listen(clientSocket, MAX_CLIENT_BACKLOG); // Wait till peeps call
clientSocket = accept(clientSocket , (struct sockaddr *)&client, &socketSize); // Connect to the socket
printf("Connection - %s\n", inet_ntoa(client.sin_addr)); // Write out the IP that the connection is from
msgSize = recv(clientSocket, buff, BUFFER_SIZE, NO_SWITCHES); // Recieve the clients welcome message
buff[msgSize] = '\0'; // Make it a a NULL terminated string
token = strtok(buff, " "); // Cut it at the first space (request type)
if (strcasecmp(token, "GET")) // If it isnt a get request
{
send(clientSocket, BAD_REQUEST, strlen(BAD_REQUEST), NO_SWITCHES); // Throw error
}
token = strtok(NULL, " "); // Cut it at the second space (request path)
strcpy(pagePath, token); // Copy the token to the path variable
printf("Path > %s\n", pagePath); // Show the requested page
if (strcmp("/", pagePath) == 0) // If its the root path
{
send(clientSocket, MAIN_PAGE, strlen(MAIN_PAGE), NO_SWITCHES); // Return the main page
}
else if (strcmp("/debug", pagePath) == 0) // If its the debug path
{
send(clientSocket, DEBUG_PAGE, strlen(DEBUG_PAGE), NO_SWITCHES); // Return the debug page
}
else // If we dont host the requested page
{
send(clientSocket, NOT_FOUND, strlen(NOT_FOUND), NO_SWITCHES); // Throw error
}
Sleep(SENDING_DELAY); // Wait so the message can be sent
closesocket(clientSocket); // End the connection
printf("End connection\n");
WSACleanup(); // Cleanup the server
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment