Skip to content

Commit

Permalink
Refactor net lib functionality and fix build issues
Browse files Browse the repository at this point in the history
This makes the following changes related to the net lib:

- A CMake run now fails in case OpenSSL is not found when the local
  PostgreSQL installation has been compiled with SSL enabled. The
  reason why it is best to fail the CMake run in this case is because
  USE_OPENSSL will be defined and 1 in `pg_config.h` and thus we will
  compile with SSL support.

- Use palloc/pfree in connection library.

- Split net library into separate source files. The net library is
  refactored so that the code for different connection types live in
  their separate source files. In particular, the source code for mock
  connections is now moved to `test/src/net`.

- The generate_typedefs.sh script now runs in subdirectories so that
  source files in those subdirectories are properly pgindented.

- The pgindent target previously did not cover files under
  `test/src`. This is now fixed. An exclude file has also been added
  that avoids running pgindent in hidden directories. This fixes issues
  with, e.g., trying to indent files in cquery caches.

- Fix formatting with pgindent Fix parameter types in HTTP lib. Add
  `const` to parameter types, such as strings.  Use `size_t` for
  length parameters

- Parse and validate the status line of HTTP responses. The beginning
  of HTTP responses weren't properly parsed and validated, allowing
  invalid character sequences at the start of requests.
  • Loading branch information
erimatnor authored and RobAtticus committed Sep 10, 2018
1 parent 45a2b76 commit 4f6b92a
Show file tree
Hide file tree
Showing 21 changed files with 657 additions and 487 deletions.
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,24 @@ configure_file(${EXT_CONTROL_FILE}.in ${EXT_CONTROL_FILE})
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/${EXT_CONTROL_FILE}
DESTINATION "${PG_SHAREDIR}/extension")

find_program(PGINDENT pgindent
HINTS ${PG_SOURCE_DIR}
PATH_SUFFIXES src/tools/pgindent
DOC "Format C code according to PostgreSQL standards")

# Configuration for running pgindent
if (PGINDENT)
message(STATUS "Using pgindent ${PGINDENT}")
file(WRITE ${CMAKE_BINARY_DIR}/pgindent_excludes "\\..*/\n")

add_custom_command(OUTPUT typedefs.list
DEPENDS ${PROJECT_NAME}
COMMAND sh ${PROJECT_BINARY_DIR}/scripts/generate_typedefs.sh > ${PROJECT_BINARY_DIR}/typedefs.list)
add_custom_target(pgindent
COMMAND ${PGINDENT} -typedefs typedefs.list -excludes=${PROJECT_BINARY_DIR}/pgindent_excludes -code-base=${PROJECT_SOURCE_DIR}/src
COMMAND ${PGINDENT} -typedefs typedefs.list -excludes=${PROJECT_BINARY_DIR}/pgindent_excludes -code-base=${PROJECT_SOURCE_DIR}/test/src
DEPENDS ${PROJECT_BINARY_DIR}/typedefs.list)
else ()
message(STATUS "Install pgindent to be able to format C code: https://github.com/postgres/postgres/tree/master/src/tools/pgindent")
endif (PGINDENT)
49 changes: 18 additions & 31 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
set(CMAKE_C_FLAGS_DEBUG "-DUSE_ASSERT_CHECKING=1 -DDEBUG=1")

INCLUDE (FindOpenSSL)
file(STRINGS ${PG_INCLUDEDIR}/pg_config.h USE_OPENSSL REGEX "define USE_OPENSSL ")
# Check if PostgreSQL has OpenSSL enabled
file(STRINGS ${PG_INCLUDEDIR}/pg_config.h PG_USE_OPENSSL REGEX "#define USE_OPENSSL [01]")
string(REGEX REPLACE "#define USE_OPENSSL ([01])" "\\1" USE_OPENSSL ${PG_USE_OPENSSL})

if (OPENSSL_FOUND AND USE_OPENSSL)
include_directories ( ${OPENSSL_INCLUDE_DIR} )
endif (OPENSSL_FOUND AND USE_OPENSSL)
# If OpenSSL couldn't be found, we need to disable usage of OpenSSL
if (USE_OPENSSL)
# Try to find a local OpenSSL installation
include(FindOpenSSL)

