Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto PR from release/0.6 to release/0.7 #280

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion fs/httpfs/httpfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ limitations under the License.
#include <photon/fs/filesystem.h>

namespace photon {
namespace net {
namespace http {
class Client;
}
}

namespace fs {
enum HTTPFileFlags {
HTTP_HEADER = 0xF01, // (const char*, const char*) ... for header
Expand Down Expand Up @@ -68,7 +74,9 @@ IFile* new_httpfile(const char* url, IFileSystem* httpfs = nullptr,

IFileSystem* new_httpfs_v2(bool default_https = false,
uint64_t conn_timeout = -1UL,
uint64_t stat_expire = -1UL);
uint64_t stat_expire = -1UL,
net::http::Client* client = nullptr,
bool client_ownership = false);

IFile* new_httpfile_v2(const char* url, IFileSystem* httpfs = nullptr,
uint64_t conn_timeout = -1UL,
Expand Down
26 changes: 18 additions & 8 deletions fs/httpfs/httpfs_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,24 @@ class HttpFs_v2 : public fs::IFileSystem {
uint64_t m_stat_timeout;

net::http::Client *m_client;
bool m_client_ownership;

public:
HttpFs_v2(bool default_https, uint64_t conn_timeout, uint64_t stat_timeout)
: m_default_https(default_https),
m_conn_timeout(conn_timeout),
HttpFs_v2(bool default_https, uint64_t conn_timeout, uint64_t stat_timeout,
net::http::Client* client, bool client_ownership)
: m_default_https(default_https), m_conn_timeout(conn_timeout),
m_stat_timeout(stat_timeout) {
m_client = net::http::new_http_client();
}
if (client == nullptr) {
m_client = net::http::new_http_client();
m_client_ownership = true;
} else {
m_client = client;
m_client_ownership = client_ownership;
}
}
~HttpFs_v2() {
delete m_client;
if (m_client_ownership)
delete m_client;
}
net::http::Client* get_client() { return m_client; }
IFile* open(const char* pathname, int flags) override;
Expand Down Expand Up @@ -265,8 +273,10 @@ IFile* HttpFs_v2::open(const char* pathname, int flags) {
}

IFileSystem* new_httpfs_v2(bool default_https, uint64_t conn_timeout,
uint64_t stat_timeout) {
return new HttpFs_v2(default_https, conn_timeout, stat_timeout);
uint64_t stat_timeout, net::http::Client* client,
bool client_ownership) {
return new HttpFs_v2(default_https, conn_timeout, stat_timeout,
client, client_ownership);
}

IFile* new_httpfile_v2(const char* url, HttpFs_v2* httpfs, uint64_t conn_timeout,
Expand Down
2 changes: 1 addition & 1 deletion net/http/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ int Request::redirect(Verb v, estring_view location, bool enable_proxy) {
else
new_request_line_size += u.target().size();

auto delta = new_request_line_size - m_buf_size;
int delta = (int)new_request_line_size - m_buf_size;
LOG_DEBUG(VALUE(delta));
if (headers.reset_host(delta, u.host_port()) < 0)
LOG_ERROR_RETURN(0, -1, "failed to move header data");
Expand Down
27 changes: 27 additions & 0 deletions net/http/test/client_function_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ TEST(http_client, get) {
resp_body_buf[10] = '\0';
LOG_DEBUG(resp_body_buf);
EXPECT_EQ(0, strcmp("http_clien", resp_body_buf));

static const char target_tb[] = "http://www.taobao.com?x";
auto op5 = client->new_operation(Verb::GET, target_tb);
DEFER(delete op5);
op5->req.headers.content_length(0);
op5->call();
EXPECT_EQ(op5->resp.status_code(), 200);
}

int body_check_handler(void*, Request &req, Response &resp, std::string_view) {
Expand Down Expand Up @@ -517,6 +524,26 @@ TEST(url, url_escape_unescape) {
);
}

TEST(url, path_fix) {
static const char url0[] = "http://xxx.com/yyy?a=b&c=d";
URL u0(url0);
EXPECT_EQ(u0.target(), "/yyy?a=b&c=d");
EXPECT_EQ(u0.path(), "/yyy");
EXPECT_EQ(u0.query(), "a=b&c=d");

static const char url1[] = "http://xxx.com";
URL u1(url1);
EXPECT_EQ(u1.target(), "/");
EXPECT_EQ(u1.path(), "/");
EXPECT_EQ(u1.query(), "");

static const char url2[] = "http://xxx.com?a=b";
URL u2(url2);
EXPECT_EQ(u2.target(), "/?a=b");
EXPECT_EQ(u2.path(), "/");
EXPECT_EQ(u2.query(), "a=b");
}

// Only for manual test
// TEST(http_client, proxy) {
// auto client = new_http_client();
Expand Down
16 changes: 16 additions & 0 deletions net/http/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,23 @@ namespace photon {
namespace net {
namespace http {

void URL::fix_target() {
auto t = (m_url | m_target);
if (m_target.size() == 0 || t.front() != '/') {
m_tmp_target = (char*)malloc(m_target.size() + 1);
m_tmp_target[0] = '/';
strncpy(m_tmp_target+1, t.data(), t.size());
m_target = rstring_view16(0, m_target.size()+1);
m_path = rstring_view16(0, m_path.size()+1);
}
}

void URL::from_string(std::string_view url) {
DEFER(fix_target(););
if (m_tmp_target) {
free((void*)m_tmp_target);
m_tmp_target = nullptr;
}
m_url = url.begin();
size_t pos = 0;
LOG_DEBUG(VALUE(url));
Expand Down
14 changes: 11 additions & 3 deletions net/http/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ class URL {
rstring_view16 m_fragment;
uint16_t m_port = 0;
bool m_secure;
char *m_tmp_target = nullptr;
public:
URL() = default;
URL(std::string_view url) { from_string(url); }
~URL() {
free((void*)m_tmp_target);
}

std::string to_string() {
return m_url;
Expand All @@ -61,15 +65,19 @@ class URL {
}

void from_string(std::string_view url);
void fix_target();
std::string_view query() const { return m_url | m_query; }

std::string_view path() const {
return m_url | m_path;
if (m_tmp_target != nullptr)
return (m_tmp_target | m_path);
return (m_url | m_path);
}

std::string_view target() const {
return m_target.size() == 0 ?
std::string_view("/") : (m_url | m_target);
if (m_tmp_target != nullptr)
return (m_tmp_target | m_target);
return (m_url | m_target);
}

std::string_view host() const { return m_url | m_host; }
Expand Down