Skip to content

Commit 40e63f3

Browse files
authored
Merge pull request #133 from Mytherin/movesource
Move sources to src/ directory
2 parents f45bdd1 + 24786b5 commit 40e63f3

32 files changed

+148
-126
lines changed

CMakeLists.txt

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,15 @@
11
cmake_minimum_required(VERSION 2.8.12...3.29)
22

3-
set(HTTPFS_BASE_FOLDER "extension/httpfs")
4-
53
project(HTTPFsExtension)
64

75
add_extension_definitions()
86

9-
include_directories(extension/httpfs/include
10-
${DUCKDB_MODULE_BASE_DIR}/third_party/httplib)
11-
127
if (NOT EMSCRIPTEN)
13-
set(EXTRA_SOURCES extension/httpfs/crypto.cpp extension/httpfs/httpfs_httplib_client.cpp extension/httpfs/httpfs_curl_client.cpp)
148
add_definitions(-DOVERRIDE_ENCRYPTION_UTILS=1)
159
else()
16-
set(EXTRA_SOURCES extension/httpfs/httpfs_client_wasm.cpp)
17-
set (DUCKDB_EXTENSION_HTTPFS_LINKED_LIBS "../../third_party/mbedtls/libduckdb_mbedtls.a")
10+
set(DUCKDB_EXTENSION_HTTPFS_LINKED_LIBS "../../third_party/mbedtls/libduckdb_mbedtls.a")
1811
endif()
1912

20-
build_static_extension(
21-
httpfs
22-
extension/httpfs/hffs.cpp
23-
extension/httpfs/s3fs.cpp
24-
extension/httpfs/httpfs.cpp
25-
extension/httpfs/http_state.cpp
26-
extension/httpfs/crypto.cpp
27-
extension/httpfs/hash_functions.cpp
28-
extension/httpfs/create_secret_functions.cpp
29-
extension/httpfs/httpfs_extension.cpp
30-
${EXTRA_SOURCES} )
31-
32-
set(PARAMETERS "-warnings")
33-
build_loadable_extension(
34-
httpfs
35-
${PARAMETERS}
36-
extension/httpfs/hffs.cpp
37-
extension/httpfs/s3fs.cpp
38-
extension/httpfs/httpfs.cpp
39-
extension/httpfs/http_state.cpp
40-
extension/httpfs/crypto.cpp
41-
extension/httpfs/hash_functions.cpp
42-
extension/httpfs/create_secret_functions.cpp
43-
extension/httpfs/httpfs_extension.cpp
44-
${EXTRA_SOURCES} )
45-
4613
if(MINGW)
4714
set(OPENSSL_USE_STATIC_LIBS TRUE)
4815
endif()
@@ -51,6 +18,17 @@ find_package(OpenSSL REQUIRED)
5118
find_package(CURL REQUIRED)
5219
include_directories(${OPENSSL_INCLUDE_DIR})
5320
include_directories(${CURL_INCLUDE_DIRS})
21+
22+
include_directories(src/include
23+
${DUCKDB_MODULE_BASE_DIR}/third_party/httplib)
24+
add_subdirectory(src)
25+
set(EXTENSION_SOURCES ${ALL_OBJECT_FILES})
26+
27+
build_static_extension(httpfs ${EXTENSION_SOURCES})
28+
29+
set(PARAMETERS "-warnings")
30+
build_loadable_extension(httpfs ${PARAMETERS} ${EXTENSION_SOURCES})
31+
5432
if(EMSCRIPTEN)
5533
target_link_libraries(httpfs_loadable_extension duckdb_mbedtls)
5634
else()

extension_config.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ duckdb_extension_load(parquet)
66

77
duckdb_extension_load(httpfs
88
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}
9-
INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/extension/httpfs/include
9+
INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/src/include
1010
)

