Size: 2213 bytes.


 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
// cs/net/http/main.cc
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#include <cstring>
#include <iostream>
#include <string>

int main() {
  int socket_desc;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  char buffer[4096];

  std::string host =
      "special-goggles-r4r46pg7jp7cxwjp-8080.app.github."
      "dev";
  // "HTTPS is more than just connecting on TCP port 443.
  // Much, much, much, much, more. You have to implement the
  // TLS protocol. All 100 pages of it. – President James K.
  // Polk May 26, 2020" See:
  // https://stackoverflow.com/questions/62010516/i-am-trying-to-use-get-without-using-sockets-and-i-keep-getting-400-bad-request
  int port = 443;
  std::string path = "/";
  std::string params = "";

  socket_desc = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_desc < 0) {
    std::cout << "failed to create socket" << std::endl;
    return 0;
  }

  server = gethostbyname(host.c_str());
  if (server == NULL) {
    std::cout << "could Not resolve hostname :("
              << std::endl;
    close(socket_desc);
    return 0;
  }

  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(port);
  bcopy((char *)server->h_addr,
        (char *)&serv_addr.sin_addr.s_addr,
        server->h_length);

  if (connect(socket_desc, (struct sockaddr *)&serv_addr,
              sizeof(serv_addr)) < 0) {
    std::cout << "connection failed :(" << std::endl;
    close(socket_desc);
    return 0;
  }

  std::string request = "GET " + path + params +
                        " HTTP/1.1\r\nHost: " + host +
                        "\r\nConnection: close\r\n\r\n";

  if (send(socket_desc, request.c_str(), request.size(),
           0) < 0) {
    std::cout << "failed to send request..." << std::endl;
    close(socket_desc);
    return 0;
  }

  int n;
  std::string raw_site;
  while ((n = recv(socket_desc, buffer, sizeof(buffer),
                   0)) > 0) {
    raw_site.append(buffer, n);
  }

  close(socket_desc);

  std::cout << raw_site.size() << " " << raw_site
            << std::endl;
  return 0;
}
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-2:8080 in 4ms.