Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
21 changes: 21 additions & 0 deletions .clang-format
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
---
BasedOnStyle: Google
BreakBeforeBraces: Custom
BraceWrapping:
BeforeCatch: true
BeforeElse: true
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
IndentWidth: 4
AccessModifierOffset: -4
IndentCaseLabels: false
NamespaceIndentation: Inner
ColumnLimit: 0
AlignConsecutiveMacros: Consecutive
DerivePointerAlignment: false
SpacesBeforeTrailingComments: 1
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
...
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line numberDiff line numberDiff 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.

49 changes: 49 additions & 0 deletions Makefile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
CXX = g++
CXXFLAGS = -std=c++11 -Wall -pedantic

BUILD_DIR = build
TEMPLATE_DIR = .template
OUT_EXE = myserver.out

ifeq ($(OS),Windows_NT)
LDLIBS += -l Ws2_32
endif

all: $(BUILD_DIR) $(OUT_EXE)

$(OUT_EXE): $(BUILD_DIR)/main.o $(BUILD_DIR)/handlers.o $(BUILD_DIR)/response.o $(BUILD_DIR)/request.o $(BUILD_DIR)/utilities.o $(BUILD_DIR)/strutils.o $(BUILD_DIR)/server.o $(BUILD_DIR)/route.o $(BUILD_DIR)/template_parser.o
$(CXX) $(CXXFLAGS) $^ $(LDLIBS) -o $@

$(BUILD_DIR):
mkdir -p $(BUILD_DIR)

$(BUILD_DIR)/template_parser.o: utils/template_parser.cpp utils/template_parser.hpp utils/request.cpp utils/request.hpp utils/utilities.hpp utils/utilities.cpp utils/strutils.hpp utils/strutils.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@

$(BUILD_DIR)/response.o: utils/response.cpp utils/response.hpp utils/include.hpp
$(CXX) $(CXXFLAGS) -c $< -o $@

$(BUILD_DIR)/request.o: utils/request.cpp utils/request.hpp utils/include.hpp utils/utilities.hpp
$(CXX) $(CXXFLAGS) -c $< -o $@

$(BUILD_DIR)/utilities.o: utils/utilities.cpp utils/utilities.hpp utils/strutils.hpp
$(CXX) $(CXXFLAGS) -c $< -o $@

$(BUILD_DIR)/strutils.o: utils/strutils.cpp utils/strutils.hpp
$(CXX) $(CXXFLAGS) -c $< -o $@

$(BUILD_DIR)/server.o: server/server.cpp server/server.hpp server/route.hpp utils/utilities.hpp utils/strutils.hpp utils/response.hpp utils/request.hpp utils/include.hpp utils/template_parser.hpp utils/template_parser.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@

$(BUILD_DIR)/route.o: server/route.cpp server/route.hpp utils/utilities.hpp utils/response.hpp utils/request.hpp utils/include.hpp
$(CXX) $(CXXFLAGS) -c $< -o $@

$(BUILD_DIR)/handlers.o: examples/handlers.cpp server/server.hpp utils/utilities.hpp utils/response.hpp utils/request.hpp utils/include.hpp
$(CXX) $(CXXFLAGS) -c $< -o $@

$(BUILD_DIR)/main.o: examples/main.cpp server/server.hpp utils/utilities.hpp utils/response.hpp utils/request.hpp utils/include.hpp
$(CXX) $(CXXFLAGS) -c $< -o $@

.PHONY: all clean
clean:
rm -rf $(BUILD_DIR) $(TEMPLATE_DIR) *.o *.out &> /dev/null
11 changes: 6 additions & 5 deletions README.md
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
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)

**AP HTTP::_server_** is a simple web application server-side blocking framework for C++ based on simplified versions of [W++](http://konteck.github.io/wpp/), [HappyHTTP](http://scumways.com/happyhttp/happyhttp.html), and [cpp-netlib](http://cpp-netlib.org/).
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/AP-ECE-UT/APHTTP/license.md)
[![Release](https://img.shields.io/github/release/AP-ECE-UT/APHTTP.svg?color=brightgreen)](https://github.com/AP-ECE-UT/APHTTP/releases/latest)
[![Wiki](https://img.shields.io/badge/GitHub-Wiki-red.svg)](https://github.com/AP-ECE-UT/APHTTP/wiki)

**AP HTTP::_server_** is a simple web application server-side blocking framework for C++.
This library is based on simplified versions of [W++](http://konteck.github.io/wpp/), [HappyHTTP](http://scumways.com/happyhttp/happyhttp.html), and [cpp-netlib](http://cpp-netlib.org/).
94 changes: 50 additions & 44 deletions examples/handlers.cpp
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,60 @@
#include "handlers.hpp"

using namespace std;

Response *RandomNumberHandler::callback(Request *req) {
Response *res = new Response;
res->setHeader("Content-Type", "text/html");
string body;
body += "<!DOCTYPE html>";
body += "<html>";
body += "<body style=\"text-align: center;\">";
body += "<h1>AP HTTP</h1>";
body += "<p>";
body += "a random number in [1, 10] is: ";
body += to_string(rand() % 10 + 1);
body += "</p>";
body += "<p>";
body += "SeddionId: ";
body += req->getSessionId();
body += "</p>";
body += "</body>";
body += "</html>";
res->setBody(body);
return res;
#include <cstdlib>
#include <iostream>

Response* RandomNumberHandler::callback(Request* req) {
Response* res = new Response();
res->setHeader("Content-Type", "text/html");

std::string randomNumber = std::to_string(std::rand() % 10 + 1);
std::string body;

body += "<!DOCTYPE html>";
body += "<html lang=\"en\">";

body += "<head>";
body += " <title>Random Number Page</title>";
body += "</head>";

body += "<body style=\"text-align: center;\">";
body += " <h1>AP HTTP</h1>";
body += " <p>A random number in [1, 10] is: " + randomNumber + "</p>";
body += " <p>SessionId: " + req->getSessionId() + "</p>";
body += "</body>";

body += "</html>";
res->setBody(body);
return res;
}

Response *LoginHandler::callback(Request *req) {
string username = req->getBodyParam("username");
string password = req->getBodyParam("password");
if (username == "root")
throw Server::Exception("Remote root access has been disabled.");
cout << "username: " << username << ",\tpassword: " << password << endl;
Response *res = Response::redirect("/rand");
res->setSessionId("SID");
return res;
Response* LoginHandler::callback(Request* req) {
std::string username = req->getBodyParam("username");
std::string password = req->getBodyParam("password");
if (username == "root") {
throw Server::Exception("Remote root access has been disabled.");
}
std::cout << "username: " << username << ",\tpassword: " << password << std::endl;
Response* res = Response::redirect("/rand");
res->setSessionId("SID");
return res;
}

Response *UploadHandler::callback(Request *req) {
string name = req->getBodyParam("file_name");
string file = req->getBodyParam("file");
cout << name << " (" << file.size() << "B):\n" << file << endl;
Response *res = Response::redirect("/");
return res;
Response* UploadHandler::callback(Request* req) {
std::string name = req->getBodyParam("file_name");
std::string file = req->getBodyParam("file");
utils::writeToFile(file, name);
Response* res = Response::redirect("/");
return res;
}

ColorHandler::ColorHandler(string filePath) : TemplateHandler(filePath) {}
ColorHandler::ColorHandler(const std::string& filePath)
: TemplateHandler(filePath) {}

map<string, string> ColorHandler::handle(Request *req) {
map<string, string> context;
string newName = "I am " + req->getQueryParam("name");
context["name"] = newName;
context["color"] = req->getQueryParam("color");
return context;
std::map<std::string, std::string> ColorHandler::handle(Request* req) {
std::string newName = "I am " + req->getQueryParam("name");
std::map<std::string, std::string> context;
context["name"] = newName;
context["color"] = req->getQueryParam("color");
return context;
}
22 changes: 11 additions & 11 deletions examples/handlers.hpp
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
#ifndef _MY_HANDLERS_
#define _MY_HANDLERS_
#ifndef HANDLERS_HPP_INCLUDE
#define HANDLERS_HPP_INCLUDE

#include <map>
#include <string>

#include "../server/server.hpp"
#include <cstdlib> // for rand and srand
#include <ctime> // for time
#include <iostream>

class RandomNumberHandler : public RequestHandler {
public:
Response *callback(Request *);
Response* callback(Request*) override;
};

class LoginHandler : public RequestHandler {
public:
Response *callback(Request *);
Response* callback(Request*) override;
};

class UploadHandler : public RequestHandler {
public:
Response *callback(Request *);
Response* callback(Request*) override;
};

class ColorHandler : public TemplateHandler {
public:
ColorHandler(std::string filePath);
std::map<std::string, std::string> handle(Request *req);
ColorHandler(const std::string& filePath);
std::map<std::string, std::string> handle(Request* req) override;
};

#endif
#endif // HANDLERS_HPP_INCLUDE
42 changes: 26 additions & 16 deletions examples/main.cpp
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
#include "handlers.hpp"
#include "my_server.hpp"
#include <cstdlib> // for rand and srand
#include <ctime> // for time
#include <iostream>
#include <string>

using namespace std;
#include "../server/server.hpp"
#include "handlers.hpp"

int main(int argc, char **argv) {
srand(time(NULL)); // for rand
try {
MyServer server(argc > 1 ? atoi(argv[1]) : 5000);
void mapServerPaths(Server& server) {
server.setNotFoundErrPage("static/404.html");
server.get("/", new ShowPage("static/home.html"));
server.get("/home.png", new ShowImage("static/home.png"));
server.get("/rand", new RandomNumberHandler());
server.get("/login", new ShowPage("static/logincss.html"));
server.post("/login", new LoginHandler());
server.get("/up", new ShowPage("static/upload_form.html"));
server.post("/up", new UploadHandler());
server.get("/rand", new RandomNumberHandler());
server.get("/home.png", new ShowImage("static/home.png"));
server.get("/", new ShowPage("static/home.html"));
server.get("/colors", new ColorHandler("template/colors.html"));
server.run();
} catch (const Server::Exception& e) {
cerr << e.getMessage() << endl;
}
server.get("/music", new ShowPage("static/music.html"));
server.get("/music/moonlight.mp3", new ShowFile("static/moonlight.mp3", "audio/mpeg"));
}

int main(int argc, char** argv) {
try {
int port = argc > 1 ? std::stoi(argv[1]) : 5000;
Server server(port);
mapServerPaths(server);
std::cout << "Server running on port: " << port << std::endl;
server.run();
}
catch (const std::invalid_argument& e) {
std::cerr << e.what() << std::endl;
}
catch (const Server::Exception& e) {
std::cerr << e.getMessage() << std::endl;
}
return 0;
}
3 changes: 0 additions & 3 deletions examples/my_server.cpp

This file was deleted.

11 changes: 0 additions & 11 deletions examples/my_server.hpp

This file was deleted.

Loading