if (NOT OPENSSL_FOUND)
message(FATAL_ERROR "PostgreSQL was compiled with SSL support, but OpenSSL was not found")
endif (NOT OPENSSL_FOUND)

include_directories(${OPENSSL_INCLUDE_DIR})
endif (USE_OPENSSL)

if (UNIX)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -L${PG_LIBDIR}")
Expand Down Expand Up @@ -131,10 +140,6 @@ set(SOURCES
utils.c
version.c)

if (NOT PROJECT_INSTALL_METHOD)
set(PROJECT_INSTALL_METHOD source)
endif (NOT PROJECT_INSTALL_METHOD)

configure_file(version.h.in version.h)
set(GITCOMMIT_H ${CMAKE_CURRENT_BINARY_DIR}/gitcommit.h)

Expand All @@ -161,30 +166,12 @@ else ()
VERBATIM)
endif (WIN32)

find_program(PGINDENT pgindent
HINTS ${PG_SOURCE_DIR}
PATH_SUFFIXES src/tools/pgindent
DOC "Format C code according to PostgreSQL standards")

if (PGINDENT)
message(STATUS "Using pgindent ${PGINDENT}")
else ()
message(STATUS "Install pgindent to be able to format C code: https://github.com/postgres/postgres/tree/master/src/tools/pgindent")
endif (PGINDENT)

# Configuration for running pgindent
if (PGINDENT)
add_custom_command(OUTPUT typedefs.list
DEPENDS ${PROJECT_NAME}
COMMAND sh ${CMAKE_BINARY_DIR}/scripts/generate_typedefs.sh > typedefs.list)
add_custom_target(pgindent
COMMAND ${PGINDENT} -typedefs typedefs.list -code-base ${CMAKE_SOURCE_DIR}/src/
COMMAND ${PGINDENT} -typedefs typedefs.list -code-base ${CMAKE_SOURCE_DIR}/test/src/
DEPENDS typedefs.list)
endif (PGINDENT)

add_library(${PROJECT_NAME} MODULE ${SOURCES} ${TEST_SOURCES} ${HEADERS} ${GITCOMMIT_H})

if (OPENSSL_FOUND AND USE_OPENSSL)
target_link_libraries(${PROJECT_NAME} ${OPENSSL_LIBRARIES})
endif (OPENSSL_FOUND AND USE_OPENSSL)

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_subdirectory(bgw)
add_subdirectory(net)
Expand Down
30 changes: 26 additions & 4 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,18 @@ extern void _process_utility_fini(void);
extern void _event_trigger_init(void);
extern void _event_trigger_fini(void);

extern void _connection_init(void);
extern void _connection_fini(void);
extern void _conn_plain_init();
extern void _conn_plain_fini();

#ifdef USE_OPENSSL
extern void _conn_ssl_init();
extern void _conn_ssl_fini();
#endif

#ifdef DEBUG
extern void _conn_mock_init();
extern void _conn_mock_fini();
#endif

extern void PGDLLEXPORT _PG_init(void);
extern void PGDLLEXPORT _PG_fini(void);
Expand All @@ -61,7 +71,13 @@ _PG_init(void)
_event_trigger_init();
_process_utility_init();
_guc_init();
_connection_init();
_conn_plain_init();
#ifdef USE_OPENSSL
_conn_ssl_init();
#endif
#ifdef DEBUG
_conn_mock_init();
#endif
}

void
Expand All @@ -71,7 +87,13 @@ _PG_fini(void)
* Order of items should be strict reverse order of _PG_init. Please
* document any exceptions.
*/
_connection_fini();
#ifdef DEBUG
_conn_mock_fini();
#endif
#ifdef USE_OPENSSL
_conn_ssl_fini();
#endif
_conn_plain_fini();
_guc_fini();
_process_utility_fini();
_event_trigger_fini();
Expand Down
4 changes: 3 additions & 1 deletion src/net/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Add all *.c to sources in upperlevel directory
set(SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/conn.c"
"${CMAKE_CURRENT_SOURCE_DIR}/conn_plain.c"
"${CMAKE_CURRENT_SOURCE_DIR}/conn_ssl.c"
"${CMAKE_CURRENT_SOURCE_DIR}/http_request.c"
"${CMAKE_CURRENT_SOURCE_DIR}/http_response.c"
)

target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

0 comments on commit 4f6b92a

Please sign in to comment.