Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
c668d8f
temp work
BrandonMFong Feb 26, 2025
0433f7e
saving work
BrandonMFong Feb 26, 2025
b9da158
updating
BrandonMFong Feb 26, 2025
b6d7035
updating libs
BrandonMFong Mar 25, 2025
b6c2c38
saving work
BrandonMFong Mar 25, 2025
f8b38a4
Can read input
BrandonMFong Apr 2, 2025
7e15e38
can handle requests on separate queue
BrandonMFong Apr 3, 2025
607721e
moving logic to office source file
BrandonMFong Apr 3, 2025
0d6caa3
adding notes
BrandonMFong Apr 3, 2025
c249779
adding request and response files
BrandonMFong Apr 3, 2025
794805b
init test bench
BrandonMFong Apr 3, 2025
fc536cf
saving work
BrandonMFong Apr 3, 2025
43b5607
can get method
BrandonMFong Apr 8, 2025
4f27ffc
can parse request with regex
BrandonMFong Apr 8, 2025
abebf6b
introduction resource and log
BrandonMFong Apr 9, 2025
67c1bc8
Adding example root folder
BrandonMFong Apr 10, 2025
b430a79
separating by method
BrandonMFong Apr 10, 2025
4b44cab
structuring out
BrandonMFong Apr 11, 2025
c6eb256
can create http response
BrandonMFong Apr 11, 2025
079d36f
trying to connect
BrandonMFong Apr 13, 2025
73edab8
can send 404 responses
BrandonMFong Apr 14, 2025
f66b1fb
can get content from server just ONCE
BrandonMFong Apr 14, 2025
b8114b7
saving work
BrandonMFong Apr 14, 2025
c0469d7
can take multiple requests
BrandonMFong Apr 14, 2025
4ec9387
lib update
BrandonMFong Apr 15, 2025
a26c652
testing memory
BrandonMFong Apr 15, 2025
0c20c54
saving work
BrandonMFong Apr 15, 2025
39a0f74
lib changes
BrandonMFong Apr 17, 2025
c07b4ed
can serve https://github.com/cloudacademy/static-website-example
BrandonMFong Apr 17, 2025
cc7a454
can get target absolute path
BrandonMFong Apr 17, 2025
15966b2
using a helper function
BrandonMFong Apr 17, 2025
af5637e
saving work
BrandonMFong Apr 17, 2025
e1d6576
updating libs
BrandonMFong Apr 17, 2025
852640c
lib update
BrandonMFong Apr 17, 2025
e137bf0
adding tests
BrandonMFong Apr 17, 2025
7525b65
updating
BrandonMFong Apr 17, 2025
85466c0
only worrying about the target path
BrandonMFong Apr 17, 2025
2bb071a
disable sanitizer
BrandonMFong Apr 17, 2025
ad90cde
update prep
BrandonMFong Apr 17, 2025
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
48 changes: 48 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: CI

on:
push:
branches: [ "dev" ]
paths-ignore:
- '*.md'
- 'docs/**'
pull_request:
branches: [ "dev" ]
paths-ignore:
- '*.md'
- 'docs/**'

jobs:
unit-tests-linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
token: ${{ secrets.PAT_TOKEN }}
- name: Install uuid-dev
run: |
sudo apt -y update;
sudo apt -y upgrade;
sudo apt -y install uuid-dev;
- name: building libs
run: |
cd external;
make build-libs;
- name: building/running tests
run: make build CONFIG=test run

unit-tests-macos:
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
token: ${{ secrets.PAT_TOKEN }}
- name: building libs
run: |
cd external;
make build-libs;
- name: building/running tests
run: make build CONFIG=test run

12 changes: 12 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>
</html>
2 changes: 1 addition & 1 deletion external/libs
Submodule libs updated 46 files
+1 −1 bflibc/makefile
+18 −0 bflibc/src/bfmath.c
+5 −0 bflibc/src/bfmath.h
+2 −0 bflibc/src/lock.c
+12 −0 bflibc/testbench/math_tests.h
+2 −2 bflibcpp/makefile
+16 −0 bflibcpp/src/array.hpp
+4 −0 bflibcpp/src/atomic.hpp
+22 −2 bflibcpp/src/basicmap.hpp
+1 −0 bflibcpp/src/bflibcpp.hpp
+4 −0 bflibcpp/src/bintree.hpp
+4 −0 bflibcpp/src/collection.hpp
+45 −1 bflibcpp/src/data.cpp
+15 −1 bflibcpp/src/data.hpp
+1 −1 bflibcpp/src/directory.hpp
+1 −1 bflibcpp/src/file.hpp
+1 −0 bflibcpp/src/hashmap.hpp
+12 −0 bflibcpp/src/object.cpp
+16 −5 bflibcpp/src/object.hpp
+1 −1 bflibcpp/src/path.hpp
+52 −4 bflibcpp/src/string.cpp
+24 −2 bflibcpp/src/string.hpp
+4 −0 bflibcpp/src/time.cpp
+1 −0 bflibcpp/src/time.hpp
+104 −0 bflibcpp/src/url.cpp
+63 −0 bflibcpp/src/url.hpp
+17 −0 bflibcpp/testbench/array_tests.hpp
+25 −37 bflibcpp/testbench/data_tests.hpp
+11 −1 bflibcpp/testbench/hashmap_tests.hpp
+32 −36 bflibcpp/testbench/string_tests.hpp
+2 −2 bflibcpp/testbench/tests.cpp
+78 −0 bflibcpp/testbench/url_tests.hpp
+6 −1 bflibcpp/todo.md
+2 −2 bfnet/makefile
+1 −0 bfnet/notes.md
+0 −3 bfnet/src/bfnet.hpp
+0 −33 bfnet/src/buffer.cpp
+0 −34 bfnet/src/buffer.hpp
+46 −46 bfnet/src/connection.cpp
+12 −4 bfnet/src/connection.hpp
+5 −6 bfnet/src/envelope.cpp
+6 −6 bfnet/src/envelope.hpp
+23 −13 bfnet/src/socket.cpp
+31 −6 bfnet/src/socket.hpp
+128 −11 bfnet/testbench/socket_tests.hpp
+2 −0 bfnet/todo.md
24 changes: 12 additions & 12 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,41 @@ BIN_PATH = bin/$(CONFIG)
BUILD_TYPE = executable
SOURCE_EXT = cpp
HEADER_EXT = hpp
FILES = \
FILES = office request response resource log

ifeq ($(CONFIG),release) # release
LIBRARIES += \
external/bin/libs/release/bflibc/libbfc.a \
LIBRARIES = \
external/bin/libs/release/bfnet/libbfnet.a \
external/bin/libs/release/bflibcpp/libbfcpp.a \
external/bin/libs/release/bfnet/libbfnet.a
external/bin/libs/release/bflibc/libbfc.a
else
LIBRARIES += \
external/bin/libs/debug/bflibc/libbfc-debug.a \
LIBRARIES = \
external/bin/libs/debug/bfnet/libbfnet-debug.a \
external/bin/libs/debug/bflibcpp/libbfcpp-debug.a \
external/bin/libs/debug/bfnet/libbfnet-debug.a
external/bin/libs/debug/bflibc/libbfc-debug.a
endif

LINKS = $(BF_LIB_C_FLAGS)
LINKS = -lpthread $(BF_LIB_C_FLAGS) -ldl

### Release settings
ifeq ($(CONFIG),release) # release
MAIN_FILE = src/main.cpp
BIN_NAME = http
FLAGS = $(CPPFLAGS) -Isrc/ $(CPPSTD) -Iexternal/bin/libs/release $(OPENSSL_INCLUDE_PATH)
FLAGS = $(CPPFLAGS) -Isrc/ $(CPPSTD) -Iexternal/bin/libs/release

### Debug settings
else ifeq ($(CONFIG),debug) # debug
MAIN_FILE = src/main.cpp
BIN_NAME = http
BIN_NAME = http-debug
#ADDR_SANITIZER = -fsanitize=address
FLAGS = $(CPPFLAGS) -DDEBUG -g -Isrc/ $(ADDR_SANITIZER) $(CPPSTD) -Iexternal/bin/libs/debug $(OPENSSL_INCLUDE_PATH)
FLAGS = $(CPPFLAGS) -DDEBUG -g -Isrc/ $(ADDR_SANITIZER) $(CPPSTD) -Iexternal/bin/libs/debug

### Test settings
else ifeq ($(CONFIG),test) # test
MAIN_FILE = testbench/tests.cpp
BIN_NAME = http-test
#ADDR_SANITIZER = -fsanitize=address
FLAGS = $(CPPFLAGS) -DDEBUG -DTESTING -g -Isrc/ $(ADDR_SANITIZER) $(CPPSTD) -Iexternal/bin/libs/debug $(OPENSSL_INCLUDE_PATH)
FLAGS = $(CPPFLAGS) -DDEBUG -DTESTING -g -Isrc/ $(ADDR_SANITIZER) $(CPPSTD) -Iexternal/bin/libs/debug
LIBRARIES += external/bin/libs/debug/bftest/libbftest-debug.a
endif # ($(CONFIG),...)

Expand Down
1 change: 1 addition & 0 deletions notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Using https://developer.mozilla.org/en-US/docs/Web/HTTP as a guide
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# http
An http server using POSIX sockets

## Resources
* [How I Built a Simple HTTP Server from Scratch using C ](https://dev.to/jeffreythecoder/how-i-built-a-simple-http-server-from-scratch-using-c-739)
* [Mozilla HTTP Documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP)

66 changes: 66 additions & 0 deletions src/log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* author: brando
* date: 4/9/25
*/

#include "log.hpp"
#include <stdarg.h>
#include <bflibcpp/bflibcpp.hpp>
#include <sys/types.h>
#include <unistd.h>

extern "C" {
#include <bflibc/bflibc.h>
}

void _LogWriteEntry(BFFileWriter * filewriter, int mode, ...) {
#ifndef TESTING
if (!filewriter) return;

va_list arg0, arg1;
va_start(arg0, mode);
va_start(arg1, mode);

const char * format = va_arg(arg0, const char *);
if (!format) return;

char * logstr = BFStringCreateFormatArgListString(format, arg0);
if (!logstr) return;

BFDateTime dt = {0};
if (BFTimeGetCurrentDateTime(&dt)) return;

format = "[%02d/%02d/%04d, %02d:%02d:%02d] - %s";

BFFileWriterQueueFormatLine(
filewriter,
format,
dt.month,
dt.day,
dt.year,
dt.hour,
dt.minute,
dt.second,
logstr
);

printf(
format,
dt.month,
dt.day,
dt.year,
dt.hour,
dt.minute,
dt.second,
logstr
);
printf("\n");
fflush(stdout);

va_end(arg0);
va_end(arg1);

BFFree(logstr);
#endif
}

72 changes: 72 additions & 0 deletions src/log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* author: brando
* date: 4/9/25
*/

#ifndef LOG_HPP
#define LOG_HPP

extern "C" {
#include <bflibc/filewriter.h>
}

#define CHAT_LOG_PATH "/tmp/http.log"

extern BFFileWriter gFileWriter;

/**
* must define in a source file
*/
#define LOG_INIT \
BFFileWriter gFileWriter = 0; \
void __LogCallbackBFNet(const char * buf) { \
LOG_DEBUG("bfnet: %s", buf); \
LOG_FLUSH; \
}

/**
* initializes logs for:
* - http
* - bfnet
*/
#define LOG_OPEN \
BFFileWriterCreate(&gFileWriter, CHAT_LOG_PATH); \
BF::Net::Log::SetCallback(__LogCallbackBFNet);

/**
* `mode`: 'd' for debug, 'e' for error, or 0 for normal
*/
void _LogWriteEntry(BFFileWriter * filewriter, int mode, ...);

/**
* LOG_WRITE vs LOG_DEBUG vs LOG_ERROR
*
* - each writes a line into the same log file
* - each log entry will explicitly tell you what type of log entry it is
*/

/**
* writes ent (line) to log file
*/
#define LOG_WRITE(...) _LogWriteEntry(&gFileWriter, 0, __VA_ARGS__)

#define LOG_ERROR(...) _LogWriteEntry(&gFileWriter, 'e', __VA_ARGS__)

#ifdef DEBUG
#define LOG_DEBUG(...) _LogWriteEntry(&gFileWriter, 'd', __VA_ARGS__)
#else // DEBUG
#define LOG_DEBUG(...)
#endif // DEBUG

/**
* flushes write buffers
*/
#define LOG_FLUSH BFFileWriterFlush(&gFileWriter)

/**
* closes log file
*/
#define LOG_CLOSE BFFileWriterClose(&gFileWriter)

#endif // LOG_HPP

83 changes: 80 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,87 @@
* date: 2/26/25
*/

#include <stdio.h>
#include "office.hpp"
#include "log.hpp"
#include "resource.hpp"
#include <cstdio>
#include <bflibcpp/bflibcpp.hpp>
#include <bfnet/bfnet.hpp>
#include <iostream>

extern "C" {
#include <bflibc/bflibc.h>
}

#define ARGUMENT_ROOT "-root"

using namespace BF::Net;
using namespace BF;
using namespace std;

LOG_INIT;

void help(const char * toolname) {
printf("usage: %s %s <path>\n", toolname, ARGUMENT_ROOT);
printf("\n");
printf("Arguments:\n");
printf(" %s <path>\tThis is the root folder where we will look for resources\n", ARGUMENT_ROOT);
printf("\nCopyright © 2025 Brando. All rights reserved.\n");
}

void __NewConnection(Connection * sc) {
LOG_WRITE("new connection made");
}

int __ReadArguments(int argc, char * argv[]) {
if (argc == 1) {
help(argv[0]);
return -1;
}

for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], ARGUMENT_ROOT)) {
if (!Resource::setRootFolder(argv[++i])) {
LOG_ERROR("'%s' is not accepted as a root folder", argv[i]);
}
}
}

int main() {
printf("Hello world!\n");
return 0;
}

int main(int argc, char * argv[]) {
LOG_OPEN;

if (__ReadArguments(argc, argv)) {
return -1;
}

int error = 0;
Log::SetCallback(__LogCallbackBFNet);

Office::start();
Socket * skt = Socket::create(SOCKET_MODE_SERVER, "0.0.0.0", 8080, &error);
if (!error) {
skt->setInStreamCallback(Office::envelopeReceive);
skt->setNewConnectionCallback(__NewConnection);
skt->setBufferSize(1024 * 1024 * 100);
error = skt->start();
}

if (!error) {
cout << "Press any key to stop...";
cin.get();
error = skt->stop();

cout << "Stopped..." << endl;
}

BFRelease(skt);
Office::stop();

LOG_CLOSE;

return error;
}

Loading