Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
HttpSession Class Reference

Manages a single HTTP session with a client. More...

+ Inheritance diagram for HttpSession:

Public Member Functions

 HttpSession (tcp::socket socket)
 Constructs an HttpSession with the given socket.
 
void start ()
 Starts processing the HTTP session.
 

Private Member Functions

void read_request ()
 Initiates asynchronous read of the HTTP request from the client.
 
void handle_request ()
 Processes the HTTP request and sends a corresponding response.
 

Private Attributes

tcp::socket socket_
 The socket used for communication with the client.
 
beast::flat_buffer buffer_
 Buffer for reading data from the socket.
 
http::request< http::string_body > req_
 HTTP request object.
 

Detailed Description

Manages a single HTTP session with a client.

This class handles reading an HTTP request, processing it by fetching a GitHub user's data, and sending an appropriate HTTP response. Each client connection is handled in its own instance.

Definition at line 36 of file server.cpp.

Constructor & Destructor Documentation

◆ HttpSession()

HttpSession::HttpSession ( tcp::socket socket)
inlineexplicit

Constructs an HttpSession with the given socket.

Parameters
socketThe TCP socket associated with the client connection.

Definition at line 42 of file server.cpp.

42: socket_(std::move(socket)) {}
tcp::socket socket_
The socket used for communication with the client.
Definition server.cpp:50

Member Function Documentation

◆ handle_request()

void HttpSession::handle_request ( )
inlineprivate

Processes the HTTP request and sends a corresponding response.

If the request target contains a valid GitHub username, it fetches the user's data and returns it as JSON. Otherwise, it responds with an error message.

Definition at line 74 of file server.cpp.

74 {
75 auto res = std::make_shared<http::response<http::string_body>>();
76 res->version(req_.version());
77 res->keep_alive(false);
78
79 std::string target(req_.target());
80 std::string username = target.length() > 1 ? target.substr(1) : "";
81
82 if (username.empty()) {
83 res->result(http::status::bad_request);
84 res->set(http::field::content_type, "text/plain");
85 res->body() = "Missing username in request URI.";
86 } else {
87 try {
88 User user = User::fetch_github_user(username);
89 nlohmann::json json = {{"login", user.login},
90 {"name", user.name},
91 {"company", user.company},
92 {"location", user.location}};
93 res->result(http::status::ok);
94 res->set(http::field::content_type, "application/json");
95 res->body() = json.dump();
96 } catch (const std::exception &e) {
97 res->result(http::status::not_found);
98 res->set(http::field::content_type, "text/plain");
99 res->body() = std::string("Error fetching user: ") + e.what();
100 }
101 }
102
103 res->prepare_payload();
104
105 auto self = shared_from_this();
106 http::async_write(
107 socket_, *res,
108 [self, res](beast::error_code /*error_code*/, std::size_t) {
109 try {
110 self->socket_.shutdown(tcp::socket::shutdown_send);
111 } catch (const boost::system::system_error &system_error) {
112 std::cerr << "Shutdown error: " << system_error.what() << '\n';
113 }
114 });
115 }
http::request< http::string_body > req_
HTTP request object.
Definition server.cpp:52
decltype(sizeof(void *)) size_t
Definition doctest.h:524
Structure representing a GitHub user.
Definition user.h:9
const char * name
Full name.
Definition user.h:11
const char * login
GitHub username.
Definition user.h:10
const char * location
User's location.
Definition user.h:13
const char * company
Company name.
Definition user.h:12

References User::company, User::location, User::login, User::name, req_, and socket_.

◆ read_request()

void HttpSession::read_request ( )
inlineprivate

Initiates asynchronous read of the HTTP request from the client.

Definition at line 57 of file server.cpp.

57 {
58 auto self = shared_from_this();
59 http::async_read(socket_, buffer_, req_,
60 [self](beast::error_code error_code, std::size_t) {
61 if (!error_code) {
62 self->handle_request();
63 }
64 });
65 }
beast::flat_buffer buffer_
Buffer for reading data from the socket.
Definition server.cpp:51

References buffer_, req_, and socket_.

Referenced by start().

◆ start()

void HttpSession::start ( )
inline

Starts processing the HTTP session.

Definition at line 47 of file server.cpp.

47{ read_request(); }
void read_request()
Initiates asynchronous read of the HTTP request from the client.
Definition server.cpp:57

References read_request().

Field Documentation

◆ buffer_

beast::flat_buffer HttpSession::buffer_
private

Buffer for reading data from the socket.

Definition at line 51 of file server.cpp.

Referenced by read_request().

◆ req_

http::request<http::string_body> HttpSession::req_
private

HTTP request object.

Definition at line 52 of file server.cpp.

Referenced by handle_request(), and read_request().

◆ socket_

tcp::socket HttpSession::socket_
private

The socket used for communication with the client.

Definition at line 50 of file server.cpp.

Referenced by handle_request(), and read_request().


The documentation for this class was generated from the following file: