aboutsummaryrefslogtreecommitdiff
path: root/src/net/http_client.cc
blob: b31073ce0508ff6c50f73787b14aa36d26c423ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Ricochet - A minimalist CLI web browser for fast, distraction-free navigation.
 * Copyright (C) 2026  Richard Qin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "ricochet/net/http_client.hh"

#include "ricochet/net/sys/address.hh"
#include "ricochet/net/sys/socket.hh"

#include <cstddef>
#include <format>
#include <string>

namespace ricochet::net {

namespace {

struct ParsedUrl
{
  std::string host {};
  std::string path {};
};

std::expected<ParsedUrl, std::string> parse_url( std::string_view url )
{
  const std::string_view http_prefix = "http://";

  if ( !url.starts_with( http_prefix ) ) {
    return std::unexpected( "Only 'http://' URLs are supported in MVP. (e.g., http://example.com)" );
  }

  url.remove_prefix( http_prefix.length() );
  auto slash_pos = url.find( '/' );

  ParsedUrl result;
  if ( slash_pos == std::string_view::npos ) {
    result.host = std::string( url );
    result.path = "/";
  } else {
    result.host = std::string( url.substr( 0, slash_pos ) );
    result.path = std::string( url.substr( slash_pos ) );
  }

  return result;
}
} // namespace

std::expected<HttpResponse, std::string>
HttpClient::fetch( // NOLINT(readability-convert-member-functions-to-static)
  std::string_view url ) const
{
  auto parsed_url = parse_url( url );
  if ( !parsed_url.has_value() ) {
    return std::unexpected( parsed_url.error() );
  }

  auto [host, path] = parsed_url.value();

  try {
    const Address server_addr( host, "http" );

    TCPSocket socket;
    socket.connect( server_addr );

    const std::string request = std::format( "GET {} HTTP/1.1\r\n"
                                             "Host: {}\r\n"
                                             "User-Agent: Ricochet/0.1.0\r\n"
                                             "Connection: close\r\n\r\n",
                                             path,
                                             host );

    size_t bytes_written = 0;
    while ( bytes_written < request.size() ) {
      bytes_written += socket.write( request.substr( bytes_written ) );
    }

    std::string raw_response;
    while ( !socket.eof() ) {
      std::string buffer;
      socket.read( buffer );
      raw_response += buffer;
    }

    HttpResponse response;
    auto header_end = raw_response.find( "\r\n\r\n" );

    if ( header_end != std::string::npos ) {
      response.body = raw_response.substr( header_end + 4 );
      if ( raw_response.size() >= 12 ) {
        try {
          response.status_code = std::stoi( raw_response.substr( 9, 3 ) );
        } catch ( ... ) {
          response.status_code = 0;
        }
      }
    } else {
      response.body = raw_response;
    }

    return response;
  } catch ( const std::exception& e ) {
    return std::unexpected( std::string( "Network Error: " ) + e.what() );
  }
}

} // namespace ricochet::net