-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const std = @import("std"); | ||
|
||
pub fn build(b: *std.Build) void { | ||
const target = b.standardTargetOptions(.{}); | ||
const optimize = b.standardOptimizeOption(.{}); | ||
|
||
const libasio_dep = b.dependency("asio", .{ | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
const libasio = libasio_dep.artifact("asio"); | ||
|
||
const exe = b.addExecutable(.{ | ||
.name = "cpp-asio-httpclient", | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
// get include to zig-cache/i/{hash-pkg}/include | ||
for (libasio.include_dirs.items) |include| { | ||
exe.include_dirs.append(include) catch {}; | ||
} | ||
exe.addCSourceFile(.{ | ||
.file = .{ | ||
.path = "src/main.cpp", | ||
}, | ||
.flags = &.{ | ||
"-Wall", | ||
"-Wextra", | ||
"-Wshadow", | ||
}, | ||
}); | ||
exe.linkLibrary(libasio); | ||
exe.linkLibCpp(); | ||
|
||
b.installArtifact(exe); | ||
|
||
const run_cmd = b.addRunArtifact(exe); | ||
run_cmd.step.dependOn(b.getInstallStep()); | ||
|
||
if (b.args) |args| { | ||
run_cmd.addArgs(args); | ||
} | ||
|
||
const run_step = b.step("run", "Run the app"); | ||
run_step.dependOn(&run_cmd.step); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.{ | ||
.name = "cpp-asio-httpclient", | ||
.version = "0.1.0", | ||
.license = "MIT", | ||
.dependencies = .{ | ||
.asio = .{ | ||
.url = "https://github.com/kassane/asio/archive/2e97b6a4d37be85529d191380eeda67240fd61fe.tar.gz", | ||
.hash = "12208b60f54e758b964ad3038973b7c4198d40ed6d6ea2955b6e44cee971e6edeb5e", | ||
}, | ||
}, | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <asio.hpp> | ||
#include <iostream> | ||
|
||
using asio::ip::tcp; | ||
|
||
int main() { | ||
try { | ||
asio::io_context io_context; | ||
|
||
// Resolve the server address and port | ||
tcp::resolver resolver(io_context); | ||
tcp::resolver::results_type endpoints = | ||
resolver.resolve("localhost", "8000"); | ||
|
||
// Create a socket and connect to the server | ||
tcp::socket socket(io_context); | ||
asio::connect(socket, endpoints); | ||
|
||
// Prepare the HTTP request | ||
std::string request = "GET /get HTTP/1.1\r\n" | ||
"Host: localhost\r\n" | ||
"Connection: close\r\n" | ||
"\r\n"; | ||
|
||
// Send the request to the server | ||
asio::write(socket, asio::buffer(request)); | ||
|
||
// Read and print the response from the server | ||
asio::streambuf response_buffer; | ||
asio::read_until(socket, response_buffer, | ||
"\r\n\r\n"); // Read until headers end | ||
std::istream response_stream(&response_buffer); | ||
|
||
// Print the response headers | ||
std::string header; | ||
while (std::getline(response_stream, header) && header != "\r") | ||
std::cout << header << std::endl; | ||
|
||
// Read and print the response content | ||
std::string content; | ||
if (response_buffer.size() > 0) | ||
content.assign(asio::buffers_begin(response_buffer.data()), | ||
asio::buffers_end(response_buffer.data())); | ||
|
||
std::cout << "Response Content: " << content << std::endl; | ||
|
||
// Close the socket | ||
socket.close(); | ||
} catch (const std::exception &e) { | ||
std::cerr << "Error: " << e.what() << std::endl; | ||
} | ||
|
||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const std = @import("std"); | ||
|
||
pub fn build(b: *std.Build) void { | ||
const target = b.standardTargetOptions(.{}); | ||
const optimize = b.standardOptimizeOption(.{}); | ||
|
||
// Standalone-Server uses asio (standalone/non-boost) | ||
const libasio_dep = b.dependency("standaloneServer", .{ | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
const libasio = libasio_dep.artifact("Standalone-server"); | ||
|
||
const exe = b.addExecutable(.{ | ||
.name = "cpp-asio-httpserver", | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
// get include to zig-cache/i/{hash-pkg}/include | ||
for (libasio.include_dirs.items) |include| { | ||
exe.include_dirs.append(include) catch {}; | ||
} | ||
exe.addCSourceFile(.{ | ||
.file = .{ | ||
.path = "src/main.cpp", | ||
}, | ||
.flags = &.{ | ||
"-Wall", | ||
"-Wextra", | ||
"-Wshadow", | ||
}, | ||
}); | ||
// use standalone asio - non-boost | ||
exe.defineCMacro("ASIO_STANDALONE", null); | ||
exe.linkLibrary(libasio); | ||
exe.linkLibCpp(); | ||
|
||
b.installArtifact(exe); | ||
|
||
const run_cmd = b.addRunArtifact(exe); | ||
run_cmd.step.dependOn(b.getInstallStep()); | ||
|
||
if (b.args) |args| { | ||
run_cmd.addArgs(args); | ||
} | ||
|
||
const run_step = b.step("run", b.fmt("Run the {s} app", .{exe.name})); | ||
run_step.dependOn(&run_cmd.step); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.{ | ||
.name = "cpp-asio-httpserver", | ||
.version = "0.1.0", | ||
.license = "MIT", | ||
.dependencies = .{ | ||
.standaloneServer = .{ | ||
.url = "https://github.com/kassane/Standalone-Server/archive/12af18dfce121f5c6dc59702a0bbf99ce69f1677.tar.gz", | ||
.hash = "1220b0320efd82e58de88d8a7ea86777efd39cb6196b2a4abdcdf3e264707121770e", | ||
}, | ||
}, | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <server_http.hpp> | ||
|
||
using HttpServer = SimpleWeb::Server<SimpleWeb::HTTP>; | ||
|
||
int main() { | ||
// Create the HTTP server | ||
HttpServer server; | ||
server.config.port = 8000; | ||
|
||
// Define the request handler | ||
server.resource["^/get$"]["GET"] = [](std::shared_ptr<HttpServer::Response> response, std::shared_ptr<HttpServer::Request> request) { | ||
std::string content = "C++ Bits!\n"; | ||
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content; | ||
}; | ||
|
||
// Start the server | ||
std::cout << "Server started on port 8000." << std::endl; | ||
server.start(); | ||
|
||
return 0; | ||
} |