Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "main.h"
- WSADATA wsaData;
- SOCKET listenSocket;
- char configFile[MAX_PATH] = {0};
- lua_State *luastate = NULL;
- char *lua_print_buffer = NULL;
- int main(int argc, char *argv[])
- {
- GetCurrentDirectoryA(MAX_PATH, configFile);
- lstrcatA(configFile, "\\webcfg.ini");
- char port[16];
- GetPrivateProfileStringA("MAIN", "port", "8080", port, 16, configFile);
- printf("Starting winsock...\n");
- //Startup winsock
- int startupResult = 0;
- startupResult = WSAStartup(MAKEWORD(2,2), &wsaData);
- if (startupResult != 0) {
- printf("WSAStartup failed: %d\n", startupResult);
- return 1;
- }
- //Create server socket
- struct addrinfo *result = NULL;
- struct addrinfo *ptr = NULL;
- struct addrinfo hints;
- ZeroMemory(&hints, sizeof(hints));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_flags = AI_PASSIVE;
- //--Resolve address & port
- int gaiResult = 0;
- gaiResult = getaddrinfo(NULL, port, &hints, &result);
- if (gaiResult != 0) {
- printf("getaddrinfo failed: %d\n", gaiResult);
- WSACleanup();
- return 1;
- }
- //--Create socket
- listenSocket = INVALID_SOCKET;
- for (int i = 0; i < 5; i++)
- {
- listenSocket = socket(result->ai_family,
- result->ai_socktype, result->ai_protocol);
- if (listenSocket != INVALID_SOCKET) break;
- printf("Failed to create socket [Attempt %d]\n", i);
- Sleep(1000);
- }
- if (listenSocket == INVALID_SOCKET) {
- printf("Failed to create socket: %d\n", WSAGetLastError());
- freeaddrinfo(result);
- WSACleanup();
- return 1;
- }
- //--Bind socket
- int bindResult = 0;
- bindResult = bind(listenSocket, result->ai_addr,
- (int)result->ai_addrlen); //bind to localhost:port
- if (bindResult == SOCKET_ERROR) {
- printf("Fail to bind socket: %d\n", WSAGetLastError());
- freeaddrinfo(result);
- WSACleanup();
- return 1;
- }
- freeaddrinfo(result);
- //Start listening
- if (listen(listenSocket, MAX_CONNECTIONS) == SOCKET_ERROR) {
- printf("Failed to listen: %d\n", WSAGetLastError());
- closesocket(listenSocket);
- WSACleanup();
- return 1;
- }
- /*Non-blocking
- unsigned long nonblocking = 1;
- ioctlsocket(listenSocket, FIONBIO, &nonblocking);*/
- //Startup lua
- printf("Creating lua state...\n");
- luastate = lua_open();
- luaL_openlibs(luastate);
- lua_register(luastate, "print", l_print);
- //Server loop
- printf("Entering server loop...\n");
- char buffer[BUFFER_SIZE] = {0};
- while (!GetAsyncKeyState(VK_END))
- {
- memset(buffer, 0, BUFFER_SIZE);
- SOCKET cs = INVALID_SOCKET;
- cs = accept(listenSocket, NULL, NULL);
- if (cs == INVALID_SOCKET) continue;
- int sz = recv(cs, buffer, BUFFER_SIZE, 0);
- if (sz > 0)
- {
- if (lstrcmpn(buffer, "GET", 3) == 0)
- {
- parsesendGET(&cs, buffer);
- }
- }
- closesocket(cs);
- }
- lua_close(luastate);
- closesocket(listenSocket);
- WSACleanup();
- return 0;
- }
- void parsesendGET(SOCKET *cs, char *request)
- {
- char *newreq = (char*)halloc(lstrlenA(request));
- char lastReqChar = *request; int nridx = 0;
- while (*request != NULL)
- {
- if (*request != '\r' && !(*request == '\n' && lastReqChar == '\n'))
- {
- newreq[nridx++] = *request;
- lastReqChar = *request;
- }
- *request++;
- }
- newreq[lstrlenA(newreq)-1] = '\0';
- char *pnewreq = newreq;
- while (*pnewreq != NULL)
- {
- if (*pnewreq == '\n') *pnewreq = '\0';
- pnewreq++;
- }
- char *GETline = newreq;
- char *GETfile = GETline+4;
- while (*GETline != NULL)
- {
- if (*GETline == ' ') *GETline = '\0';
- *GETline++;
- }
- char returnFile[MAX_PATH] = {0};
- if (lstrcmpA(GETfile, "/") == 0)
- lstrcpyA(returnFile, HTTP_DEFAULT_FILE);
- else
- lstrcpyA(returnFile, GETfile);
- GETfile = &returnFile[0];
- char *GEText = NULL;
- while (*GETfile != NULL)
- {
- if (*GETfile == '.') GEText = GETfile+1;
- *GETfile++;
- }
- char returnExt[16] = {0};
- lstrcpyA(returnExt, "ext_");
- lstrcatA(returnExt, GEText);
- hfree(newreq);
- char mimeType[MAX_PATH] = {0};
- if (GEText == NULL) lstrcpyA(mimeType, "text/plain");
- else GetPrivateProfileStringA("TRANSLATIONS", returnExt, "text/plain",
- mimeType, MAX_PATH, configFile);
- char filePath[MAX_PATH] = {0};
- GetCurrentDirectoryA(MAX_PATH, filePath);
- int currdirsize = lstrlenA(filePath);
- lstrcatA(filePath, returnFile);
- //Read file
- printf("File requested: %s\n", &filePath[0]+currdirsize);
- char *fc = NULL; DWORD fsize = 0;
- HANDLE file = INVALID_HANDLE_VALUE;
- file = CreateFileA(filePath, GENERIC_READ, FILE_SHARE_READ,
- NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (file == INVALID_HANDLE_VALUE)
- {
- printf("Error opening file! %d\n", GetLastError());
- fc = (char*)halloc(KB * 5);
- GetPrivateProfileSectionA("ERROR_404", fc, KB * 5, configFile);
- if (lstrlenA(fc) > 0) ProfileSection2str(fc);
- else lstrcpyA(fc, HTTP_404_CONTENT);
- lstrcpyA(mimeType, "text/html");
- }
- else
- {
- fsize = GetFileSize(file, NULL);
- printf("File size: %d\n", fsize);
- fc = (char*)halloc(fsize+8);
- DWORD bytesRead = 0;
- if (ReadFile(file, fc, fsize, &bytesRead, NULL) == FALSE)
- {
- printf("Error reading file! %d\n", GetLastError());
- CloseHandle(file);
- }
- CloseHandle(file);
- }
- //Build HTTP response
- lua_print_buffer = (char*)halloc(LUA_PRINTBUFFER_SIZE);
- *lua_print_buffer = '\n';
- char *data = (char*)halloc(512+fsize+LUA_PRINTBUFFER_SIZE);
- char httpVer[64] = {0};
- wsprintfA(httpVer, HTTP_VER, (fsize==0)?"404 Not Found":"200 OK");
- lstrcatA(data, HTTP_VER);
- lstrcatA(data, HTTP_SERVER);
- lstrcatA(data, HTTP_CACHECONTROL);
- lstrcatA(data, HTTP_KEEPALIVE);
- lstrcatA(data, HTTP_CONNECTION);
- char httpContenttype[64] = {0};
- wsprintfA(httpContenttype, HTTP_CONTENTTYPE, mimeType);
- lstrcatA(data, httpContenttype);
- if (fc != NULL)
- {
- char *lend = ParseLuaTags(fc);
- lstrcatA(data, fc);
- if (lend != NULL)
- {
- lstrcatA(data, lend);
- if (lstrlenA(lua_print_buffer) > 0)
- lstrcatA(data, lua_print_buffer);
- hfree(lua_print_buffer);
- }
- hfree(fc);
- }
- send(*cs, data, lstrlenA(data), 0);
- hfree(data);
- }
- char *ParseLuaTags(char *html)
- {
- char *ptr = html;
- char *luastart = NULL;
- char *luaend = NULL;
- int lualen = -1;
- while (*ptr != NULL)
- {
- if (luastart == NULL)
- {
- if (*ptr == '<' && *++ptr == 'l' && *++ptr == 'u' &&
- *++ptr == 'a' && *++ptr == '>')
- {
- *(ptr - 4) = '\0';
- luastart = ++ptr;
- if (*luastart == '\r') *luastart++;
- if (*luastart == '\n') *luastart++;
- }
- } else {
- if (*ptr == '<' && *++ptr == '/' && *++ptr == 'l' &&
- *++ptr == 'u' && *++ptr == 'a' && *++ptr == '>')
- {
- luaend = ptr - 7;
- }
- }
- *ptr++;
- }
- if (luastart != NULL && luaend != NULL)
- {
- lualen = luaend - luastart;
- char *luastr = (char*)halloc(lualen + 1);
- lstrcpynA(luastr, luastart, lualen);
- //printf("Lua: %X %X %d\n", luastart, luaend, lualen);
- luaL_dostring(luastate, luastr);
- hfree(luastr);
- }
- return luaend+8;
- }
- LUA_FUNCTION(l_print)
- {
- if (!lua_isstring(L, 1)) return 0;
- const char *str = lua_tostring(L, 1);
- if (lstrlenA(lua_print_buffer) + lstrlenA(str) < LUA_PRINTBUFFER_SIZE)
- lstrcatA(lua_print_buffer, str);
- return 0;
- }
- void ProfileSection2str(char *sec)
- {
- char *ptr = sec;
- while (true)
- {
- if (*ptr == NULL)
- {
- if (*(ptr+1) == NULL) break;
- *ptr = '\n';
- }
- *ptr++;
- }
- }
- int lstrcmpn(char *str1, char *str2, int size)
- {
- for (int i = 0; i < size; i++)
- {
- if (str1[i] != str2[i]) return 1;
- }
- return 0;
- }
- int lstrcnt(char *str1, char chr)
- {
- char *ptr = str1;
- int charCount = 0;
- while (*ptr != NULL)
- {
- if (chr == *ptr) charCount++;
- *ptr++;
- }
- return charCount;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement