Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: CI

on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run lint
uses: jidicula/clang-format-action@v4.11.0
with:
clang-format-version: "13"
fallback-style: "LLVM"

build-linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y g++ make
- name: Build
run: make
env:
CC: gcc
CXX: g++

build-macos:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: Build
run: make
env:
CC: gcc
CXX: g++
29 changes: 0 additions & 29 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AP HTTP
===
[![Travis (.org)](https://travis-ci.com/UTAP/APHTTP.svg)](https://travis-ci.com/UTAP/APHTTP)

[![code style: LLVM](https://img.shields.io/badge/code_style-LLVM-brightgreen.svg)](https://llvm.org/docs/CodingStandards.html)
[![Release](https://img.shields.io/github/release/UTAP/APHTTP.svg)](https://github.com/UTAP/APHTTP/releases/latest)
[![Wiki](https://img.shields.io/badge/GitHub-Wiki-yellowgreen.svg)](https://github.com/UTAP/APHTTP/wiki)
Expand Down
2 changes: 1 addition & 1 deletion examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main(int argc, char **argv) {
server.get("/", new ShowPage("static/home.html"));
server.get("/colors", new ColorHandler("template/colors.html"));
server.run();
} catch (const Server::Exception& e) {
} catch (const Server::Exception &e) {
cerr << e.getMessage() << endl;
}
}
41 changes: 22 additions & 19 deletions server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@

#include "../utils/utilities.hpp"


#ifdef _WIN32
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501 //win xp
#define _WIN32_WINNT 0x0501 // win xp
#endif
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <winsock2.h>
#else
//POSIX sockets
#include <sys/socket.h>
// POSIX sockets
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h> //close()
#endif

Expand All @@ -39,17 +38,19 @@
#define GETSOCKETERRNO() (errno)
#endif

static const char* getSocketError() {
static const char *getSocketError() {
#ifdef _WIN32
static char message[256];
message[0] = '\0';
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, WSAGetLastError(), 0, (LPSTR)&message, sizeof(message), NULL);
char *newline = strrchr(message, '\n');
if (newline) *newline = '\0';
return message;
static char message[256];
message[0] = '\0';
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, WSAGetLastError(), 0, (LPSTR)&message, sizeof(message),
NULL);
char *newline = strrchr(message, '\n');
if (newline)
*newline = '\0';
return message;
#else
return strerror(errno);
return strerror(errno);
#endif
}

Expand Down Expand Up @@ -234,7 +235,7 @@ Request *parseRawReq(char *headersRaw, size_t length) {
} break;
}
}
} catch (const Server::Exception&) {
} catch (const Server::Exception &) {
throw;
} catch (...) {
throw Server::Exception("Error on parsing request");
Expand All @@ -247,7 +248,8 @@ Server::Server(int _port) : port(_port) {
WSADATA wsa_data;
int initializeResult = WSAStartup(MAKEWORD(2, 2), &wsa_data);
if (initializeResult != 0) {
throw Exception("Error: WinSock WSAStartup failed: " + string(getSocketError()));
throw Exception("Error: WinSock WSAStartup failed: " +
string(getSocketError()));
}
#endif

Expand All @@ -257,7 +259,8 @@ Server::Server(int _port) : port(_port) {
int sc_option = 1;

#ifdef _WIN32
setsockopt(sc, SOL_SOCKET, SO_REUSEADDR, (char*)&sc_option, sizeof(sc_option));
setsockopt(sc, SOL_SOCKET, SO_REUSEADDR, (char *)&sc_option,
sizeof(sc_option));
#else
setsockopt(sc, SOL_SOCKET, SO_REUSEADDR, &sc_option, sizeof(sc_option));
#endif
Expand Down Expand Up @@ -300,7 +303,7 @@ void Server::run() {
throw Exception("Error on accept: " + string(getSocketError()));
Response *res = NULL;
try {
char* data = new char[BUFSIZE + 1];
char *data = new char[BUFSIZE + 1];
size_t recv_len, recv_total_len = 0;
Request *req = NULL;
while (!req) {
Expand Down Expand Up @@ -330,7 +333,7 @@ void Server::run() {
res = notFoundHandler->callback(req);
}
delete req;
} catch (const Exception& exc) {
} catch (const Exception &exc) {
delete res;
res = ServerErrorHandler::callback(exc.getMessage());
}
Expand Down
27 changes: 15 additions & 12 deletions utils/template_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#include <map>
using namespace std;

static std::string mkdirNoErrors(const std::string& dirName) {
//do not error if dir already exists (-p flag on linux)
static std::string mkdirNoErrors(const std::string &dirName) {
// do not error if dir already exists (-p flag on linux)
#ifdef _WIN32
return ("(if not exist \"" + dirName + "\" " + SysCmd::mkdir + "\"" + dirName + "\")");
return ("(if not exist \"" + dirName + "\" " + SysCmd::mkdir + "\"" +
dirName + "\")");
#else
return (SysCmd::mkdir + "-p \"" + dirName + "\"");
#endif
Expand All @@ -19,7 +20,8 @@ const std::string localTemplate(const int parserNum) {
TemplateParser::TemplateParser(string _filePath) {
filePath = _filePath;
variableCount = 0;
programName = to_string(TemplateParser::lastParserNum) + SysCmd::fileExtention;
programName =
to_string(TemplateParser::lastParserNum) + SysCmd::fileExtention;
parserNum = TemplateParser::lastParserNum++;
code = "";
parseTemplate();
Expand All @@ -34,7 +36,7 @@ string TemplateParser::getHtml(map<string, string> _context) {
void TemplateParser::parseTemplate() {
string unparsedTemplate = readFile(filePath);
int parsePointer = 0;
while (parsePointer < (signed int) unparsedTemplate.size()) {
while (parsePointer < (signed int)unparsedTemplate.size()) {
int begin = findBeginOfCodeBlock(parsePointer, unparsedTemplate);
int end = findEndOfCodeBlock(parsePointer, unparsedTemplate);
if (begin < 0)
Expand Down Expand Up @@ -101,16 +103,16 @@ void TemplateParser::compileCode() {
throw Server::Exception("Can not write generated template code!");

string cmd = mkdirNoErrors(outputFolder) + " && " + cc + " " + toCompileFile +
" " + utilitiesPath + " -o " + outputFolder + SysCmd::slash + programName +
"&& " + SysCmd::rm + toCompileFile;
" " + utilitiesPath + " -o " + outputFolder + SysCmd::slash +
programName + "&& " + SysCmd::rm + toCompileFile;
string error = "Can not compile template " + filePath;
TemplateUtils::runSystemCommand(cmd, error);
}

string TemplateParser::runGeneratedCode() {

string cmd =
SysCmd::programStart + outputFolder + SysCmd::slash + programName + " " + " > " + staticTemplate;
string cmd = SysCmd::programStart + outputFolder + SysCmd::slash +
programName + " " + " > " + staticTemplate;
string error = "Error in running template " + filePath;
TemplateUtils::runSystemCommand(cmd, error);

Expand Down Expand Up @@ -162,7 +164,8 @@ void TemplateParser::deleteExecutable() {
}

void TemplateParser::deleteLocalTemplate() {
string cmd = SysCmd::rm + outputFolder + SysCmd::slash + localTemplate(parserNum);
string cmd =
SysCmd::rm + outputFolder + SysCmd::slash + localTemplate(parserNum);
string error = "Error in deleting local template at " + outputFolder + "/" +
localTemplate(parserNum);
TemplateUtils::runSystemCommand(cmd, error);
Expand All @@ -172,8 +175,8 @@ void TemplateParser::TemplateUtils::runSystemCommand(string command,
string error) {
int ret = system(command.c_str());
#ifdef _WIN32
if(ret != 0) {
throw Server::Exception(error);
if (ret != 0) {
throw Server::Exception(error);
}
#else
if (WEXITSTATUS(ret) != EXIT_SUCCESS) {
Expand Down
2 changes: 1 addition & 1 deletion utils/template_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const std::string mkdir = "mkdir ";
const std::string slash = "/";
const std::string fileExtention = ".o";
#endif
}
} // namespace SysCmd

const std::string beginCodeBlockTag = "<%";
const std::string endCodeBlockTag = "%>";
Expand Down
5 changes: 3 additions & 2 deletions utils/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ bool comp::operator()(const string &lhs, const string &rhs) const {
string readFile(const char *filename) {
ifstream infile;
infile.open(filename, infile.binary);
if (!infile.is_open()) return string();
if (!infile.is_open())
return string();

infile.seekg(0, infile.end);
size_t length = infile.tellg();
infile.seekg(0, infile.beg);

if (length > BUFFER_SIZE)
length = BUFFER_SIZE;
char* buffer = new char[length + 1];
char *buffer = new char[length + 1];

infile.read(buffer, length);

Expand Down