aboutsummaryrefslogtreecommitdiff
path: root/src/net/url_parser.cc
blob: 95e86ef5f89b73aa41e0a0b464b2692feab02d2b (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
/*
 * 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/url_parser.hh"

#include <cctype>

namespace ricochet::net {

std::expected<ParsedUrl, std::string> parse_url( std::string_view url )
{
  std::string_view prefix = "http://";
  if ( url.starts_with( "https://" ) ) {
    prefix = "https://";
  } else if ( url.starts_with( "http://" ) ) {
    prefix = "http://";
  } else {
    return std::unexpected( "Only 'http://' or 'https://' URLs are supported." );
  }

  url.remove_prefix( prefix.length() );

  const auto sep_pos = url.find_first_of( "/?#" );

  ParsedUrl result;
  if ( sep_pos == std::string_view::npos ) {
    result.host = std::string( url );
    result.path = "/";
  } else {
    result.host = std::string( url.substr( 0, sep_pos ) );
    std::string_view path_view = url.substr( sep_pos );
    const auto hash_pos = path_view.find( '#' );
    if ( hash_pos != std::string_view::npos ) {
      path_view = path_view.substr( 0, hash_pos );
    }
    result.path = std::string( path_view );
    if ( result.path.empty() || result.path[0] != '/' ) {
      result.path.insert( 0, "/" );
    }
  }
  return result;
}

std::string extract_redirect_url( const std::string& raw_response, bool is_https, const std::string& host )
{
  auto loc_pos = raw_response.find( "Location: " );
  if ( loc_pos == std::string::npos ) {
    loc_pos = raw_response.find( "location: " );
  }
  if ( loc_pos != std::string::npos ) {
    loc_pos += 10;
    auto end_pos = raw_response.find( '\r', loc_pos );
    if ( end_pos == std::string::npos ) {
      end_pos = raw_response.find( '\n', loc_pos );
    }
    if ( end_pos != std::string::npos ) {
      std::string next_url = raw_response.substr( loc_pos, end_pos - loc_pos );
      while ( !next_url.empty() && std::isspace( static_cast<unsigned char>( next_url.front() ) ) ) {
        next_url.erase( 0, 1 );
      }
      if ( next_url.starts_with( "//" ) ) {
        next_url = ( is_https ? "https:" : "http:" ) + next_url;
      } else if ( next_url.starts_with( "/" ) ) {
        next_url = ( is_https ? "https://" : "http://" ) + host + next_url;
      }
      return next_url;
    }
  }
  return "";
}

} // namespace ricochet::net