src/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
set(HTTPFS_SOURCES
2+
hffs.cpp
3+
s3fs.cpp
4+
httpfs.cpp
5+
http_state.cpp
6+
crypto.cpp
7+
hash_functions.cpp
8+
create_secret_functions.cpp
9+
httpfs_extension.cpp)
10+
if(NOT EMSCRIPTEN)
11+
set(HTTPFS_SOURCES ${HTTPFS_SOURCES} crypto.cpp httpfs_httplib_client.cpp
12+
httpfs_curl_client.cpp)
13+
else()
14+
set(HTTPFS_SOURCES ${HTTPFS_SOURCES} httpfs_client_wasm.cpp)
15+
endif()
16+
17+
add_library(httpfs_library OBJECT ${HTTPFS_SOURCES})
18+
set(ALL_OBJECT_FILES
19+
${ALL_OBJECT_FILES} $<TARGET_OBJECTS:httpfs_library>
20+
PARENT_SCOPE)
File renamed without changes.
File renamed without changes.

extension/httpfs/crypto.cpp renamed to src/crypto.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323

2424
namespace duckdb {
2525

26-
AESStateSSL::AESStateSSL(EncryptionTypes::CipherType cipher_p, idx_t key_len) : EncryptionState(cipher_p, key_len), context(EVP_CIPHER_CTX_new()) {
26+
AESStateSSL::AESStateSSL(EncryptionTypes::CipherType cipher_p, idx_t key_len)
27+
: EncryptionState(cipher_p, key_len), context(EVP_CIPHER_CTX_new()) {
2728
if (!(context)) {
2829
throw InternalException("OpenSSL AES failed with initializing context");
2930
}
@@ -83,7 +84,8 @@ void AESStateSSL::GenerateRandomData(data_ptr_t data, idx_t len) {
8384
RAND_bytes(data, len);
8485
}
8586

86-
void AESStateSSL::InitializeEncryption(const_data_ptr_t iv, idx_t iv_len, const_data_ptr_t key, idx_t key_len_p, const_data_ptr_t aad, idx_t aad_len) {
87+
void AESStateSSL::InitializeEncryption(const_data_ptr_t iv, idx_t iv_len, const_data_ptr_t key, idx_t key_len_p,
88+
const_data_ptr_t aad, idx_t aad_len) {
8789
mode = EncryptionTypes::ENCRYPT;
8890

8991
if (key_len_p != key_len) {
@@ -101,14 +103,15 @@ void AESStateSSL::InitializeEncryption(const_data_ptr_t iv, idx_t iv_len, const_
101103
}
102104

103105
int len;
104-
if (aad_len > 0){
106+
if (aad_len > 0) {
105107
if (!EVP_DecryptUpdate(context, NULL, &len, aad, aad_len)) {
106108
throw InternalException("Setting Additional Authenticated Data failed");
107109
}
108110
}
109111
}
110112

111-
void AESStateSSL::InitializeDecryption(const_data_ptr_t iv, idx_t iv_len, const_data_ptr_t key, idx_t key_len_p, const_data_ptr_t aad, idx_t aad_len) {
113+
void AESStateSSL::InitializeDecryption(const_data_ptr_t iv, idx_t iv_len, const_data_ptr_t key, idx_t key_len_p,
114+
const_data_ptr_t aad, idx_t aad_len) {
112115
mode = EncryptionTypes::DECRYPT;
113116
if (key_len_p != key_len) {
114117
throw InternalException("Invalid encryption key length, expected %llu, got %llu", key_len, key_len_p);
@@ -126,7 +129,7 @@ void AESStateSSL::InitializeDecryption(const_data_ptr_t iv, idx_t iv_len, const_
126129
throw InternalException("EVP_DecryptInit_ex failed to set iv/key");
127130
}
128131
int len;
129-
if (aad_len > 0){
132+
if (aad_len > 0) {
130133
if (!EVP_DecryptUpdate(context, NULL, &len, aad, aad_len)) {
131134
throw InternalException("Setting Additional Authenticated Data failed");
132135
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

extension/httpfs/httpfs.cpp renamed to src/httpfs.cpp

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ unique_ptr<HTTPParams> HTTPFSUtil::InitializeParameters(optional_ptr<FileOpener>
4444
// Setting lookups
4545
FileOpener::TryGetCurrentSetting(opener, "http_timeout", result->timeout, info);
4646
FileOpener::TryGetCurrentSetting(opener, "force_download", result->force_download, info);
47-
FileOpener::TryGetCurrentSetting(opener, "auto_fallback_to_full_download", result->auto_fallback_to_full_download, info);
47+
FileOpener::TryGetCurrentSetting(opener, "auto_fallback_to_full_download", result->auto_fallback_to_full_download,
48+
info);
4849
FileOpener::TryGetCurrentSetting(opener, "http_retries", result->retries, info);
4950
FileOpener::TryGetCurrentSetting(opener, "http_retry_wait_ms", result->retry_wait_ms, info);
5051
FileOpener::TryGetCurrentSetting(opener, "http_retry_backoff", result->retry_backoff, info);
5152
FileOpener::TryGetCurrentSetting(opener, "http_keep_alive", result->keep_alive, info);
52-
FileOpener::TryGetCurrentSetting(opener, "enable_curl_server_cert_verification", result->enable_curl_server_cert_verification,
53-
info);
53+
FileOpener::TryGetCurrentSetting(opener, "enable_curl_server_cert_verification",
54+
result->enable_curl_server_cert_verification, info);
5455
FileOpener::TryGetCurrentSetting(opener, "enable_server_cert_verification", result->enable_server_cert_verification,
5556
info);
5657
FileOpener::TryGetCurrentSetting(opener, "ca_cert_file", result->ca_cert_file, info);
@@ -262,19 +263,23 @@ unique_ptr<HTTPResponse> HTTPFileSystem::GetRangeRequest(FileHandle &handle, str
262263
out_offset = 0;
263264

264265
if (!hfh.http_params.unsafe_disable_etag_checks && !hfh.etag.empty() && response.HasHeader("ETag")) {
265-
string responseEtag = response.GetHeaderValue("ETag");
266-
267-
if (!responseEtag.empty() && responseEtag != hfh.etag) {
268-
throw HTTPException(response, "ETag was initially %s and now it returned %s, this likely means the remote file has changed.\nTry to restart the read or close the file-handle and read the file again (e.g. `DETACH` in the file is a database file).\nYou can disable checking etags via `SET unsafe_disable_etag_checks = true;`", hfh.etag, response.GetHeaderValue("ETag"));
269-
}
270-
}
271-
272-
266+
string responseEtag = response.GetHeaderValue("ETag");
267+
268+
if (!responseEtag.empty() && responseEtag != hfh.etag) {
269+
throw HTTPException(
270+
response,
271+
"ETag was initially %s and now it returned %s, this likely means the remote file has "
272+
"changed.\nTry to restart the read or close the file-handle and read the file again (e.g. "
273+
"`DETACH` in the file is a database file).\nYou can disable checking etags via `SET "
274+
"unsafe_disable_etag_checks = true;`",
275+
hfh.etag, response.GetHeaderValue("ETag"));
276+
}
277+
}
273278

274279
if (response.HasHeader("Content-Length")) {
275280
auto content_length = stoll(response.GetHeaderValue("Content-Length"));
276281
if ((idx_t)content_length != buffer_out_len) {
277-
RangeRequestNotSupportedException::Throw();
282+
RangeRequestNotSupportedException::Throw();
278283
}
279284
}
280285
}
@@ -388,31 +393,33 @@ unique_ptr<FileHandle> HTTPFileSystem::OpenFileExtended(const OpenFileInfo &file
388393
return std::move(handle);
389394
}
390395

391-
bool HTTPFileSystem::TryRangeRequest(FileHandle &handle, string url, HTTPHeaders header_map, idx_t file_offset, char *buffer_out, idx_t buffer_out_len) {
396+
bool HTTPFileSystem::TryRangeRequest(FileHandle &handle, string url, HTTPHeaders header_map, idx_t file_offset,
397+
char *buffer_out, idx_t buffer_out_len) {
392398
auto res = GetRangeRequest(handle, url, header_map, file_offset, buffer_out, buffer_out_len);
393399

394400
if (res) {
395401
// Request succeeded TODO: fix upstream that 206 is not considered success
396-
if (res->Success() || res->status == HTTPStatusCode::PartialContent_206 || res->status == HTTPStatusCode::Accepted_202) {
402+
if (res->Success() || res->status == HTTPStatusCode::PartialContent_206 ||
403+
res->status == HTTPStatusCode::Accepted_202) {
397404
return true;
398405
}
399406

400407
// Request failed and we have a request error
401408
if (res->HasRequestError()) {
402-
ErrorData error (res->GetRequestError());
409+
ErrorData error(res->GetRequestError());
403410

404411
// Special case: we can do a retry with a full file download
405-
if (error.Type() == RangeRequestNotSupportedException::TYPE && error.RawMessage() == RangeRequestNotSupportedException::MESSAGE) {
412+
if (error.Type() == RangeRequestNotSupportedException::TYPE &&
413+
error.RawMessage() == RangeRequestNotSupportedException::MESSAGE) {
406414
auto &hfh = handle.Cast<HTTPFileHandle>();
407415
if (hfh.http_params.auto_fallback_to_full_download) {
408416
return false;
409417
}
410-
411418
}
412419
error.Throw();
413420
}
414-
throw HTTPException(*res, "Request returned HTTP %d for HTTP %s to '%s'",
415-
static_cast<int>(res->status), EnumUtil::ToString(RequestType::GET_REQUEST), res->url);
421+
throw HTTPException(*res, "Request returned HTTP %d for HTTP %s to '%s'", static_cast<int>(res->status),
422+
EnumUtil::ToString(RequestType::GET_REQUEST), res->url);
416423
}
417424
throw IOException("Unknown error for HTTP %s to '%s'", EnumUtil::ToString(RequestType::GET_REQUEST), url);
418425
}
@@ -485,15 +492,17 @@ bool HTTPFileSystem::ReadInternal(FileHandle &handle, void *buffer, int64_t nr_b
485492

486493
// Bypass buffer if we read more than buffer size
487494
if (to_read > new_buffer_available) {
488-
if (!TryRangeRequest(hfh, hfh.path, {}, location + buffer_offset, (char *)buffer + buffer_offset, to_read)) {
495+
if (!TryRangeRequest(hfh, hfh.path, {}, location + buffer_offset, (char *)buffer + buffer_offset,
496+
to_read)) {
489497
return false;
490498
}
491499
hfh.buffer_available = 0;
492500
hfh.buffer_idx = 0;
493501
start_offset += to_read;
494502
break;
495503
} else {
496-
if (!TryRangeRequest(hfh, hfh.path, {}, start_offset, (char *)hfh.read_buffer.get(), new_buffer_available)) {
504+
if (!TryRangeRequest(hfh, hfh.path, {}, start_offset, (char *)hfh.read_buffer.get(),
505+
new_buffer_available)) {
497506
return false;
498507
}
499508
hfh.buffer_available = new_buffer_available;
@@ -520,7 +529,10 @@ void HTTPFileSystem::Read(FileHandle &handle, void *buffer, int64_t nr_bytes, id
520529
// attempt to download the full file and retry.
521530

522531
if (handle.logger) {
523-
DUCKDB_LOG_WARN(handle.logger, "Falling back to full file download for file '%s': the server does not support HTTP range requests. Performance and memory usage are potentially degraded.", handle.path);
532+
DUCKDB_LOG_WARN(handle.logger,
533+
"Falling back to full file download for file '%s': the server does not support HTTP range "
534+
"requests. Performance and memory usage are potentially degraded.",
535+
handle.path);
524536
}
525537

526538
auto &hfh = handle.Cast<HTTPFileHandle>();
@@ -744,7 +756,6 @@ void HTTPFileHandle::LoadFileInfo() {
744756
initialized = true;
745757
}
746758

747-
748759
void HTTPFileHandle::TryAddLogger(FileOpener &opener) {
749760
auto context = opener.TryGetClientContext();
750761
if (context) {

0 commit comments

Comments
 (0)