Created
September 23, 2024 02:20
-
-
Save ActuallyFro/2d66c8d7b6fede1fa764411df36b3360 to your computer and use it in GitHub Desktop.
Dr. Christensen's weblite -- but to serve kindle Mobis...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Base code: https://cse.usf.edu/~kchriste/tools/weblite.c | |
| //ALL modifications by ChatGPT -- I make zero claims, have no license to give -- contact him: http://www.csee.usf.edu/~christen || christen @ csee . usf . edu | |
| //https://cse.usf.edu/~kchriste/tools/toolpage.html | |
| // Compile: ` x86_64-w64-mingw32-gcc weblite.c -lws2_32 -o weblite` | |
| // | |
| //ALSO -- to convert .epub to .mobi: | |
| //`sudo apt-get install calibre` | |
| //THE BASH SCRIPT: | |
| // #!/bin/bash | |
| // # Loop through all ePub files in the current directory | |
| // for epub_file in *.epub; do | |
| // # Check if there are any ePub files | |
| // if [[ ! -e "$epub_file" ]]; then | |
| // echo "No ePub files found in the current directory." | |
| // exit 1 | |
| // fi | |
| // # Get the base name of the file (without the extension) | |
| // base_name=$(basename "$epub_file" .epub) | |
| // # Convert ePub to MOBI using Calibre's ebook-convert | |
| // ebook-convert "$epub_file" "$base_name.mobi" | |
| // echo "Converted $epub_file to $base_name.mobi" | |
| // done | |
| // echo "Conversion complete." | |
| //The SECOND Bashscript: (to make index.html) | |
| // #!/bin/bash | |
| // # Output HTML file | |
| // OUTPUT="index.html" | |
| // # Start HTML file | |
| // echo "<html>" > "$OUTPUT" | |
| // echo "<head><title>Download Links</title></head>" >> "$OUTPUT" | |
| // echo "<body>" >> "$OUTPUT" | |
| // echo "<h1>Download Links for MOBI Files</h1>" >> "$OUTPUT" | |
| // echo "<ul>" >> "$OUTPUT" | |
| // # Loop through .mobi files in the current directory and create links | |
| // for file in ./*.mobi; do | |
| // if [ -f "$file" ]; then | |
| // filename=$(basename "$file") | |
| // echo "<li><a href=\"$filename\">$filename</a></li>" >> "$OUTPUT" | |
| // fi | |
| // done | |
| // # End HTML file | |
| // echo "</ul>" >> "$OUTPUT" | |
| // echo "</body>" >> "$OUTPUT" | |
| // echo "</html>" >> "$OUTPUT" | |
| // echo "Generated $OUTPUT in $(pwd)" | |
| /////////////////////////////////////////////////////////////////////////////// | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <process.h> // For _beginthread | |
| #include <winsock2.h> // For sockets | |
| #define PORT 8080 | |
| #define BUFFER_SIZE 1024 | |
| void send_file(SOCKET sock, const char* filename) { | |
| FILE* file = fopen(filename, "rb"); | |
| if (!file) { | |
| // Serve index.html if the requested file is not found | |
| file = fopen("index.html", "rb"); | |
| if (!file) { | |
| const char* not_found_response = "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\nFile Not Found\n"; | |
| send(sock, not_found_response, strlen(not_found_response), 0); | |
| return; | |
| } | |
| } | |
| // Determine the content type | |
| const char* content_type; | |
| if (strstr(filename, ".mobi")) { | |
| content_type = "application/x-mobipocket-ebook"; | |
| } else { | |
| content_type = "text/html"; | |
| } | |
| // Send 200 OK response | |
| char response_header[BUFFER_SIZE]; | |
| snprintf(response_header, sizeof(response_header), | |
| "HTTP/1.1 200 OK\r\nContent-Type: %s\r\nContent-Disposition: %s\r\n\r\n", | |
| content_type, strstr(filename, ".mobi") ? "attachment" : "inline"); | |
| send(sock, response_header, strlen(response_header), 0); | |
| // Read the file and send its contents | |
| char buffer[BUFFER_SIZE]; | |
| size_t bytes_read; | |
| while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) > 0) { | |
| send(sock, buffer, bytes_read, 0); | |
| } | |
| fclose(file); | |
| } | |
| void handle_get(void* client_socket) { | |
| SOCKET sock = *(SOCKET*)client_socket; | |
| char buffer[BUFFER_SIZE]; | |
| int bytes_received; | |
| // Receive data (request from client) | |
| bytes_received = recv(sock, buffer, sizeof(buffer) - 1, 0); | |
| if (bytes_received <= 0) { | |
| closesocket(sock); | |
| free(client_socket); | |
| return; | |
| } | |
| buffer[bytes_received] = '\0'; // Null-terminate the received data | |
| printf("Received request: %s\n", buffer); | |
| // Check the request line for the file to serve | |
| char method[16], path[256]; | |
| sscanf(buffer, "%s %s", method, path); | |
| // Remove the leading '/' from the path | |
| if (strcmp(path, "/") == 0) { | |
| path[1] = '\0'; // Change "/" to "" | |
| } else { | |
| memmove(path, path + 1, strlen(path)); | |
| } | |
| send_file(sock, path[0] ? path : "index.html"); | |
| closesocket(sock); | |
| free(client_socket); | |
| } | |
| int main() { | |
| WSADATA wsaData; | |
| SOCKET server_socket, client_socket; | |
| struct sockaddr_in server_addr, client_addr; | |
| int addr_len = sizeof(client_addr); | |
| // Initialize Winsock | |
| if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { | |
| perror("WSAStartup failed"); | |
| return 1; | |
| } | |
| // Create socket | |
| server_socket = socket(AF_INET, SOCK_STREAM, 0); | |
| if (server_socket == INVALID_SOCKET) { | |
| perror("Socket creation failed"); | |
| WSACleanup(); | |
| return 1; | |
| } | |
| // Setup the server address structure | |
| server_addr.sin_family = AF_INET; | |
| server_addr.sin_addr.s_addr = INADDR_ANY; | |
| server_addr.sin_port = htons(PORT); | |
| // Bind the socket | |
| if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) { | |
| perror("Bind failed"); | |
| closesocket(server_socket); | |
| WSACleanup(); | |
| return 1; | |
| } | |
| // Listen for incoming connections | |
| if (listen(server_socket, 3) == SOCKET_ERROR) { | |
| perror("Listen failed"); | |
| closesocket(server_socket); | |
| WSACleanup(); | |
| return 1; | |
| } | |
| printf("Server listening on port %d\n", PORT); | |
| while (1) { | |
| // Accept a client socket | |
| client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &addr_len); | |
| if (client_socket == INVALID_SOCKET) { | |
| perror("Accept failed"); | |
| continue; // Continue to the next iteration | |
| } | |
| SOCKET* p_client = malloc(sizeof(SOCKET)); | |
| if (p_client == NULL) { | |
| perror("Memory allocation failed"); | |
| closesocket(client_socket); | |
| continue; // Skip this iteration if memory allocation fails | |
| } | |
| *p_client = client_socket; | |
| // Create a new thread to handle the client | |
| if (_beginthread(handle_get, 0, (void*)p_client) < 0) { | |
| perror("Failed to create thread"); | |
| closesocket(client_socket); | |
| free(p_client); | |
| } | |
| } | |
| // Clean up | |
| closesocket(server_socket); | |
| WSACleanup(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment