Files
android/libwvdrmengine/cdm/core/test/http_socket.h
Rahul Frias c41b6cb713 Request debug headers and log URL correctly
Merged from https://widevine-internal-review.googlesource.com/165861

We want debug headers to help diagnose b/186031735. I also
saw that we were only logging the domain name for some
errors instead of the full URL.

Bug: 186031735
Test: GtsMediaTestCases
Change-Id: I4d469a73e54f86d4d3b5d50bd0030fdb2a36df50
2023-02-22 15:09:22 -08:00

81 lines
2.5 KiB
C++

// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#ifndef CDM_TEST_HTTP_SOCKET_H_
#define CDM_TEST_HTTP_SOCKET_H_
#include <stdlib.h>
#include <ctime>
#include <string>
#include <gtest/gtest_prod.h>
#include <openssl/ssl.h>
#include "disallow_copy_and_assign.h"
namespace wvcdm {
// Provides basic Linux based TCP socket interface.
class HttpSocket {
public:
// A scheme (http:// or https://) is required for the URL.
explicit HttpSocket(const std::string& url);
~HttpSocket();
bool ConnectAndLogErrors(int timeout_in_ms);
void CloseSocket();
const std::string& scheme() const { return scheme_; }
bool secure_connect() const { return secure_connect_; }
const std::string& domain_name() const { return domain_name_; }
int port() const { return atoi(port_.c_str()); }
const std::string& resource_path() const { return resource_path_; }
std::string url() const {
return scheme_ + "://" + domain_name_ + ":" + port_ + resource_path_;
}
int ReadAndLogErrors(char* data, int len, int timeout_in_ms);
int WriteAndLogErrors(const char* data, int len, int timeout_in_ms);
private:
static bool ParseUrl(const std::string& url, std::string* scheme,
bool* secure_connect, std::string* domain_name,
std::string* port, std::string* path);
// The following three functions do the work without logging errors.
bool Connect(int timeout_in_ms);
int Read(char* data, int len, int timeout_in_ms);
int Write(const char* data, int len, int timeout_in_ms);
// Log times with a note as an error.
void LogTime(const char* note, const std::time_t& start,
const std::time_t& finish);
// Wait for a socket to be ready for reading or writing.
// Establishing a connection counts as "ready for write".
// Returns false on select error or timeout.
// Returns true when the socket is ready.
bool Wait(bool for_read, int timeout_in_ms);
FRIEND_TEST(HttpSocketTest, ParseUrlTest);
std::string scheme_;
bool secure_connect_;
std::string domain_name_;
std::string port_;
std::string resource_path_;
bool valid_url_;
int socket_fd_;
SSL* ssl_;
SSL_CTX* ssl_ctx_;
// When the socket was created. Logged on error to help debug flaky
// tests. e.g. b/186031735
std::time_t create_time_;
CORE_DISALLOW_COPY_AND_ASSIGN(HttpSocket);
};
} // namespace wvcdm
#endif // CDM_TEST_HTTP_SOCKET_H_