├── .travis.yml ├── CMakeLists.txt ├── cmake ├── FindLua.cmake ├── dist.cmake └── lua.cmake ├── dist.info ├── luacurl.c └── luacurl.def /.travis.yml: -------------------------------------------------------------------------------- 1 | # 2 | # LuaDist Travis-CI Hook 3 | # 4 | 5 | # We assume C build environments 6 | language: C 7 | 8 | # Try using multiple Lua Implementations 9 | env: 10 | - TOOL="gcc" # Use native compiler (GCC usually) 11 | - TOOL="clang" # Use clang 12 | - TOOL="i686-w64-mingw32" # 32bit MinGW 13 | - TOOL="x86_64-w64-mingw32" # 64bit MinGW 14 | - TOOL="arm-linux-gnueabihf" # ARM hard-float (hf), linux 15 | 16 | # Crosscompile builds may fail 17 | matrix: 18 | allow_failures: 19 | - env: TOOL="i686-w64-mingw32" 20 | - env: TOOL="x86_64-w64-mingw32" 21 | - env: TOOL="arm-linux-gnueabihf" 22 | 23 | # Install dependencies 24 | install: 25 | - git clone git://github.com/LuaDist/Tools.git ~/_tools 26 | - ~/_tools/travis/travis install 27 | 28 | # Bootstap 29 | before_script: 30 | - ~/_tools/travis/travis bootstrap 31 | 32 | # Build the module 33 | script: 34 | - ~/_tools/travis/travis build 35 | 36 | # Execute additional tests or commands 37 | after_script: 38 | - ~/_tools/travis/travis test 39 | 40 | # Only watch the master branch 41 | branches: 42 | only: 43 | - master 44 | 45 | # Notify the LuaDist Dev group if needed 46 | notifications: 47 | recipients: 48 | - luadist-dev@googlegroups.com 49 | email: 50 | on_success: change 51 | on_failure: always 52 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2007-2012 LuaDist. 2 | # Created by Peter Drahoš 3 | # Redistribution and use of this file is allowed according to the terms of the MIT license. 4 | # For details see the COPYRIGHT file distributed with LuaDist. 5 | # Please note that the package source code is licensed under its own license. 6 | 7 | project ( luacurl C ) 8 | cmake_minimum_required ( VERSION 2.8 ) 9 | include ( cmake/dist.cmake ) 10 | include ( lua ) 11 | 12 | # Find curl 13 | find_package ( CURL REQUIRED ) 14 | include_directories ( ${CURL_INCLUDE_DIR} ) 15 | 16 | install_lua_module ( luacurl luacurl.c luacurl.def LINK ${CURL_LIBRARY} ) 17 | -------------------------------------------------------------------------------- /cmake/FindLua.cmake: -------------------------------------------------------------------------------- 1 | # Locate Lua library 2 | # This module defines 3 | # LUA_EXECUTABLE, if found 4 | # LUA_FOUND, if false, do not try to link to Lua 5 | # LUA_LIBRARIES 6 | # LUA_INCLUDE_DIR, where to find lua.h 7 | # LUA_VERSION_STRING, the version of Lua found (since CMake 2.8.8) 8 | # 9 | # Note that the expected include convention is 10 | # #include "lua.h" 11 | # and not 12 | # #include 13 | # This is because, the lua location is not standardized and may exist 14 | # in locations other than lua/ 15 | 16 | #============================================================================= 17 | # Copyright 2007-2009 Kitware, Inc. 18 | # Modified to support Lua 5.2 by LuaDist 2012 19 | # 20 | # Distributed under the OSI-approved BSD License (the "License"); 21 | # see accompanying file Copyright.txt for details. 22 | # 23 | # This software is distributed WITHOUT ANY WARRANTY; without even the 24 | # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 25 | # See the License for more information. 26 | #============================================================================= 27 | # (To distribute this file outside of CMake, substitute the full 28 | # License text for the above reference.) 29 | # 30 | # The required version of Lua can be specified using the 31 | # standard syntax, e.g. FIND_PACKAGE(Lua 5.1) 32 | # Otherwise the module will search for any available Lua implementation 33 | 34 | # Always search for non-versioned lua first (recommended) 35 | SET(_POSSIBLE_LUA_INCLUDE include include/lua) 36 | SET(_POSSIBLE_LUA_EXECUTABLE lua) 37 | SET(_POSSIBLE_LUA_LIBRARY lua) 38 | 39 | # Determine possible naming suffixes (there is no standard for this) 40 | IF(Lua_FIND_VERSION_MAJOR AND Lua_FIND_VERSION_MINOR) 41 | SET(_POSSIBLE_SUFFIXES "${Lua_FIND_VERSION_MAJOR}${Lua_FIND_VERSION_MINOR}" "${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR}" "-${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR}") 42 | ELSE(Lua_FIND_VERSION_MAJOR AND Lua_FIND_VERSION_MINOR) 43 | SET(_POSSIBLE_SUFFIXES "52" "5.2" "-5.2" "51" "5.1" "-5.1") 44 | ENDIF(Lua_FIND_VERSION_MAJOR AND Lua_FIND_VERSION_MINOR) 45 | 46 | # Set up possible search names and locations 47 | FOREACH(_SUFFIX ${_POSSIBLE_SUFFIXES}) 48 | LIST(APPEND _POSSIBLE_LUA_INCLUDE "include/lua${_SUFFIX}") 49 | LIST(APPEND _POSSIBLE_LUA_EXECUTABLE "lua${_SUFFIX}") 50 | LIST(APPEND _POSSIBLE_LUA_LIBRARY "lua${_SUFFIX}") 51 | ENDFOREACH(_SUFFIX) 52 | 53 | # Find the lua executable 54 | FIND_PROGRAM(LUA_EXECUTABLE 55 | NAMES ${_POSSIBLE_LUA_EXECUTABLE} 56 | ) 57 | 58 | # Find the lua header 59 | FIND_PATH(LUA_INCLUDE_DIR lua.h 60 | HINTS 61 | $ENV{LUA_DIR} 62 | PATH_SUFFIXES ${_POSSIBLE_LUA_INCLUDE} 63 | PATHS 64 | ~/Library/Frameworks 65 | /Library/Frameworks 66 | /usr/local 67 | /usr 68 | /sw # Fink 69 | /opt/local # DarwinPorts 70 | /opt/csw # Blastwave 71 | /opt 72 | ) 73 | 74 | # Find the lua library 75 | FIND_LIBRARY(LUA_LIBRARY 76 | NAMES ${_POSSIBLE_LUA_LIBRARY} 77 | HINTS 78 | $ENV{LUA_DIR} 79 | PATH_SUFFIXES lib64 lib 80 | PATHS 81 | ~/Library/Frameworks 82 | /Library/Frameworks 83 | /usr/local 84 | /usr 85 | /sw 86 | /opt/local 87 | /opt/csw 88 | /opt 89 | ) 90 | 91 | IF(LUA_LIBRARY) 92 | # include the math library for Unix 93 | IF(UNIX AND NOT APPLE) 94 | FIND_LIBRARY(LUA_MATH_LIBRARY m) 95 | SET( LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}" CACHE STRING "Lua Libraries") 96 | # For Windows and Mac, don't need to explicitly include the math library 97 | ELSE(UNIX AND NOT APPLE) 98 | SET( LUA_LIBRARIES "${LUA_LIBRARY}" CACHE STRING "Lua Libraries") 99 | ENDIF(UNIX AND NOT APPLE) 100 | ENDIF(LUA_LIBRARY) 101 | 102 | # Determine Lua version 103 | IF(LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h") 104 | FILE(STRINGS "${LUA_INCLUDE_DIR}/lua.h" lua_version_str REGEX "^#define[ \t]+LUA_RELEASE[ \t]+\"Lua .+\"") 105 | 106 | STRING(REGEX REPLACE "^#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([^\"]+)\".*" "\\1" LUA_VERSION_STRING "${lua_version_str}") 107 | UNSET(lua_version_str) 108 | ENDIF() 109 | 110 | INCLUDE(FindPackageHandleStandardArgs) 111 | # handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if 112 | # all listed variables are TRUE 113 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua 114 | REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR 115 | VERSION_VAR LUA_VERSION_STRING) 116 | 117 | MARK_AS_ADVANCED(LUA_INCLUDE_DIR LUA_LIBRARIES LUA_LIBRARY LUA_MATH_LIBRARY LUA_EXECUTABLE) 118 | 119 | -------------------------------------------------------------------------------- /cmake/dist.cmake: -------------------------------------------------------------------------------- 1 | # LuaDist CMake utility library. 2 | # Provides sane project defaults and macros common to LuaDist CMake builds. 3 | # 4 | # Copyright (C) 2007-2012 LuaDist. 5 | # by David Manura, Peter Drahoš 6 | # Redistribution and use of this file is allowed according to the terms of the MIT license. 7 | # For details see the COPYRIGHT file distributed with LuaDist. 8 | # Please note that the package source code is licensed under its own license. 9 | 10 | ## Extract information from dist.info 11 | if ( NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/dist.info ) 12 | message ( FATAL_ERROR 13 | "Missing dist.info file (${CMAKE_CURRENT_SOURCE_DIR}/dist.info)." ) 14 | endif () 15 | file ( READ ${CMAKE_CURRENT_SOURCE_DIR}/dist.info DIST_INFO ) 16 | if ( "${DIST_INFO}" STREQUAL "" ) 17 | message ( FATAL_ERROR "Failed to load dist.info." ) 18 | endif () 19 | # Reads field `name` from dist.info string `DIST_INFO` into variable `var`. 20 | macro ( _parse_dist_field name var ) 21 | string ( REGEX REPLACE ".*${name}[ \t]?=[ \t]?[\"']([^\"']+)[\"'].*" "\\1" 22 | ${var} "${DIST_INFO}" ) 23 | if ( ${var} STREQUAL DIST_INFO ) 24 | message ( FATAL_ERROR "Failed to extract \"${var}\" from dist.info" ) 25 | endif () 26 | endmacro () 27 | # 28 | _parse_dist_field ( name DIST_NAME ) 29 | _parse_dist_field ( version DIST_VERSION ) 30 | _parse_dist_field ( license DIST_LICENSE ) 31 | _parse_dist_field ( author DIST_AUTHOR ) 32 | _parse_dist_field ( maintainer DIST_MAINTAINER ) 33 | _parse_dist_field ( url DIST_URL ) 34 | _parse_dist_field ( desc DIST_DESC ) 35 | message ( "DIST_NAME: ${DIST_NAME}") 36 | message ( "DIST_VERSION: ${DIST_VERSION}") 37 | message ( "DIST_LICENSE: ${DIST_LICENSE}") 38 | message ( "DIST_AUTHOR: ${DIST_AUTHOR}") 39 | message ( "DIST_MAINTAINER: ${DIST_MAINTAINER}") 40 | message ( "DIST_URL: ${DIST_URL}") 41 | message ( "DIST_DESC: ${DIST_DESC}") 42 | string ( REGEX REPLACE ".*depends[ \t]?=[ \t]?[\"']([^\"']+)[\"'].*" "\\1" 43 | DIST_DEPENDS ${DIST_INFO} ) 44 | if ( DIST_DEPENDS STREQUAL DIST_INFO ) 45 | set ( DIST_DEPENDS "" ) 46 | endif () 47 | message ( "DIST_DEPENDS: ${DIST_DEPENDS}") 48 | ## 2DO: Parse DIST_DEPENDS and try to install Dependencies with automatically using externalproject_add 49 | 50 | 51 | ## INSTALL DEFAULTS (Relative to CMAKE_INSTALL_PREFIX) 52 | # Primary paths 53 | set ( INSTALL_BIN bin CACHE PATH "Where to install binaries to." ) 54 | set ( INSTALL_LIB lib CACHE PATH "Where to install libraries to." ) 55 | set ( INSTALL_INC include CACHE PATH "Where to install headers to." ) 56 | set ( INSTALL_ETC etc CACHE PATH "Where to store configuration files" ) 57 | set ( INSTALL_SHARE share CACHE PATH "Directory for shared data." ) 58 | 59 | # Secondary paths 60 | option ( INSTALL_VERSION 61 | "Install runtime libraries and executables with version information." OFF) 62 | set ( INSTALL_DATA ${INSTALL_SHARE}/${DIST_NAME} CACHE PATH 63 | "Directory the package can store documentation, tests or other data in.") 64 | set ( INSTALL_DOC ${INSTALL_DATA}/doc CACHE PATH 65 | "Recommended directory to install documentation into.") 66 | set ( INSTALL_EXAMPLE ${INSTALL_DATA}/example CACHE PATH 67 | "Recommended directory to install examples into.") 68 | set ( INSTALL_TEST ${INSTALL_DATA}/test CACHE PATH 69 | "Recommended directory to install tests into.") 70 | set ( INSTALL_FOO ${INSTALL_DATA}/etc CACHE PATH 71 | "Where to install additional files") 72 | 73 | # Tweaks and other defaults 74 | # Setting CMAKE to use loose block and search for find modules in source directory 75 | set ( CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true ) 76 | set ( CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH} ) 77 | option ( BUILD_SHARED_LIBS "Build shared libraries" ON ) 78 | 79 | # In MSVC, prevent warnings that can occur when using standard libraries. 80 | if ( MSVC ) 81 | add_definitions ( -D_CRT_SECURE_NO_WARNINGS ) 82 | endif () 83 | 84 | # RPath and relative linking 85 | option ( USE_RPATH "Use relative linking." ON) 86 | if ( USE_RPATH ) 87 | string ( REGEX REPLACE "[^!/]+" ".." UP_DIR ${INSTALL_BIN} ) 88 | set ( CMAKE_SKIP_BUILD_RPATH FALSE CACHE STRING "" FORCE ) 89 | set ( CMAKE_BUILD_WITH_INSTALL_RPATH FALSE CACHE STRING "" FORCE ) 90 | set ( CMAKE_INSTALL_RPATH $ORIGIN/${UP_DIR}/${INSTALL_LIB} 91 | CACHE STRING "" FORCE ) 92 | set ( CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE STRING "" FORCE ) 93 | set ( CMAKE_INSTALL_NAME_DIR @executable_path/${UP_DIR}/${INSTALL_LIB} 94 | CACHE STRING "" FORCE ) 95 | endif () 96 | 97 | ## MACROS 98 | # Parser macro 99 | macro ( parse_arguments prefix arg_names option_names) 100 | set ( DEFAULT_ARGS ) 101 | foreach ( arg_name ${arg_names} ) 102 | set ( ${prefix}_${arg_name} ) 103 | endforeach () 104 | foreach ( option ${option_names} ) 105 | set ( ${prefix}_${option} FALSE ) 106 | endforeach () 107 | 108 | set ( current_arg_name DEFAULT_ARGS ) 109 | set ( current_arg_list ) 110 | foreach ( arg ${ARGN} ) 111 | set ( larg_names ${arg_names} ) 112 | list ( FIND larg_names "${arg}" is_arg_name ) 113 | if ( is_arg_name GREATER -1 ) 114 | set ( ${prefix}_${current_arg_name} ${current_arg_list} ) 115 | set ( current_arg_name ${arg} ) 116 | set ( current_arg_list ) 117 | else () 118 | set ( loption_names ${option_names} ) 119 | list ( FIND loption_names "${arg}" is_option ) 120 | if ( is_option GREATER -1 ) 121 | set ( ${prefix}_${arg} TRUE ) 122 | else () 123 | set ( current_arg_list ${current_arg_list} ${arg} ) 124 | endif () 125 | endif () 126 | endforeach () 127 | set ( ${prefix}_${current_arg_name} ${current_arg_list} ) 128 | endmacro () 129 | 130 | 131 | # install_executable ( executable_targets ) 132 | # Installs any executables generated using "add_executable". 133 | # USE: install_executable ( lua ) 134 | # NOTE: subdirectories are NOT supported 135 | set ( CPACK_COMPONENT_RUNTIME_DISPLAY_NAME "${DIST_NAME} Runtime" ) 136 | set ( CPACK_COMPONENT_RUNTIME_DESCRIPTION 137 | "Executables and runtime libraries. Installed into ${INSTALL_BIN}." ) 138 | macro ( install_executable ) 139 | foreach ( _file ${ARGN} ) 140 | if ( INSTALL_VERSION ) 141 | set_target_properties ( ${_file} PROPERTIES VERSION ${DIST_VERSION} 142 | SOVERSION ${DIST_VERSION} ) 143 | endif () 144 | install ( TARGETS ${_file} RUNTIME DESTINATION ${INSTALL_BIN} 145 | COMPONENT Runtime ) 146 | endforeach() 147 | endmacro () 148 | 149 | # install_library ( library_targets ) 150 | # Installs any libraries generated using "add_library" into apropriate places. 151 | # USE: install_library ( libexpat ) 152 | # NOTE: subdirectories are NOT supported 153 | set ( CPACK_COMPONENT_LIBRARY_DISPLAY_NAME "${DIST_NAME} Development Libraries" ) 154 | set ( CPACK_COMPONENT_LIBRARY_DESCRIPTION 155 | "Static and import libraries needed for development. Installed into ${INSTALL_LIB} or ${INSTALL_BIN}." ) 156 | macro ( install_library ) 157 | foreach ( _file ${ARGN} ) 158 | if ( INSTALL_VERSION ) 159 | set_target_properties ( ${_file} PROPERTIES VERSION ${DIST_VERSION} 160 | SOVERSION ${DIST_VERSION} ) 161 | endif () 162 | install ( TARGETS ${_file} 163 | RUNTIME DESTINATION ${INSTALL_BIN} COMPONENT Runtime 164 | LIBRARY DESTINATION ${INSTALL_LIB} COMPONENT Runtime 165 | ARCHIVE DESTINATION ${INSTALL_LIB} COMPONENT Library ) 166 | endforeach() 167 | endmacro () 168 | 169 | # helper function for various install_* functions, for PATTERN/REGEX args. 170 | macro ( _complete_install_args ) 171 | if ( NOT("${_ARG_PATTERN}" STREQUAL "") ) 172 | set ( _ARG_PATTERN PATTERN ${_ARG_PATTERN} ) 173 | endif () 174 | if ( NOT("${_ARG_REGEX}" STREQUAL "") ) 175 | set ( _ARG_REGEX REGEX ${_ARG_REGEX} ) 176 | endif () 177 | endmacro () 178 | 179 | # install_header ( files/directories [INTO destination] ) 180 | # Install a directories or files into header destination. 181 | # USE: install_header ( lua.h luaconf.h ) or install_header ( GL ) 182 | # USE: install_header ( mylib.h INTO mylib ) 183 | # For directories, supports optional PATTERN/REGEX arguments like install(). 184 | set ( CPACK_COMPONENT_HEADER_DISPLAY_NAME "${DIST_NAME} Development Headers" ) 185 | set ( CPACK_COMPONENT_HEADER_DESCRIPTION 186 | "Headers needed for development. Installed into ${INSTALL_INC}." ) 187 | macro ( install_header ) 188 | parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} ) 189 | _complete_install_args() 190 | foreach ( _file ${_ARG_DEFAULT_ARGS} ) 191 | if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) 192 | install ( DIRECTORY ${_file} DESTINATION ${INSTALL_INC}/${_ARG_INTO} 193 | COMPONENT Header ${_ARG_PATTERN} ${_ARG_REGEX} ) 194 | else () 195 | install ( FILES ${_file} DESTINATION ${INSTALL_INC}/${_ARG_INTO} 196 | COMPONENT Header ) 197 | endif () 198 | endforeach() 199 | endmacro () 200 | 201 | # install_data ( files/directories [INTO destination] ) 202 | # This installs additional data files or directories. 203 | # USE: install_data ( extra data.dat ) 204 | # USE: install_data ( image1.png image2.png INTO images ) 205 | # For directories, supports optional PATTERN/REGEX arguments like install(). 206 | set ( CPACK_COMPONENT_DATA_DISPLAY_NAME "${DIST_NAME} Data" ) 207 | set ( CPACK_COMPONENT_DATA_DESCRIPTION 208 | "Application data. Installed into ${INSTALL_DATA}." ) 209 | macro ( install_data ) 210 | parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} ) 211 | _complete_install_args() 212 | foreach ( _file ${_ARG_DEFAULT_ARGS} ) 213 | if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) 214 | install ( DIRECTORY ${_file} 215 | DESTINATION ${INSTALL_DATA}/${_ARG_INTO} 216 | COMPONENT Data ${_ARG_PATTERN} ${_ARG_REGEX} ) 217 | else () 218 | install ( FILES ${_file} DESTINATION ${INSTALL_DATA}/${_ARG_INTO} 219 | COMPONENT Data ) 220 | endif () 221 | endforeach() 222 | endmacro () 223 | 224 | # INSTALL_DOC ( files/directories [INTO destination] ) 225 | # This installs documentation content 226 | # USE: install_doc ( doc/ doc.pdf ) 227 | # USE: install_doc ( index.html INTO html ) 228 | # For directories, supports optional PATTERN/REGEX arguments like install(). 229 | set ( CPACK_COMPONENT_DOCUMENTATION_DISPLAY_NAME "${DIST_NAME} Documentation" ) 230 | set ( CPACK_COMPONENT_DOCUMENTATION_DESCRIPTION 231 | "Application documentation. Installed into ${INSTALL_DOC}." ) 232 | macro ( install_doc ) 233 | parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} ) 234 | _complete_install_args() 235 | foreach ( _file ${_ARG_DEFAULT_ARGS} ) 236 | if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) 237 | install ( DIRECTORY ${_file} DESTINATION ${INSTALL_DOC}/${_ARG_INTO} 238 | COMPONENT Documentation ${_ARG_PATTERN} ${_ARG_REGEX} ) 239 | else () 240 | install ( FILES ${_file} DESTINATION ${INSTALL_DOC}/${_ARG_INTO} 241 | COMPONENT Documentation ) 242 | endif () 243 | endforeach() 244 | endmacro () 245 | 246 | # install_example ( files/directories [INTO destination] ) 247 | # This installs additional examples 248 | # USE: install_example ( examples/ exampleA ) 249 | # USE: install_example ( super_example super_data INTO super) 250 | # For directories, supports optional PATTERN/REGEX argument like install(). 251 | set ( CPACK_COMPONENT_EXAMPLE_DISPLAY_NAME "${DIST_NAME} Examples" ) 252 | set ( CPACK_COMPONENT_EXAMPLE_DESCRIPTION 253 | "Examples and their associated data. Installed into ${INSTALL_EXAMPLE}." ) 254 | macro ( install_example ) 255 | parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} ) 256 | _complete_install_args() 257 | foreach ( _file ${_ARG_DEFAULT_ARGS} ) 258 | if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) 259 | install ( DIRECTORY ${_file} DESTINATION ${INSTALL_EXAMPLE}/${_ARG_INTO} 260 | COMPONENT Example ${_ARG_PATTERN} ${_ARG_REGEX} ) 261 | else () 262 | install ( FILES ${_file} DESTINATION ${INSTALL_EXAMPLE}/${_ARG_INTO} 263 | COMPONENT Example ) 264 | endif () 265 | endforeach() 266 | endmacro () 267 | 268 | # install_test ( files/directories [INTO destination] ) 269 | # This installs tests and test files, DOES NOT EXECUTE TESTS 270 | # USE: install_test ( my_test data.sql ) 271 | # USE: install_test ( feature_x_test INTO x ) 272 | # For directories, supports optional PATTERN/REGEX argument like install(). 273 | set ( CPACK_COMPONENT_TEST_DISPLAY_NAME "${DIST_NAME} Tests" ) 274 | set ( CPACK_COMPONENT_TEST_DESCRIPTION 275 | "Tests and associated data. Installed into ${INSTALL_TEST}." ) 276 | macro ( install_test ) 277 | parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} ) 278 | _complete_install_args() 279 | foreach ( _file ${_ARG_DEFAULT_ARGS} ) 280 | if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) 281 | install ( DIRECTORY ${_file} DESTINATION ${INSTALL_TEST}/${_ARG_INTO} 282 | COMPONENT Test ${_ARG_PATTERN} ${_ARG_REGEX} ) 283 | else () 284 | install ( FILES ${_file} DESTINATION ${INSTALL_TEST}/${_ARG_INTO} 285 | COMPONENT Test ) 286 | endif () 287 | endforeach() 288 | endmacro () 289 | 290 | # install_foo ( files/directories [INTO destination] ) 291 | # This installs optional or otherwise unneeded content 292 | # USE: install_foo ( etc/ example.doc ) 293 | # USE: install_foo ( icon.png logo.png INTO icons) 294 | # For directories, supports optional PATTERN/REGEX argument like install(). 295 | set ( CPACK_COMPONENT_OTHER_DISPLAY_NAME "${DIST_NAME} Unspecified Content" ) 296 | set ( CPACK_COMPONENT_OTHER_DESCRIPTION 297 | "Other unspecified content. Installed into ${INSTALL_FOO}." ) 298 | macro ( install_foo ) 299 | parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} ) 300 | _complete_install_args() 301 | foreach ( _file ${_ARG_DEFAULT_ARGS} ) 302 | if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" ) 303 | install ( DIRECTORY ${_file} DESTINATION ${INSTALL_FOO}/${_ARG_INTO} 304 | COMPONENT Other ${_ARG_PATTERN} ${_ARG_REGEX} ) 305 | else () 306 | install ( FILES ${_file} DESTINATION ${INSTALL_FOO}/${_ARG_INTO} 307 | COMPONENT Other ) 308 | endif () 309 | endforeach() 310 | endmacro () 311 | 312 | ## CTest defaults 313 | 314 | ## CPack defaults 315 | set ( CPACK_GENERATOR "ZIP" ) 316 | set ( CPACK_STRIP_FILES TRUE ) 317 | set ( CPACK_PACKAGE_NAME "${DIST_NAME}" ) 318 | set ( CPACK_PACKAGE_VERSION "${DIST_VERSION}") 319 | set ( CPACK_PACKAGE_VENDOR "LuaDist" ) 320 | set ( CPACK_COMPONENTS_ALL Runtime Library Header Data Documentation Example Other ) 321 | include ( CPack ) 322 | -------------------------------------------------------------------------------- /cmake/lua.cmake: -------------------------------------------------------------------------------- 1 | # LuaDist CMake utility library for Lua. 2 | # 3 | # Copyright (C) 2007-2012 LuaDist. 4 | # by David Manura, Peter Drahos 5 | # Redistribution and use of this file is allowed according to the terms of the MIT license. 6 | # For details see the COPYRIGHT file distributed with LuaDist. 7 | # Please note that the package source code is licensed under its own license. 8 | 9 | set ( INSTALL_LMOD ${INSTALL_LIB}/lua 10 | CACHE PATH "Directory to install Lua modules." ) 11 | set ( INSTALL_CMOD ${INSTALL_LIB}/lua 12 | CACHE PATH "Directory to install Lua binary modules." ) 13 | 14 | option ( SKIP_LUA_WRAPPER 15 | "Do not build and install Lua executable wrappers." OFF) 16 | 17 | # List of (Lua module name, file path) pairs. 18 | # Used internally by add_lua_test. Built by add_lua_module. 19 | set ( _lua_modules ) 20 | 21 | # utility function: appends path `path` to path `basepath`, properly 22 | # handling cases when `path` may be relative or absolute. 23 | macro ( _append_path basepath path result ) 24 | if ( IS_ABSOLUTE "${path}" ) 25 | set ( ${result} "${path}" ) 26 | else () 27 | set ( ${result} "${basepath}/${path}" ) 28 | endif () 29 | endmacro () 30 | 31 | # install_lua_executable ( target source ) 32 | # Automatically generate a binary if srlua package is available 33 | # The application or its source will be placed into /bin 34 | # If the application source did not have .lua suffix then it will be added 35 | # USE: lua_executable ( sputnik src/sputnik.lua ) 36 | macro ( install_lua_executable _name _source ) 37 | get_filename_component ( _source_name ${_source} NAME_WE ) 38 | # Find srlua and glue 39 | find_program( SRLUA_EXECUTABLE NAMES srlua ) 40 | find_program( GLUE_EXECUTABLE NAMES glue ) 41 | # Executable output 42 | set ( _exe ${CMAKE_CURRENT_BINARY_DIR}/${_name}${CMAKE_EXECUTABLE_SUFFIX} ) 43 | if ( NOT SKIP_LUA_WRAPPER AND SRLUA_EXECUTABLE AND GLUE_EXECUTABLE ) 44 | # Generate binary gluing the lua code to srlua, this is a robuust approach for most systems 45 | add_custom_command( 46 | OUTPUT ${_exe} 47 | COMMAND ${GLUE_EXECUTABLE} 48 | ARGS ${SRLUA_EXECUTABLE} ${_source} ${_exe} 49 | DEPENDS ${_source} 50 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 51 | VERBATIM 52 | ) 53 | # Make sure we have a target associated with the binary 54 | add_custom_target(${_name} ALL 55 | DEPENDS ${_exe} 56 | ) 57 | # Install with run permissions 58 | install ( PROGRAMS ${_exe} DESTINATION ${INSTALL_BIN} COMPONENT Runtime) 59 | # Also install source as optional resurce 60 | install ( FILES ${_source} DESTINATION ${INSTALL_FOO} COMPONENT Other ) 61 | else() 62 | # Install into bin as is but without the lua suffix, we assume the executable uses UNIX shebang/hash-bang magic 63 | install ( PROGRAMS ${_source} DESTINATION ${INSTALL_BIN} 64 | RENAME ${_source_name} 65 | COMPONENT Runtime 66 | ) 67 | endif() 68 | endmacro () 69 | 70 | macro ( _lua_module_helper is_install _name ) 71 | parse_arguments ( _MODULE "LINK;ALL_IN_ONE" "" ${ARGN} ) 72 | # _target is CMake-compatible target name for module (e.g. socket_core). 73 | # _module is relative path of target (e.g. socket/core), 74 | # without extension (e.g. .lua/.so/.dll). 75 | # _MODULE_SRC is list of module source files (e.g. .lua and .c files). 76 | # _MODULE_NAMES is list of module names (e.g. socket.core). 77 | if ( _MODULE_ALL_IN_ONE ) 78 | string ( REGEX REPLACE "\\..*" "" _target "${_name}" ) 79 | string ( REGEX REPLACE "\\..*" "" _module "${_name}" ) 80 | set ( _target "${_target}_all_in_one") 81 | set ( _MODULE_SRC ${_MODULE_ALL_IN_ONE} ) 82 | set ( _MODULE_NAMES ${_name} ${_MODULE_DEFAULT_ARGS} ) 83 | else () 84 | string ( REPLACE "." "_" _target "${_name}" ) 85 | string ( REPLACE "." "/" _module "${_name}" ) 86 | set ( _MODULE_SRC ${_MODULE_DEFAULT_ARGS} ) 87 | set ( _MODULE_NAMES ${_name} ) 88 | endif () 89 | if ( NOT _MODULE_SRC ) 90 | message ( FATAL_ERROR "no module sources specified" ) 91 | endif () 92 | list ( GET _MODULE_SRC 0 _first_source ) 93 | 94 | get_filename_component ( _ext ${_first_source} EXT ) 95 | if ( _ext STREQUAL ".lua" ) # Lua source module 96 | list ( LENGTH _MODULE_SRC _len ) 97 | if ( _len GREATER 1 ) 98 | message ( FATAL_ERROR "more than one source file specified" ) 99 | endif () 100 | 101 | set ( _module "${_module}.lua" ) 102 | 103 | get_filename_component ( _module_dir ${_module} PATH ) 104 | get_filename_component ( _module_filename ${_module} NAME ) 105 | _append_path ( "${CMAKE_CURRENT_SOURCE_DIR}" "${_first_source}" _module_path ) 106 | list ( APPEND _lua_modules "${_name}" "${_module_path}" ) 107 | 108 | if ( ${is_install} ) 109 | install ( FILES ${_first_source} DESTINATION ${INSTALL_LMOD}/${_module_dir} 110 | RENAME ${_module_filename} 111 | COMPONENT Runtime 112 | ) 113 | endif () 114 | else () # Lua C binary module 115 | enable_language ( C ) 116 | find_package ( Lua REQUIRED ) 117 | include_directories ( ${LUA_INCLUDE_DIR} ) 118 | 119 | set ( _module "${_module}${CMAKE_SHARED_MODULE_SUFFIX}" ) 120 | 121 | get_filename_component ( _module_dir ${_module} PATH ) 122 | get_filename_component ( _module_filenamebase ${_module} NAME_WE ) 123 | foreach ( _thisname ${_MODULE_NAMES} ) 124 | list ( APPEND _lua_modules "${_thisname}" 125 | "${CMAKE_CURRENT_BINARY_DIR}/\${CMAKE_CFG_INTDIR}/${_module}" ) 126 | endforeach () 127 | 128 | add_library( ${_target} MODULE ${_MODULE_SRC}) 129 | target_link_libraries ( ${_target} ${LUA_LIBRARY} ${_MODULE_LINK} ) 130 | set_target_properties ( ${_target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY 131 | "${_module_dir}" PREFIX "" OUTPUT_NAME "${_module_filenamebase}" ) 132 | if ( ${is_install} ) 133 | install ( TARGETS ${_target} DESTINATION ${INSTALL_CMOD}/${_module_dir} COMPONENT Runtime) 134 | endif () 135 | endif () 136 | endmacro () 137 | 138 | # add_lua_module 139 | # Builds a Lua source module into a destination locatable by Lua 140 | # require syntax. 141 | # Binary modules are also supported where this function takes sources and 142 | # libraries to compile separated by LINK keyword. 143 | # USE: add_lua_module ( socket.http src/http.lua ) 144 | # USE2: add_lua_module ( mime.core src/mime.c ) 145 | # USE3: add_lua_module ( socket.core ${SRC_SOCKET} LINK ${LIB_SOCKET} ) 146 | # USE4: add_lua_module ( ssl.context ssl.core ALL_IN_ONE src/context.c src/ssl.c ) 147 | # This form builds an "all-in-one" module (e.g. ssl.so or ssl.dll containing 148 | # both modules ssl.context and ssl.core). The CMake target name will be 149 | # ssl_all_in_one. 150 | # Also sets variable _module_path (relative path where module typically 151 | # would be installed). 152 | macro ( add_lua_module ) 153 | _lua_module_helper ( 0 ${ARGN} ) 154 | endmacro () 155 | 156 | 157 | # install_lua_module 158 | # This is the same as `add_lua_module` but also installs the module. 159 | # USE: install_lua_module ( socket.http src/http.lua ) 160 | # USE2: install_lua_module ( mime.core src/mime.c ) 161 | # USE3: install_lua_module ( socket.core ${SRC_SOCKET} LINK ${LIB_SOCKET} ) 162 | macro ( install_lua_module ) 163 | _lua_module_helper ( 1 ${ARGN} ) 164 | endmacro () 165 | 166 | # Builds string representing Lua table mapping Lua modules names to file 167 | # paths. Used internally. 168 | macro ( _make_module_table _outvar ) 169 | set ( ${_outvar} ) 170 | list ( LENGTH _lua_modules _n ) 171 | if ( ${_n} GREATER 0 ) # avoids cmake complaint 172 | foreach ( _i RANGE 1 ${_n} 2 ) 173 | list ( GET _lua_modules ${_i} _path ) 174 | math ( EXPR _ii ${_i}-1 ) 175 | list ( GET _lua_modules ${_ii} _name ) 176 | set ( ${_outvar} "${_table} ['${_name}'] = '${_path}'\;\n") 177 | endforeach () 178 | endif () 179 | set ( ${_outvar} 180 | "local modules = { 181 | ${_table}}" ) 182 | endmacro () 183 | 184 | # add_lua_test ( _testfile [ WORKING_DIRECTORY _working_dir ] ) 185 | # Runs Lua script `_testfile` under CTest tester. 186 | # Optional named argument `WORKING_DIRECTORY` is current working directory to 187 | # run test under (defaults to ${CMAKE_CURRENT_BINARY_DIR}). 188 | # Both paths, if relative, are relative to ${CMAKE_CURRENT_SOURCE_DIR}. 189 | # Any modules previously defined with install_lua_module are automatically 190 | # preloaded (via package.preload) prior to running the test script. 191 | # Under LuaDist, set test=true in config.lua to enable testing. 192 | # USE: add_lua_test ( test/test1.lua [args...] [WORKING_DIRECTORY dir]) 193 | macro ( add_lua_test _testfile ) 194 | if ( NOT SKIP_TESTING ) 195 | parse_arguments ( _ARG "WORKING_DIRECTORY" "" ${ARGN} ) 196 | include ( CTest ) 197 | find_program ( LUA NAMES lua lua.bat ) 198 | get_filename_component ( TESTFILEABS ${_testfile} ABSOLUTE ) 199 | get_filename_component ( TESTFILENAME ${_testfile} NAME ) 200 | get_filename_component ( TESTFILEBASE ${_testfile} NAME_WE ) 201 | 202 | # Write wrapper script. 203 | # Note: One simple way to allow the script to find modules is 204 | # to just put them in package.preload. 205 | set ( TESTWRAPPER ${CMAKE_CURRENT_BINARY_DIR}/${TESTFILENAME} ) 206 | _make_module_table ( _table ) 207 | set ( TESTWRAPPERSOURCE 208 | "local CMAKE_CFG_INTDIR = ... or '.' 209 | ${_table} 210 | local function preload_modules(modules) 211 | for name, path in pairs(modules) do 212 | if path:match'%.lua' then 213 | package.preload[name] = assert(loadfile(path)) 214 | else 215 | local name = name:gsub('.*%-', '') -- remove any hyphen prefix 216 | local symbol = 'luaopen_' .. name:gsub('%.', '_') 217 | --improve: generalize to support all-in-one loader? 218 | local path = path:gsub('%$%{CMAKE_CFG_INTDIR%}', CMAKE_CFG_INTDIR) 219 | package.preload[name] = assert(package.loadlib(path, symbol)) 220 | end 221 | end 222 | end 223 | preload_modules(modules) 224 | arg[0] = '${TESTFILEABS}' 225 | table.remove(arg, 1) 226 | return assert(loadfile '${TESTFILEABS}')(unpack(arg)) 227 | " ) 228 | if ( _ARG_WORKING_DIRECTORY ) 229 | get_filename_component ( 230 | TESTCURRENTDIRABS ${_ARG_WORKING_DIRECTORY} ABSOLUTE ) 231 | # note: CMake 2.6 (unlike 2.8) lacks WORKING_DIRECTORY parameter. 232 | set ( _pre ${CMAKE_COMMAND} -E chdir "${TESTCURRENTDIRABS}" ) 233 | endif () 234 | file ( WRITE ${TESTWRAPPER} ${TESTWRAPPERSOURCE}) 235 | add_test ( NAME ${TESTFILEBASE} COMMAND ${_pre} ${LUA} 236 | ${TESTWRAPPER} "${CMAKE_CFG_INTDIR}" 237 | ${_ARG_DEFAULT_ARGS} ) 238 | endif () 239 | # see also http://gdcm.svn.sourceforge.net/viewvc/gdcm/Sandbox/CMakeModules/UsePythonTest.cmake 240 | # Note: ${CMAKE_CFG_INTDIR} is a command-line argument to allow proper 241 | # expansion by the native build tool. 242 | endmacro () 243 | 244 | 245 | # Converts Lua source file `_source` to binary string embedded in C source 246 | # file `_target`. Optionally compiles Lua source to byte code (not available 247 | # under LuaJIT2, which doesn't have a bytecode loader). Additionally, Lua 248 | # versions of bin2c [1] and luac [2] may be passed respectively as additional 249 | # arguments. 250 | # 251 | # [1] http://lua-users.org/wiki/BinToCee 252 | # [2] http://lua-users.org/wiki/LuaCompilerInLua 253 | function ( add_lua_bin2c _target _source ) 254 | find_program ( LUA NAMES lua lua.bat ) 255 | execute_process ( COMMAND ${LUA} -e "string.dump(function()end)" 256 | RESULT_VARIABLE _LUA_DUMP_RESULT ERROR_QUIET ) 257 | if ( NOT ${_LUA_DUMP_RESULT} ) 258 | SET ( HAVE_LUA_DUMP true ) 259 | endif () 260 | message ( "-- string.dump=${HAVE_LUA_DUMP}" ) 261 | 262 | if ( ARGV2 ) 263 | get_filename_component ( BIN2C ${ARGV2} ABSOLUTE ) 264 | set ( BIN2C ${LUA} ${BIN2C} ) 265 | else () 266 | find_program ( BIN2C NAMES bin2c bin2c.bat ) 267 | endif () 268 | if ( HAVE_LUA_DUMP ) 269 | if ( ARGV3 ) 270 | get_filename_component ( LUAC ${ARGV3} ABSOLUTE ) 271 | set ( LUAC ${LUA} ${LUAC} ) 272 | else () 273 | find_program ( LUAC NAMES luac luac.bat ) 274 | endif () 275 | endif ( HAVE_LUA_DUMP ) 276 | message ( "-- bin2c=${BIN2C}" ) 277 | message ( "-- luac=${LUAC}" ) 278 | 279 | get_filename_component ( SOURCEABS ${_source} ABSOLUTE ) 280 | if ( HAVE_LUA_DUMP ) 281 | get_filename_component ( SOURCEBASE ${_source} NAME_WE ) 282 | add_custom_command ( 283 | OUTPUT ${_target} DEPENDS ${_source} 284 | COMMAND ${LUAC} -o ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo 285 | ${SOURCEABS} 286 | COMMAND ${BIN2C} ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo 287 | ">${_target}" ) 288 | else () 289 | add_custom_command ( 290 | OUTPUT ${_target} DEPENDS ${SOURCEABS} 291 | COMMAND ${BIN2C} ${_source} ">${_target}" ) 292 | endif () 293 | endfunction() 294 | -------------------------------------------------------------------------------- /dist.info: -------------------------------------------------------------------------------- 1 | --- This file is part of LuaDist project 2 | 3 | name = "luacurl" 4 | version = "1.2.1" 5 | 6 | desc = "LuaCURL is Lua 5.x compatible module providing Internet browsing capabilities based on the cURL library." 7 | author = "Alexander Marinov, Enrico Tassi" 8 | license = "MIT/X11" 9 | url = "http://luaforge.net/projects/luacurl/" 10 | maintainer = "Peter Drahoš" 11 | 12 | depends = { 13 | "lua ~> 5.1", 14 | "curl >= 7.18" 15 | } 16 | -------------------------------------------------------------------------------- /luacurl.c: -------------------------------------------------------------------------------- 1 | /* luacurl.c 2 | * 3 | * author : Alexander Marinov (alekmarinov@gmail.com) 4 | * project : luacurl 5 | * description : Binds libCURL to Lua 6 | * copyright : The same as Lua license (http://www.lua.org/license.html) and 7 | * curl license (http://curl.haxx.se/docs/copyright.html) 8 | * todo : multipart formpost building, 9 | * curl multi 10 | * 11 | * Contributors: Thomas Harning added support for tables/threads as the CURLOPT_*DATA items. 12 | *******************************************************************************************/ 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #ifndef LIBCURL_VERSION 20 | #include 21 | #endif 22 | 23 | #include 24 | 25 | #if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM <= 500) 26 | #define luaL_checkstring luaL_check_string 27 | #endif 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | #ifndef LUACURL_API 34 | #define LUACURL_API extern 35 | #endif 36 | 37 | #define LUACURL_LIBNAME "curl" 38 | #define CURLHANDLE "curlT" 39 | 40 | #define MAKE_VERSION_NUM(x,y,z) (z + (y << 8) + (x << 16)) 41 | #define CURL_NEWER(x,y,z) (MAKE_VERSION_NUM(x,y,z) <= LIBCURL_VERSION_NUM) 42 | #define CURL_OLDER(x,y,z) (MAKE_VERSION_NUM(x,y,z) > LIBCURL_VERSION_NUM) 43 | 44 | /* Fast set table macro */ 45 | #define LUA_SET_TABLE(context, key_type, key, value_type, value) \ 46 | lua_push##key_type(context, key); \ 47 | lua_push##value_type(context, value); \ 48 | lua_settable(context, -3); 49 | 50 | /* wrap curl option with simple option type */ 51 | #define C_OPT(n, t) 52 | /* wrap curl option with string list type */ 53 | #define C_OPT_SL(n) static const char* KEY_##n = #n; 54 | /* wrap the other curl options not included above */ 55 | #define C_OPT_SPECIAL(n) 56 | 57 | /* describes all currently supported curl options available to curl 7.15.2 */ 58 | #define ALL_CURL_OPT \ 59 | C_OPT_SPECIAL(WRITEDATA) \ 60 | C_OPT(URL, string) \ 61 | C_OPT(PORT, number) \ 62 | C_OPT(PROXY, string) \ 63 | C_OPT(USERPWD, string) \ 64 | C_OPT(PROXYUSERPWD, string) \ 65 | C_OPT(RANGE, string) \ 66 | C_OPT_SPECIAL(READDATA) \ 67 | C_OPT_SPECIAL(WRITEFUNCTION) \ 68 | C_OPT_SPECIAL(READFUNCTION) \ 69 | C_OPT(TIMEOUT, number) \ 70 | C_OPT(INFILESIZE, number) \ 71 | C_OPT(POSTFIELDS, string) \ 72 | C_OPT(REFERER, string) \ 73 | C_OPT(FTPPORT, string) \ 74 | C_OPT(USERAGENT, string) \ 75 | C_OPT(LOW_SPEED_LIMIT, number) \ 76 | C_OPT(LOW_SPEED_TIME, number) \ 77 | C_OPT(RESUME_FROM, number) \ 78 | C_OPT(COOKIE, string) \ 79 | C_OPT_SL(HTTPHEADER) \ 80 | C_OPT_SL(HTTPPOST) \ 81 | C_OPT(SSLCERT, string) \ 82 | C_OPT(SSLKEYPASSWD, string) \ 83 | C_OPT(CRLF, boolean) \ 84 | C_OPT_SL(QUOTE) \ 85 | C_OPT_SPECIAL(HEADERDATA) \ 86 | C_OPT(COOKIEFILE, string) \ 87 | C_OPT(SSLVERSION, number) \ 88 | C_OPT(TIMECONDITION, number) \ 89 | C_OPT(TIMEVALUE, number) \ 90 | C_OPT(CUSTOMREQUEST, string) \ 91 | C_OPT_SL(POSTQUOTE) \ 92 | C_OPT(WRITEINFO, string) \ 93 | C_OPT(VERBOSE, boolean) \ 94 | C_OPT(HEADER, boolean) \ 95 | C_OPT(NOPROGRESS, boolean) \ 96 | C_OPT(NOBODY, boolean) \ 97 | C_OPT(FAILONERROR, boolean) \ 98 | C_OPT(UPLOAD, boolean) \ 99 | C_OPT(POST, boolean) \ 100 | C_OPT(FTPLISTONLY, boolean) \ 101 | C_OPT(FTPAPPEND, boolean) \ 102 | C_OPT(NETRC, number) \ 103 | C_OPT(FOLLOWLOCATION, boolean) \ 104 | C_OPT(TRANSFERTEXT, boolean) \ 105 | C_OPT(PUT, boolean) \ 106 | C_OPT_SPECIAL(PROGRESSFUNCTION) \ 107 | C_OPT_SPECIAL(PROGRESSDATA) \ 108 | C_OPT(AUTOREFERER, boolean) \ 109 | C_OPT(PROXYPORT, number) \ 110 | C_OPT(POSTFIELDSIZE, number) \ 111 | C_OPT(HTTPPROXYTUNNEL, boolean) \ 112 | C_OPT(INTERFACE, string) \ 113 | C_OPT(KRB4LEVEL, string) \ 114 | C_OPT(SSL_VERIFYPEER, boolean) \ 115 | C_OPT(CAINFO, string) \ 116 | C_OPT(MAXREDIRS, number) \ 117 | C_OPT(FILETIME, boolean) \ 118 | C_OPT_SL(TELNETOPTIONS) \ 119 | C_OPT(MAXCONNECTS, number) \ 120 | C_OPT(CLOSEPOLICY, number) \ 121 | C_OPT(FRESH_CONNECT, boolean) \ 122 | C_OPT(FORBID_REUSE, boolean) \ 123 | C_OPT(RANDOM_FILE, string) \ 124 | C_OPT(EGDSOCKET, string) \ 125 | C_OPT(CONNECTTIMEOUT, number) \ 126 | C_OPT_SPECIAL(HEADERFUNCTION) \ 127 | C_OPT(HTTPGET, boolean) \ 128 | C_OPT(SSL_VERIFYHOST, number) \ 129 | C_OPT(COOKIEJAR, string) \ 130 | C_OPT(SSL_CIPHER_LIST, string) \ 131 | C_OPT(HTTP_VERSION, number) \ 132 | C_OPT(FTP_USE_EPSV, boolean) \ 133 | C_OPT(SSLCERTTYPE, string) \ 134 | C_OPT(SSLKEY, string) \ 135 | C_OPT(SSLKEYTYPE, string) \ 136 | C_OPT(SSLENGINE, string) \ 137 | C_OPT(SSLENGINE_DEFAULT, boolean) \ 138 | C_OPT(DNS_USE_GLOBAL_CACHE, boolean) \ 139 | C_OPT(DNS_CACHE_TIMEOUT, number) \ 140 | C_OPT_SL(PREQUOTE) \ 141 | C_OPT(COOKIESESSION, boolean) \ 142 | C_OPT(CAPATH, string) \ 143 | C_OPT(BUFFERSIZE, number) \ 144 | C_OPT(NOSIGNAL, boolean) \ 145 | C_OPT(PROXYTYPE, number) \ 146 | C_OPT(ENCODING, string) \ 147 | C_OPT_SL(HTTP200ALIASES) \ 148 | C_OPT(UNRESTRICTED_AUTH, boolean) \ 149 | C_OPT(FTP_USE_EPRT, boolean) \ 150 | C_OPT(HTTPAUTH, number) \ 151 | C_OPT(FTP_CREATE_MISSING_DIRS, boolean) \ 152 | C_OPT(PROXYAUTH, number) \ 153 | C_OPT(FTP_RESPONSE_TIMEOUT, number) \ 154 | C_OPT(IPRESOLVE, number) \ 155 | C_OPT(MAXFILESIZE, number) \ 156 | C_OPT(INFILESIZE_LARGE, number) \ 157 | C_OPT(RESUME_FROM_LARGE, number) \ 158 | C_OPT(MAXFILESIZE_LARGE, number) \ 159 | C_OPT(NETRC_FILE, string) \ 160 | C_OPT(FTP_SSL, number) \ 161 | C_OPT(POSTFIELDSIZE_LARGE, number) \ 162 | C_OPT(TCP_NODELAY, boolean) \ 163 | C_OPT(SOURCE_USERPWD, string) \ 164 | C_OPT_SL(SOURCE_PREQUOTE) \ 165 | C_OPT_SL(SOURCE_POSTQUOTE) \ 166 | C_OPT(FTPSSLAUTH, number) \ 167 | C_OPT_SPECIAL(IOCTLFUNCTION) \ 168 | C_OPT_SPECIAL(IOCTLDATA) \ 169 | C_OPT(SOURCE_URL, string) \ 170 | C_OPT_SL(SOURCE_QUOTE) \ 171 | C_OPT(FTP_ACCOUNT, string) 172 | 173 | #if CURL_OLDER(7,12,1) || CURL_NEWER(7,16,2) 174 | #define CURLOPT_SOURCE_PREQUOTE -MAKE_VERSION_NUM(1,12,7)+0 175 | #define CURLOPT_SOURCE_POSTQUOTE -MAKE_VERSION_NUM(1,12,7)+1 176 | #define CURLOPT_SOURCE_USERPWD -MAKE_VERSION_NUM(1,12,7)+2 177 | #endif 178 | 179 | #if CURL_OLDER(7,12,2) 180 | #define CURLOPT_FTPSSLAUTH -MAKE_VERSION_NUM(2,12,7)+0 181 | #endif 182 | 183 | #if CURL_OLDER(7,12,3) 184 | #define CURLOPT_TCP_NODELAY -MAKE_VERSION_NUM(3,12,7)+0 185 | #define CURLOPT_IOCTLFUNCTION -MAKE_VERSION_NUM(3,12,7)+1 186 | #define CURLOPT_IOCTLDATA -MAKE_VERSION_NUM(3,12,7)+2 187 | #define CURLE_SSL_ENGINE_INITFAILED -MAKE_VERSION_NUM(3,12,7)+3 188 | #define CURLE_IOCTLFUNCTION -MAKE_VERSION_NUM(3,12,7)+4 189 | #define CURLE_IOCTLDATA -MAKE_VERSION_NUM(3,12,7)+5 190 | #endif 191 | 192 | #if CURL_OLDER(7,13,0) 193 | #define CURLOPT_FTP_ACCOUNT -MAKE_VERSION_NUM(0,13,7)+2 194 | #define CURLE_INTERFACE_FAILED -MAKE_VERSION_NUM(0,13,7)+3 195 | #define CURLE_SEND_FAIL_REWIND -MAKE_VERSION_NUM(0,13,7)+4 196 | #endif 197 | 198 | #if CURL_OLDER(7,13,0) || CURL_NEWER(7,16,2) 199 | #define CURLOPT_SOURCE_URL -MAKE_VERSION_NUM(0,13,7)+0 200 | #define CURLOPT_SOURCE_QUOTE -MAKE_VERSION_NUM(0,13,7)+1 201 | #endif 202 | 203 | #if CURL_OLDER(7,13,1) 204 | #define CURLE_LOGIN_DENIED -MAKE_VERSION_NUM(1,13,7)+0 205 | #endif 206 | 207 | /* register static the names of options of type curl_slist */ 208 | ALL_CURL_OPT 209 | 210 | /* not supported options for any reason 211 | CURLOPT_STDERR (TODO) 212 | CURLOPT_ERRORBUFFER (all curl operations return error message via curl_easy_strerror) 213 | CURLOPT_SHARE (TODO) 214 | CURLOPT_PRIVATE (TODO) 215 | CURLOPT_DEBUGFUNCTION (requires curl debug mode) 216 | CURLOPT_DEBUGDATA 217 | CURLOPT_SSLCERTPASSWD (duplicated by SSLKEYPASSWD) 218 | CURLOPT_SSL_CTX_FUNCTION (TODO, this will make sense only if openssl is available in Lua) 219 | CURLOPT_SSL_CTX_DATA 220 | */ 221 | 222 | /* wrap values of arbitrary type */ 223 | union luaValueT 224 | { 225 | struct curl_slist *slist; 226 | curl_read_callback rcb; 227 | curl_write_callback wcb; 228 | curl_progress_callback pcb; 229 | 230 | #if CURL_NEWER(7,12,3) 231 | curl_ioctl_callback icb; 232 | #endif 233 | 234 | long nval; 235 | char* sval; 236 | void* ptr; 237 | }; 238 | 239 | /* CURL object wrapper type */ 240 | typedef struct 241 | { 242 | CURL* curl; 243 | lua_State* L; 244 | int fwriterRef; int wudtype; union luaValueT wud; 245 | int freaderRef; int rudtype; union luaValueT rud; 246 | int fprogressRef; int pudtype; union luaValueT pud; 247 | int fheaderRef; int hudtype; union luaValueT hud; 248 | int fioctlRef; int iudtype; union luaValueT iud; 249 | } curlT; 250 | 251 | /* push correctly luaValue to Lua stack */ 252 | static void pushLuaValueT(lua_State* L, int t, union luaValueT v) 253 | { 254 | switch (t) 255 | { 256 | case LUA_TNIL: lua_pushnil(L); break; 257 | case LUA_TBOOLEAN: lua_pushboolean(L, v.nval); break; 258 | case LUA_TTABLE: 259 | case LUA_TFUNCTION: 260 | case LUA_TTHREAD: 261 | case LUA_TUSERDATA: lua_rawgeti(L, LUA_REGISTRYINDEX, v.nval); break; 262 | case LUA_TLIGHTUSERDATA: lua_pushlightuserdata(L, v.ptr); break; 263 | case LUA_TNUMBER: lua_pushnumber(L, v.nval); break; 264 | case LUA_TSTRING: lua_pushstring(L, v.sval); break; 265 | default: luaL_error(L, "invalid type %s\n", lua_typename(L, t)); 266 | } 267 | } 268 | 269 | /* curl callbacks connected with Lua functions */ 270 | static size_t readerCallback( void *ptr, size_t size, size_t nmemb, void *stream) 271 | { 272 | const char* readBytes; 273 | curlT* c=(curlT*)stream; 274 | lua_rawgeti(c->L, LUA_REGISTRYINDEX, c->freaderRef); 275 | pushLuaValueT(c->L, c->rudtype, c->rud); 276 | lua_pushnumber(c->L, size * nmemb); 277 | lua_call(c->L, 2, 1); 278 | readBytes=lua_tostring(c->L, -1); 279 | if (readBytes) 280 | { 281 | memcpy(ptr, readBytes, lua_strlen(c->L, -1)); 282 | return lua_strlen(c->L, -1); 283 | } 284 | return 0; 285 | } 286 | 287 | static size_t writerCallback(void *ptr, size_t size, size_t nmemb, void *stream) 288 | { 289 | curlT* c=(curlT*)stream; 290 | lua_rawgeti(c->L, LUA_REGISTRYINDEX, c->fwriterRef); 291 | pushLuaValueT(c->L, c->wudtype, c->wud); 292 | lua_pushlstring(c->L, ptr, size * nmemb); 293 | lua_call(c->L, 2, 1); 294 | return (size_t)lua_tonumber(c->L, -1); 295 | } 296 | 297 | int progressCallback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) 298 | { 299 | curlT* c=(curlT*)clientp; 300 | lua_rawgeti(c->L, LUA_REGISTRYINDEX, c->fprogressRef); 301 | pushLuaValueT(c->L, c->pudtype, c->pud); 302 | lua_pushnumber(c->L, dltotal); 303 | lua_pushnumber(c->L, dlnow); 304 | lua_pushnumber(c->L, ultotal); 305 | lua_pushnumber(c->L, ulnow); 306 | lua_call(c->L, 5, 1); 307 | return (int)lua_tonumber(c->L, -1); 308 | } 309 | 310 | static size_t headerCallback(void *ptr, size_t size, size_t nmemb, void *stream) 311 | { 312 | curlT* c=(curlT*)stream; 313 | lua_rawgeti(c->L, LUA_REGISTRYINDEX, c->fheaderRef); 314 | pushLuaValueT(c->L, c->hudtype, c->hud); 315 | lua_pushlstring(c->L, ptr, size * nmemb); 316 | lua_call(c->L, 2, 1); 317 | return (size_t)lua_tonumber(c->L, -1); 318 | } 319 | 320 | #if CURL_NEWER(7,12,3) 321 | curlioerr ioctlCallback(CURL *handle, int cmd, void *clientp) 322 | { 323 | curlT* c=(curlT*)clientp; 324 | lua_rawgeti(c->L, LUA_REGISTRYINDEX, c->fioctlRef); 325 | pushLuaValueT(c->L, c->iudtype, c->iud); 326 | lua_pushnumber(c->L, cmd); 327 | lua_call(c->L, 2, 1); 328 | return (curlioerr)lua_tonumber(c->L, -1); 329 | } 330 | #endif 331 | 332 | /* Initializes CURL connection */ 333 | static int lcurl_easy_init(lua_State* L) 334 | { 335 | curlT* c = (curlT*)lua_newuserdata(L, sizeof(curlT)); 336 | c->L=L; 337 | c->freaderRef=c->fwriterRef=c->fprogressRef=c->fheaderRef=c->fioctlRef=LUA_REFNIL; 338 | c->rud.nval=c->wud.nval=c->pud.nval=c->hud.nval=c->iud.nval=0; 339 | c->rudtype=c->wudtype=c->pudtype=c->hudtype=c->iudtype=LUA_TNIL; 340 | /* open curl handle */ 341 | c->curl = curl_easy_init(); 342 | /* set metatable to curlT object */ 343 | luaL_getmetatable(L, CURLHANDLE); 344 | lua_setmetatable(L, -2); 345 | return 1; 346 | } 347 | 348 | /* Escapes URL strings */ 349 | static int lcurl_escape(lua_State* L) 350 | { 351 | if (!lua_isnil(L, 1)) 352 | { 353 | const char* s=luaL_checkstring(L, 1); 354 | lua_pushstring(L, curl_escape(s, (int)lua_strlen(L, 1))); 355 | return 1; 356 | } else 357 | { 358 | luaL_argerror(L, 1, "string parameter expected"); 359 | } 360 | return 0; 361 | } 362 | 363 | /* Unescapes URL encoding in strings */ 364 | static int lcurl_unescape(lua_State* L) 365 | { 366 | if (!lua_isnil(L, 1)) 367 | { 368 | const char* s=luaL_checkstring(L, 1); 369 | lua_pushstring(L, curl_unescape(s, (int)lua_strlen(L, 1))); 370 | return 1; 371 | } else 372 | { 373 | luaL_argerror(L, 1, "string parameter expected"); 374 | } 375 | return 0; 376 | } 377 | 378 | /* Access curlT object from the Lua stack at specified position */ 379 | static curlT* tocurl (lua_State *L, int cindex) 380 | { 381 | curlT* c=(curlT*)luaL_checkudata(L, cindex, CURLHANDLE); 382 | if (!c) luaL_argerror(L, cindex, "invalid curl object"); 383 | if (!c->curl) luaL_error(L, "attempt to use closed curl object"); 384 | return c; 385 | } 386 | 387 | /* Request internal information from the curl session */ 388 | static int lcurl_easy_getinfo(lua_State* L) 389 | { 390 | curlT* c=tocurl(L, 1); 391 | CURLINFO nInfo; 392 | CURLcode code=-1; 393 | luaL_checktype(L, 2, LUA_TNUMBER); /* accept info code number only */ 394 | nInfo=lua_tonumber(L, 2); 395 | if (nInfo>CURLINFO_SLIST) 396 | { 397 | /* string list */ 398 | struct curl_slist *slist=0; 399 | if (CURLE_OK == (code=curl_easy_getinfo(c->curl, nInfo, &slist))) 400 | { 401 | if (slist) 402 | { 403 | int i; 404 | lua_newtable(L); 405 | for (i=1; slist; i++, slist=slist->next) 406 | { 407 | lua_pushnumber(L, i); 408 | lua_pushstring(L, slist->data); 409 | lua_settable(L, -3); 410 | } 411 | curl_slist_free_all(slist); 412 | } else 413 | { 414 | lua_pushnil(L); 415 | } 416 | return 1; 417 | } else 418 | { 419 | /* curl_easy_getinfo returns error */ 420 | } 421 | } else 422 | if (nInfo>CURLINFO_DOUBLE) 423 | { 424 | /* double */ 425 | double value; 426 | if (CURLE_OK == (code=curl_easy_getinfo(c->curl, nInfo, &value))) 427 | { 428 | lua_pushnumber(L, value); 429 | return 1; 430 | } else 431 | { 432 | /* curl_easy_getinfo returns error */ 433 | } 434 | } else 435 | if (nInfo>CURLINFO_LONG) 436 | { 437 | /* long */ 438 | long value; 439 | if (CURLE_OK == (code=curl_easy_getinfo(c->curl, nInfo, &value))) 440 | { 441 | lua_pushinteger(L, (lua_Integer)value); 442 | return 1; 443 | } else 444 | { 445 | /* curl_easy_getinfo returns error */ 446 | } 447 | } else 448 | if (nInfo>CURLINFO_STRING) 449 | { 450 | /* string */ 451 | char* value; 452 | if (CURLE_OK == (code=curl_easy_getinfo(c->curl, nInfo, &value))) 453 | { 454 | lua_pushstring(L, value); 455 | return 1; 456 | } else 457 | { 458 | /* curl_easy_getinfo returns error */ 459 | } 460 | } 461 | /* on error */ 462 | /* return nil, error message, error code */ 463 | lua_pushnil(L); 464 | if (code>CURLE_OK) 465 | { 466 | #if CURL_NEWER(7,11,2) 467 | lua_pushstring(L, curl_easy_strerror(code)); 468 | #else 469 | lua_pushfstring(L, "Curl error: #%d", (code)); 470 | #endif 471 | lua_pushnumber(L, code); 472 | return 3; 473 | } 474 | else 475 | { 476 | lua_pushfstring(L, "Invalid CURLINFO number: %d", nInfo); 477 | return 2; 478 | } 479 | } 480 | 481 | /* convert argument n to string allowing nil values */ 482 | static union luaValueT get_string(lua_State* L, int n) 483 | { 484 | union luaValueT v; 485 | v.sval=(char*)lua_tostring(L, 3); 486 | return v; 487 | } 488 | 489 | /* convert argument n to number allowing only number convertable values */ 490 | static union luaValueT get_number(lua_State* L, int n) 491 | { 492 | union luaValueT v; 493 | v.nval=(int)luaL_checknumber(L, n); 494 | return v; 495 | } 496 | 497 | /* get argument n as boolean but if not boolean argument then fail with Lua error */ 498 | static union luaValueT get_boolean(lua_State* L, int n) 499 | { 500 | union luaValueT v; 501 | if (!lua_isboolean(L, n)) 502 | { 503 | luaL_argerror(L, n, "boolean value expected"); 504 | } 505 | v.nval=(int)lua_toboolean(L, n); 506 | return v; 507 | } 508 | 509 | /* remove and free old slist from registry if any associated with the given key */ 510 | static void free_slist(lua_State* L, const char** key) 511 | { 512 | struct curl_slist *slist; 513 | lua_pushlightuserdata(L, (void *)key); 514 | lua_rawget(L, LUA_REGISTRYINDEX); 515 | slist=(struct curl_slist *)lua_topointer(L, -1); 516 | if (slist) 517 | { 518 | curl_slist_free_all(slist); 519 | } 520 | } 521 | 522 | /* after argument number n combine all arguments to the curl type curl_slist */ 523 | static union luaValueT get_slist(lua_State* L, int n, const char** key) 524 | { 525 | int i; 526 | union luaValueT v; 527 | struct curl_slist *slist=0; 528 | 529 | /* free the previous slist if any */ 530 | free_slist(L, key); 531 | 532 | /* check if all parameters are strings */ 533 | for (i=n; ifreaderRef); /* unregister previous reference to reader if any */ 593 | c->freaderRef=ref; /* keep the reader function reference in self */ 594 | v.rcb=(curl_read_callback)readerCallback; /* redirect the option value to readerCallback */ 595 | if (CURLE_OK != (code=curl_easy_setopt(c->curl, CURLOPT_READDATA, c))) goto on_error; 596 | } 597 | else if (curlOpt == CURLOPT_WRITEFUNCTION) 598 | { 599 | luaL_unref(L, LUA_REGISTRYINDEX, c->fwriterRef); 600 | c->fwriterRef=ref; 601 | v.wcb=(curl_write_callback)writerCallback; 602 | if (CURLE_OK != (code=curl_easy_setopt(c->curl, CURLOPT_WRITEDATA, c))) goto on_error; 603 | } 604 | else if (curlOpt == CURLOPT_PROGRESSFUNCTION) 605 | { 606 | luaL_unref(L, LUA_REGISTRYINDEX, c->fprogressRef); 607 | c->fprogressRef=ref; 608 | v.pcb=(curl_progress_callback)progressCallback; 609 | if (CURLE_OK != (code=curl_easy_setopt(c->curl, CURLOPT_PROGRESSDATA, c))) goto on_error; 610 | } 611 | else if (curlOpt == CURLOPT_HEADERFUNCTION) 612 | { 613 | luaL_unref(L, LUA_REGISTRYINDEX, c->fheaderRef); 614 | c->fheaderRef=ref; 615 | v.wcb=(curl_write_callback)headerCallback; 616 | if (CURLE_OK != (code=curl_easy_setopt(c->curl, CURLOPT_HEADERDATA, c))) goto on_error; 617 | } 618 | #if CURL_NEWER(7,12,3) 619 | else if (curlOpt == CURLOPT_IOCTLFUNCTION) 620 | { 621 | luaL_unref(L, LUA_REGISTRYINDEX, c->fioctlRef); 622 | c->fioctlRef=ref; 623 | v.icb=ioctlCallback; 624 | if (CURLE_OK != (code=curl_easy_setopt(c->curl, CURLOPT_IOCTLDATA, c))) goto on_error; 625 | } 626 | #endif 627 | else 628 | { 629 | /* When the option code is any of CURLOPT_xxxDATA and the argument is table, 630 | /* userdata or function set the curl option value to the lua object reference */ 631 | v.nval=ref; 632 | } 633 | }break; 634 | }break; 635 | 636 | /* Handle all supported curl options differently according the specific option argument type */ 637 | #undef C_OPT 638 | #define C_OPT(n, t) \ 639 | case CURLOPT_##n: \ 640 | v=get_##t(L, 3); \ 641 | break; 642 | 643 | #undef C_OPT_SL 644 | #define C_OPT_SL(n) \ 645 | case CURLOPT_##n: \ 646 | { \ 647 | v=get_slist(L, 3, &KEY_##n); \ 648 | }break; 649 | 650 | /* Expands all the list of switch-case's here */ 651 | ALL_CURL_OPT 652 | 653 | default: 654 | luaL_error(L, "Not supported curl option %d", curlOpt); 655 | } 656 | 657 | /* additional check if the option value has compatible type with the option code */ 658 | switch (lua_type(L, 3)) 659 | { 660 | case LUA_TFUNCTION: /* allow function argument only for the special option codes */ 661 | if (curlOpt == CURLOPT_READFUNCTION 662 | || curlOpt == CURLOPT_WRITEFUNCTION 663 | || curlOpt == CURLOPT_PROGRESSFUNCTION 664 | || curlOpt == CURLOPT_HEADERFUNCTION 665 | || curlOpt == CURLOPT_IOCTLFUNCTION 666 | ) 667 | break; 668 | case LUA_TTABLE: /* allow table or userdata only for the callback parameter option */ 669 | case LUA_TUSERDATA: 670 | if (curlOpt != CURLOPT_READDATA 671 | && curlOpt != CURLOPT_WRITEDATA 672 | && curlOpt != CURLOPT_PROGRESSDATA 673 | && curlOpt != CURLOPT_HEADERDATA 674 | #if CURL_NEWER(7,12,3) 675 | && curlOpt != CURLOPT_IOCTLDATA 676 | #endif 677 | ) 678 | luaL_error(L, "argument #2 type %s is not compatible with this option", lua_typename(L, 3)); 679 | break; 680 | } 681 | 682 | /* handle curl option for setting callback parameter */ 683 | switch (curlOpt) 684 | { 685 | case CURLOPT_READDATA: 686 | if (c->rudtype == LUA_TFUNCTION || c->rudtype == LUA_TUSERDATA || c->rudtype == LUA_TTABLE || c->rudtype == LUA_TTHREAD) 687 | luaL_unref(L, LUA_REGISTRYINDEX, c->rud.nval); /* unref previously referenced read data */ 688 | c->rudtype=lua_type(L, 3); /* set the read data type */ 689 | c->rud=v; /* set the read data value (it can be reference) */ 690 | v.ptr=c; /* set the real read data to curl as our self object */ 691 | break; 692 | case CURLOPT_WRITEDATA: 693 | if (c->wudtype == LUA_TFUNCTION || c->wudtype == LUA_TUSERDATA || c->wudtype == LUA_TTABLE || c->wudtype == LUA_TTHREAD) 694 | luaL_unref(L, LUA_REGISTRYINDEX, c->wud.nval); 695 | c->wudtype=lua_type(L, 3); 696 | c->wud=v; 697 | v.ptr=c; 698 | break; 699 | case CURLOPT_PROGRESSDATA: 700 | if (c->pudtype == LUA_TFUNCTION || c->pudtype == LUA_TUSERDATA || c->pudtype == LUA_TTABLE || c->pudtype == LUA_TTHREAD) 701 | luaL_unref(L, LUA_REGISTRYINDEX, c->pud.nval); 702 | c->pudtype=lua_type(L, 3); 703 | c->pud=v; 704 | v.ptr=c; 705 | break; 706 | case CURLOPT_HEADERDATA: 707 | if (c->hudtype == LUA_TFUNCTION || c->hudtype == LUA_TUSERDATA || c->hudtype == LUA_TTABLE || c->hudtype == LUA_TTHREAD) 708 | luaL_unref(L, LUA_REGISTRYINDEX, c->hud.nval); 709 | c->hudtype=lua_type(L, 3); 710 | c->hud=v; 711 | v.ptr=c; 712 | break; 713 | #if CURL_NEWER(7,12,3) 714 | case CURLOPT_IOCTLDATA: 715 | if (c->iudtype == LUA_TFUNCTION || c->iudtype == LUA_TUSERDATA || c->iudtype == LUA_TTABLE || c->iudtype == LUA_TTHREAD) 716 | luaL_unref(L, LUA_REGISTRYINDEX, c->iud.nval); 717 | c->iudtype=lua_type(L, 3); 718 | c->iud=v; 719 | v.ptr=c; 720 | break; 721 | #endif 722 | } 723 | 724 | /* set easy the curl option with the processed value */ 725 | if (CURLE_OK == (code=curl_easy_setopt(c->curl, (int)lua_tonumber(L, 2), v.nval))) 726 | { 727 | /* on success return true */ 728 | lua_pushboolean(L, 1); 729 | return 1; 730 | } 731 | on_error: 732 | /* on fail return nil, error message, error code */ 733 | lua_pushnil(L); 734 | #if CURL_NEWER(7,11,2) 735 | lua_pushstring(L, curl_easy_strerror(code)); 736 | #else 737 | lua_pushfstring(L, "Curl error: #%d", (code)); 738 | #endif 739 | lua_pushnumber(L, code); 740 | return 3; 741 | } 742 | 743 | /* perform the curl commands */ 744 | static int lcurl_easy_perform(lua_State* L) 745 | { 746 | CURLcode code; /* return error code from curl */ 747 | curlT* c = tocurl(L, 1); /* get self object */ 748 | code = curl_easy_perform(c->curl); /* do the curl perform */ 749 | if (CURLE_OK == code) 750 | { 751 | /* on success return true */ 752 | lua_pushboolean(L, 1); 753 | return 1; 754 | } 755 | /* on fail return nil, error message, error code */ 756 | lua_pushnil(L); 757 | #if CURL_NEWER(7,11,2) 758 | lua_pushstring(L, curl_easy_strerror(code)); 759 | #else 760 | lua_pushfstring(L, "Curl error: #%d", (code)); 761 | #endif 762 | lua_pushnumber(L, code); 763 | return 3; 764 | } 765 | 766 | /* Finalizes CURL */ 767 | static int lcurl_easy_close(lua_State* L) 768 | { 769 | curlT* c=tocurl(L, 1); 770 | curl_easy_cleanup(c->curl); 771 | luaL_unref(L, LUA_REGISTRYINDEX, c->freaderRef); 772 | luaL_unref(L, LUA_REGISTRYINDEX, c->fwriterRef); 773 | luaL_unref(L, LUA_REGISTRYINDEX, c->fprogressRef); 774 | luaL_unref(L, LUA_REGISTRYINDEX, c->fheaderRef); 775 | luaL_unref(L, LUA_REGISTRYINDEX, c->fioctlRef); 776 | if (c->rudtype == LUA_TFUNCTION || c->rudtype == LUA_TUSERDATA || c->rudtype == LUA_TTABLE || c->rudtype == LUA_TTHREAD) 777 | luaL_unref(L, LUA_REGISTRYINDEX, c->rud.nval); 778 | if (c->wudtype == LUA_TFUNCTION || c->wudtype == LUA_TUSERDATA || c->wudtype == LUA_TTABLE || c->wudtype == LUA_TTHREAD) 779 | luaL_unref(L, LUA_REGISTRYINDEX, c->wud.nval); 780 | if (c->pudtype == LUA_TFUNCTION || c->pudtype == LUA_TUSERDATA || c->pudtype == LUA_TTABLE || c->pudtype == LUA_TTHREAD) 781 | luaL_unref(L, LUA_REGISTRYINDEX, c->pud.nval); 782 | if (c->hudtype == LUA_TFUNCTION || c->hudtype == LUA_TUSERDATA || c->hudtype == LUA_TTABLE || c->hudtype == LUA_TTHREAD) 783 | luaL_unref(L, LUA_REGISTRYINDEX, c->hud.nval); 784 | if (c->iudtype == LUA_TFUNCTION || c->iudtype == LUA_TUSERDATA || c->iudtype == LUA_TTABLE || c->iudtype == LUA_TTHREAD) 785 | luaL_unref(L, LUA_REGISTRYINDEX, c->iud.nval); 786 | 787 | #undef C_OPT 788 | #undef C_OPT_SL 789 | #undef C_OPT_SPECIAL 790 | #define C_OPT(n, t) 791 | #define C_OPT_SL(n) free_slist(L, &KEY_##n); 792 | #define C_OPT_SPECIAL(n) 793 | 794 | ALL_CURL_OPT 795 | 796 | c->curl=0; 797 | lua_pushboolean(L, 1); 798 | return 1; 799 | } 800 | 801 | /* garbage collect the curl object */ 802 | static int lcurl_gc(lua_State* L) 803 | { 804 | curlT* c = (curlT*)luaL_checkudata(L, 1, CURLHANDLE); 805 | if (c && c->curl) 806 | { 807 | curl_easy_cleanup(c->curl); 808 | } 809 | return 0; 810 | } 811 | 812 | 813 | static const struct luaL_reg luacurl_meths[] = 814 | { 815 | {"close", lcurl_easy_close}, 816 | {"setopt", lcurl_easy_setopt}, 817 | {"perform", lcurl_easy_perform}, 818 | {"getinfo", lcurl_easy_getinfo}, 819 | {"__gc", lcurl_gc}, 820 | {0, 0} 821 | }; 822 | 823 | static const struct luaL_reg luacurl_funcs[] = 824 | { 825 | {"new", lcurl_easy_init}, 826 | {"escape", lcurl_escape}, 827 | {"unescape", lcurl_unescape}, 828 | {0, 0} 829 | }; 830 | 831 | static void createmeta (lua_State *L) 832 | { 833 | luaL_newmetatable(L, CURLHANDLE); 834 | lua_pushliteral(L, "__index"); 835 | lua_pushvalue(L, -2); 836 | lua_rawset(L, -3); 837 | } 838 | 839 | /* 840 | * Assumes the table is on top of the stack. 841 | */ 842 | static void set_info (lua_State *L) 843 | { 844 | LUA_SET_TABLE(L, literal, "_COPYRIGHT", literal, "(C) 2003-2006 AVIQ Systems AG"); 845 | LUA_SET_TABLE(L, literal, "_DESCRIPTION", literal, "LuaCurl binds the CURL easy interface to Lua"); 846 | LUA_SET_TABLE(L, literal, "_NAME", literal, "luacurl"); 847 | LUA_SET_TABLE(L, literal, "_VERSION", literal, "1.1.0"); 848 | LUA_SET_TABLE(L, literal, "_CURLVERSION", string, curl_version()); 849 | LUA_SET_TABLE(L, literal, "_SUPPORTED_CURLVERSION", literal, LIBCURL_VERSION); 850 | } 851 | 852 | static void setcurlerrors(lua_State* L) 853 | { 854 | LUA_SET_TABLE(L, literal, "OK", number, CURLE_OK); 855 | LUA_SET_TABLE(L, literal, "FAILED_INIT", number, CURLE_FAILED_INIT); 856 | LUA_SET_TABLE(L, literal, "UNSUPPORTED_PROTOCOL", number, CURLE_UNSUPPORTED_PROTOCOL); 857 | LUA_SET_TABLE(L, literal, "URL_MALFORMAT", number, CURLE_URL_MALFORMAT); 858 | LUA_SET_TABLE(L, literal, "URL_MALFORMAT_USER", number, CURLE_URL_MALFORMAT_USER); 859 | LUA_SET_TABLE(L, literal, "COULDNT_RESOLVE_PROXY", number, CURLE_COULDNT_RESOLVE_PROXY); 860 | LUA_SET_TABLE(L, literal, "COULDNT_RESOLVE_HOST", number, CURLE_COULDNT_RESOLVE_HOST); 861 | LUA_SET_TABLE(L, literal, "COULDNT_CONNECT", number, CURLE_COULDNT_CONNECT); 862 | LUA_SET_TABLE(L, literal, "FTP_WEIRD_SERVER_REPLY", number, CURLE_FTP_WEIRD_SERVER_REPLY); 863 | LUA_SET_TABLE(L, literal, "FTP_ACCESS_DENIED", number, CURLE_FTP_ACCESS_DENIED); 864 | LUA_SET_TABLE(L, literal, "FTP_USER_PASSWORD_INCORRECT", number, CURLE_FTP_USER_PASSWORD_INCORRECT); 865 | LUA_SET_TABLE(L, literal, "FTP_WEIRD_PASS_REPLY", number, CURLE_FTP_WEIRD_PASS_REPLY); 866 | LUA_SET_TABLE(L, literal, "FTP_WEIRD_USER_REPLY", number, CURLE_FTP_WEIRD_USER_REPLY); 867 | LUA_SET_TABLE(L, literal, "FTP_WEIRD_PASV_REPLY", number, CURLE_FTP_WEIRD_PASV_REPLY); 868 | LUA_SET_TABLE(L, literal, "FTP_WEIRD_227_FORMAT", number, CURLE_FTP_WEIRD_227_FORMAT); 869 | LUA_SET_TABLE(L, literal, "FTP_CANT_GET_HOST", number, CURLE_FTP_CANT_GET_HOST); 870 | LUA_SET_TABLE(L, literal, "FTP_CANT_RECONNECT", number, CURLE_FTP_CANT_RECONNECT); 871 | LUA_SET_TABLE(L, literal, "FTP_COULDNT_SET_BINARY", number, CURLE_FTP_COULDNT_SET_BINARY); 872 | LUA_SET_TABLE(L, literal, "PARTIAL_FILE", number, CURLE_PARTIAL_FILE); 873 | LUA_SET_TABLE(L, literal, "FTP_COULDNT_RETR_FILE", number, CURLE_FTP_COULDNT_RETR_FILE); 874 | LUA_SET_TABLE(L, literal, "FTP_WRITE_ERROR", number, CURLE_FTP_WRITE_ERROR); 875 | LUA_SET_TABLE(L, literal, "FTP_QUOTE_ERROR", number, CURLE_FTP_QUOTE_ERROR); 876 | LUA_SET_TABLE(L, literal, "HTTP_RETURNED_ERROR", number, CURLE_HTTP_RETURNED_ERROR); 877 | LUA_SET_TABLE(L, literal, "WRITE_ERROR", number, CURLE_WRITE_ERROR); 878 | LUA_SET_TABLE(L, literal, "MALFORMAT_USER", number, CURLE_MALFORMAT_USER); 879 | LUA_SET_TABLE(L, literal, "FTP_COULDNT_STOR_FILE", number, CURLE_FTP_COULDNT_STOR_FILE); 880 | LUA_SET_TABLE(L, literal, "READ_ERROR", number, CURLE_READ_ERROR); 881 | LUA_SET_TABLE(L, literal, "OUT_OF_MEMORY", number, CURLE_OUT_OF_MEMORY); 882 | LUA_SET_TABLE(L, literal, "OPERATION_TIMEOUTED", number, CURLE_OPERATION_TIMEOUTED); 883 | LUA_SET_TABLE(L, literal, "FTP_COULDNT_SET_ASCII", number, CURLE_FTP_COULDNT_SET_ASCII); 884 | LUA_SET_TABLE(L, literal, "FTP_PORT_FAILED", number, CURLE_FTP_PORT_FAILED); 885 | LUA_SET_TABLE(L, literal, "FTP_COULDNT_USE_REST", number, CURLE_FTP_COULDNT_USE_REST); 886 | LUA_SET_TABLE(L, literal, "FTP_COULDNT_GET_SIZE", number, CURLE_FTP_COULDNT_GET_SIZE); 887 | LUA_SET_TABLE(L, literal, "HTTP_RANGE_ERROR", number, CURLE_HTTP_RANGE_ERROR); 888 | LUA_SET_TABLE(L, literal, "HTTP_POST_ERROR", number, CURLE_HTTP_POST_ERROR); 889 | LUA_SET_TABLE(L, literal, "SSL_CONNECT_ERROR", number, CURLE_SSL_CONNECT_ERROR); 890 | LUA_SET_TABLE(L, literal, "BAD_DOWNLOAD_RESUME", number, CURLE_BAD_DOWNLOAD_RESUME); 891 | LUA_SET_TABLE(L, literal, "FILE_COULDNT_READ_FILE", number, CURLE_FILE_COULDNT_READ_FILE); 892 | LUA_SET_TABLE(L, literal, "LDAP_CANNOT_BIND", number, CURLE_LDAP_CANNOT_BIND); 893 | LUA_SET_TABLE(L, literal, "LDAP_SEARCH_FAILED", number, CURLE_LDAP_SEARCH_FAILED); 894 | LUA_SET_TABLE(L, literal, "LIBRARY_NOT_FOUND", number, CURLE_LIBRARY_NOT_FOUND); 895 | LUA_SET_TABLE(L, literal, "FUNCTION_NOT_FOUND", number, CURLE_FUNCTION_NOT_FOUND); 896 | LUA_SET_TABLE(L, literal, "ABORTED_BY_CALLBACK", number, CURLE_ABORTED_BY_CALLBACK); 897 | LUA_SET_TABLE(L, literal, "BAD_FUNCTION_ARGUMENT", number, CURLE_BAD_FUNCTION_ARGUMENT); 898 | LUA_SET_TABLE(L, literal, "BAD_CALLING_ORDER", number, CURLE_BAD_CALLING_ORDER); 899 | LUA_SET_TABLE(L, literal, "INTERFACE_FAILED", number, CURLE_INTERFACE_FAILED); 900 | LUA_SET_TABLE(L, literal, "BAD_PASSWORD_ENTERED", number, CURLE_BAD_PASSWORD_ENTERED); 901 | LUA_SET_TABLE(L, literal, "TOO_MANY_REDIRECTS", number, CURLE_TOO_MANY_REDIRECTS); 902 | LUA_SET_TABLE(L, literal, "UNKNOWN_TELNET_OPTION", number, CURLE_UNKNOWN_TELNET_OPTION); 903 | LUA_SET_TABLE(L, literal, "TELNET_OPTION_SYNTAX", number, CURLE_TELNET_OPTION_SYNTAX); 904 | LUA_SET_TABLE(L, literal, "OBSOLETE", number, CURLE_OBSOLETE); 905 | LUA_SET_TABLE(L, literal, "SSL_PEER_CERTIFICATE", number, CURLE_SSL_PEER_CERTIFICATE); 906 | LUA_SET_TABLE(L, literal, "GOT_NOTHING", number, CURLE_GOT_NOTHING); 907 | LUA_SET_TABLE(L, literal, "SSL_ENGINE_NOTFOUND", number, CURLE_SSL_ENGINE_NOTFOUND); 908 | LUA_SET_TABLE(L, literal, "SSL_ENGINE_SETFAILED", number, CURLE_SSL_ENGINE_SETFAILED); 909 | LUA_SET_TABLE(L, literal, "SEND_ERROR", number, CURLE_SEND_ERROR); 910 | LUA_SET_TABLE(L, literal, "RECV_ERROR", number, CURLE_RECV_ERROR); 911 | LUA_SET_TABLE(L, literal, "SHARE_IN_USE", number, CURLE_SHARE_IN_USE); 912 | LUA_SET_TABLE(L, literal, "SSL_CERTPROBLEM", number, CURLE_SSL_CERTPROBLEM); 913 | LUA_SET_TABLE(L, literal, "SSL_CIPHER", number, CURLE_SSL_CIPHER); 914 | LUA_SET_TABLE(L, literal, "SSL_CACERT", number, CURLE_SSL_CACERT); 915 | LUA_SET_TABLE(L, literal, "BAD_CONTENT_ENCODING", number, CURLE_BAD_CONTENT_ENCODING); 916 | LUA_SET_TABLE(L, literal, "LDAP_INVALID_URL", number, CURLE_LDAP_INVALID_URL); 917 | LUA_SET_TABLE(L, literal, "FILESIZE_EXCEEDED", number, CURLE_FILESIZE_EXCEEDED); 918 | LUA_SET_TABLE(L, literal, "FTP_SSL_FAILED", number, CURLE_FTP_SSL_FAILED); 919 | LUA_SET_TABLE(L, literal, "SEND_FAIL_REWIND", number, CURLE_SEND_FAIL_REWIND); 920 | LUA_SET_TABLE(L, literal, "SSL_ENGINE_INITFAILED", number, CURLE_SSL_ENGINE_INITFAILED); 921 | LUA_SET_TABLE(L, literal, "LOGIN_DENIED", number, CURLE_LOGIN_DENIED); 922 | } 923 | 924 | static void setcurloptions(lua_State* L) 925 | { 926 | #undef C_OPT_SPECIAL 927 | #undef C_OPT 928 | #undef C_OPT_SL 929 | #define C_OPT(n, t) LUA_SET_TABLE(L, literal, "OPT_"#n, number, CURLOPT_##n); 930 | #define C_OPT_SL(n) C_OPT(n, dummy) 931 | #define C_OPT_SPECIAL(n) C_OPT(n, dummy) 932 | 933 | ALL_CURL_OPT 934 | } 935 | 936 | static void setcurlvalues(lua_State* L) 937 | { 938 | #if CURL_NEWER(7,12,1) 939 | LUA_SET_TABLE(L, literal, "READFUNC_ABORT", number, CURL_READFUNC_ABORT); 940 | #endif 941 | 942 | #if CURL_NEWER(7,12,3) 943 | /* enum curlioerr */ 944 | LUA_SET_TABLE(L, literal, "IOE_OK", number, CURLIOE_OK); 945 | LUA_SET_TABLE(L, literal, "IOE_UNKNOWNCMD", number, CURLIOE_UNKNOWNCMD); 946 | LUA_SET_TABLE(L, literal, "IOE_FAILRESTART", number, CURLIOE_FAILRESTART); 947 | 948 | /* enum curliocmd */ 949 | LUA_SET_TABLE(L, literal, "IOCMD_NOP", number, CURLIOCMD_NOP); 950 | LUA_SET_TABLE(L, literal, "IOCMD_RESTARTREAD", number, CURLIOCMD_RESTARTREAD); 951 | #endif 952 | 953 | /* enum curl_proxytype */ 954 | LUA_SET_TABLE(L, literal, "PROXY_HTTP", number, CURLPROXY_HTTP); 955 | LUA_SET_TABLE(L, literal, "PROXY_SOCKS4", number, CURLPROXY_SOCKS4); 956 | LUA_SET_TABLE(L, literal, "PROXY_SOCKS5", number, CURLPROXY_SOCKS5); 957 | 958 | /* auth types */ 959 | LUA_SET_TABLE(L, literal, "AUTH_NONE", number, CURLAUTH_NONE); 960 | LUA_SET_TABLE(L, literal, "AUTH_BASIC", number, CURLAUTH_BASIC); 961 | LUA_SET_TABLE(L, literal, "AUTH_DIGEST", number, CURLAUTH_DIGEST); 962 | LUA_SET_TABLE(L, literal, "AUTH_GSSNEGOTIATE", number, CURLAUTH_GSSNEGOTIATE); 963 | LUA_SET_TABLE(L, literal, "AUTH_NTLM", number, CURLAUTH_NTLM); 964 | LUA_SET_TABLE(L, literal, "AUTH_ANY", number, CURLAUTH_ANY); 965 | LUA_SET_TABLE(L, literal, "AUTH_ANYSAFE", number, CURLAUTH_ANYSAFE); 966 | 967 | /* enum curl_ftpssl */ 968 | LUA_SET_TABLE(L, literal, "FTPSSL_NONE", number, CURLFTPSSL_NONE); 969 | LUA_SET_TABLE(L, literal, "FTPSSL_TRY", number, CURLFTPSSL_TRY); 970 | LUA_SET_TABLE(L, literal, "FTPSSL_CONTROL", number, CURLFTPSSL_CONTROL); 971 | LUA_SET_TABLE(L, literal, "FTPSSL_ALL", number, CURLFTPSSL_ALL); 972 | 973 | #if CURL_NEWER(7,12,2) 974 | /* enum curl_ftpauth */ 975 | LUA_SET_TABLE(L, literal, "FTPAUTH_DEFAULT", number, CURLFTPAUTH_DEFAULT); 976 | LUA_SET_TABLE(L, literal, "FTPAUTH_SSL", number, CURLFTPAUTH_SSL); 977 | LUA_SET_TABLE(L, literal, "FTPAUTH_TLS", number, CURLFTPAUTH_TLS); 978 | #endif 979 | 980 | /* ip resolve options */ 981 | LUA_SET_TABLE(L, literal, "IPRESOLVE_WHATEVER", number, CURL_IPRESOLVE_WHATEVER); 982 | LUA_SET_TABLE(L, literal, "IPRESOLVE_V4", number, CURL_IPRESOLVE_V4); 983 | LUA_SET_TABLE(L, literal, "IPRESOLVE_V6", number, CURL_IPRESOLVE_V6); 984 | 985 | /* http versions */ 986 | LUA_SET_TABLE(L, literal, "HTTP_VERSION_NONE", number, CURL_HTTP_VERSION_NONE); 987 | LUA_SET_TABLE(L, literal, "HTTP_VERSION_1_0", number, CURL_HTTP_VERSION_1_0); 988 | LUA_SET_TABLE(L, literal, "HTTP_VERSION_1_1", number, CURL_HTTP_VERSION_1_1); 989 | 990 | /* CURL_NETRC_OPTION */ 991 | LUA_SET_TABLE(L, literal, "NETRC_IGNORED", number, CURL_NETRC_IGNORED); 992 | LUA_SET_TABLE(L, literal, "NETRC_OPTIONAL", number, CURL_NETRC_OPTIONAL); 993 | LUA_SET_TABLE(L, literal, "NETRC_REQUIRED", number, CURL_NETRC_REQUIRED); 994 | 995 | /* ssl version */ 996 | LUA_SET_TABLE(L, literal, "SSLVERSION_DEFAULT", number, CURL_SSLVERSION_DEFAULT); 997 | LUA_SET_TABLE(L, literal, "SSLVERSION_TLSv1", number, CURL_SSLVERSION_TLSv1); 998 | LUA_SET_TABLE(L, literal, "SSLVERSION_SSLv2", number, CURL_SSLVERSION_SSLv2); 999 | LUA_SET_TABLE(L, literal, "SSLVERSION_SSLv3", number, CURL_SSLVERSION_SSLv3); 1000 | 1001 | /* CURLOPT_TIMECONDITION */ 1002 | LUA_SET_TABLE(L, literal, "TIMECOND_NONE", number, CURL_TIMECOND_NONE); 1003 | LUA_SET_TABLE(L, literal, "TIMECOND_IFMODSINCE", number, CURL_TIMECOND_IFMODSINCE); 1004 | LUA_SET_TABLE(L, literal, "TIMECOND_IFUNMODSINCE", number, CURL_TIMECOND_IFUNMODSINCE); 1005 | LUA_SET_TABLE(L, literal, "TIMECOND_LASTMOD", number, CURL_TIMECOND_LASTMOD); 1006 | 1007 | /* enum CURLformoption */ 1008 | LUA_SET_TABLE(L, literal, "COPYNAME", number, CURLFORM_COPYNAME); 1009 | LUA_SET_TABLE(L, literal, "PTRNAME", number, CURLFORM_PTRNAME); 1010 | LUA_SET_TABLE(L, literal, "NAMELENGTH", number, CURLFORM_NAMELENGTH); 1011 | LUA_SET_TABLE(L, literal, "COPYCONTENTS", number, CURLFORM_COPYCONTENTS); 1012 | LUA_SET_TABLE(L, literal, "PTRCONTENTS", number, CURLFORM_PTRCONTENTS); 1013 | LUA_SET_TABLE(L, literal, "CONTENTSLENGTH", number, CURLFORM_CONTENTSLENGTH); 1014 | LUA_SET_TABLE(L, literal, "FILECONTENT", number, CURLFORM_FILECONTENT); 1015 | LUA_SET_TABLE(L, literal, "ARRAY", number, CURLFORM_ARRAY); 1016 | LUA_SET_TABLE(L, literal, "OBSOLETE", number, CURLFORM_OBSOLETE); 1017 | LUA_SET_TABLE(L, literal, "FILE", number, CURLFORM_FILE); 1018 | LUA_SET_TABLE(L, literal, "BUFFER", number, CURLFORM_BUFFER); 1019 | LUA_SET_TABLE(L, literal, "BUFFERPTR", number, CURLFORM_BUFFERPTR); 1020 | LUA_SET_TABLE(L, literal, "BUFFERLENGTH", number, CURLFORM_BUFFERLENGTH); 1021 | LUA_SET_TABLE(L, literal, "CONTENTTYPE", number, CURLFORM_CONTENTTYPE); 1022 | LUA_SET_TABLE(L, literal, "CONTENTHEADER", number, CURLFORM_CONTENTHEADER); 1023 | LUA_SET_TABLE(L, literal, "FILENAME", number, CURLFORM_FILENAME); 1024 | LUA_SET_TABLE(L, literal, "END", number, CURLFORM_END); 1025 | LUA_SET_TABLE(L, literal, "OBSOLETE2", number, CURLFORM_OBSOLETE2); 1026 | 1027 | /* enum CURLFORMcode*/ 1028 | LUA_SET_TABLE(L, literal, "CURL_FORMADD_OK", number, CURL_FORMADD_OK); 1029 | LUA_SET_TABLE(L, literal, "CURL_FORMADD_MEMORY", number, CURL_FORMADD_MEMORY); 1030 | LUA_SET_TABLE(L, literal, "CURL_FORMADD_OPTION_TWICE", number, CURL_FORMADD_OPTION_TWICE); 1031 | LUA_SET_TABLE(L, literal, "CURL_FORMADD_NULL", number, CURL_FORMADD_NULL); 1032 | LUA_SET_TABLE(L, literal, "CURL_FORMADD_UNKNOWN_OPTION", number, CURL_FORMADD_UNKNOWN_OPTION); 1033 | LUA_SET_TABLE(L, literal, "CURL_FORMADD_INCOMPLETE", number, CURL_FORMADD_INCOMPLETE); 1034 | LUA_SET_TABLE(L, literal, "CURL_FORMADD_ILLEGAL_ARRAY", number, CURL_FORMADD_ILLEGAL_ARRAY); 1035 | 1036 | #if CURL_NEWER(7,12,1) 1037 | LUA_SET_TABLE(L, literal, "CURL_FORMADD_DISABLED", number, CURL_FORMADD_DISABLED); 1038 | #endif 1039 | 1040 | /* enum curl_closepolicy*/ 1041 | LUA_SET_TABLE(L, literal, "CLOSEPOLICY_OLDEST", number, CURLCLOSEPOLICY_OLDEST); 1042 | LUA_SET_TABLE(L, literal, "CLOSEPOLICY_LEAST_RECENTLY_USED", number, CURLCLOSEPOLICY_LEAST_RECENTLY_USED); 1043 | LUA_SET_TABLE(L, literal, "CLOSEPOLICY_LEAST_TRAFFIC", number, CURLCLOSEPOLICY_LEAST_TRAFFIC); 1044 | LUA_SET_TABLE(L, literal, "CLOSEPOLICY_SLOWEST", number, CURLCLOSEPOLICY_SLOWEST); 1045 | LUA_SET_TABLE(L, literal, "CLOSEPOLICY_CALLBACK", number, CURLCLOSEPOLICY_CALLBACK); 1046 | } 1047 | 1048 | static void setcurlinfo(lua_State* L) 1049 | { 1050 | LUA_SET_TABLE(L, literal, "INFO_NONE", number, CURLINFO_NONE); 1051 | LUA_SET_TABLE(L, literal, "INFO_EFFECTIVE_URL", number, CURLINFO_EFFECTIVE_URL); 1052 | LUA_SET_TABLE(L, literal, "INFO_RESPONSE_CODE", number, CURLINFO_RESPONSE_CODE); 1053 | LUA_SET_TABLE(L, literal, "INFO_TOTAL_TIME", number, CURLINFO_TOTAL_TIME); 1054 | LUA_SET_TABLE(L, literal, "INFO_NAMELOOKUP_TIME", number, CURLINFO_NAMELOOKUP_TIME); 1055 | LUA_SET_TABLE(L, literal, "INFO_CONNECT_TIME", number, CURLINFO_CONNECT_TIME); 1056 | LUA_SET_TABLE(L, literal, "INFO_PRETRANSFER_TIME", number, CURLINFO_PRETRANSFER_TIME); 1057 | LUA_SET_TABLE(L, literal, "INFO_SIZE_UPLOAD", number, CURLINFO_SIZE_UPLOAD); 1058 | LUA_SET_TABLE(L, literal, "INFO_SIZE_DOWNLOAD", number, CURLINFO_SIZE_DOWNLOAD); 1059 | LUA_SET_TABLE(L, literal, "INFO_SPEED_DOWNLOAD", number, CURLINFO_SPEED_DOWNLOAD); 1060 | LUA_SET_TABLE(L, literal, "INFO_SPEED_UPLOAD", number, CURLINFO_SPEED_UPLOAD); 1061 | LUA_SET_TABLE(L, literal, "INFO_HEADER_SIZE", number, CURLINFO_HEADER_SIZE); 1062 | LUA_SET_TABLE(L, literal, "INFO_REQUEST_SIZE", number, CURLINFO_REQUEST_SIZE); 1063 | LUA_SET_TABLE(L, literal, "INFO_SSL_VERIFYRESULT", number, CURLINFO_SSL_VERIFYRESULT); 1064 | LUA_SET_TABLE(L, literal, "INFO_FILETIME", number, CURLINFO_FILETIME); 1065 | LUA_SET_TABLE(L, literal, "INFO_CONTENT_LENGTH_DOWNLOAD", number, CURLINFO_CONTENT_LENGTH_DOWNLOAD); 1066 | LUA_SET_TABLE(L, literal, "INFO_CONTENT_LENGTH_UPLOAD", number, CURLINFO_CONTENT_LENGTH_UPLOAD); 1067 | LUA_SET_TABLE(L, literal, "INFO_STARTTRANSFER_TIME", number, CURLINFO_STARTTRANSFER_TIME); 1068 | LUA_SET_TABLE(L, literal, "INFO_CONTENT_TYPE", number, CURLINFO_CONTENT_TYPE); 1069 | LUA_SET_TABLE(L, literal, "INFO_REDIRECT_TIME", number, CURLINFO_REDIRECT_TIME); 1070 | LUA_SET_TABLE(L, literal, "INFO_REDIRECT_COUNT", number, CURLINFO_REDIRECT_COUNT); 1071 | LUA_SET_TABLE(L, literal, "INFO_PRIVATE", number, CURLINFO_PRIVATE); 1072 | LUA_SET_TABLE(L, literal, "INFO_HTTP_CONNECTCODE", number, CURLINFO_HTTP_CONNECTCODE); 1073 | LUA_SET_TABLE(L, literal, "INFO_HTTPAUTH_AVAIL", number, CURLINFO_HTTPAUTH_AVAIL); 1074 | LUA_SET_TABLE(L, literal, "INFO_PROXYAUTH_AVAIL", number, CURLINFO_PROXYAUTH_AVAIL); 1075 | LUA_SET_TABLE(L, literal, "INFO_OS_ERRNO", number, CURLINFO_OS_ERRNO); 1076 | LUA_SET_TABLE(L, literal, "INFO_NUM_CONNECTS", number, CURLINFO_NUM_CONNECTS); 1077 | LUA_SET_TABLE(L, literal, "INFO_SSL_ENGINES", number, CURLINFO_SSL_ENGINES); 1078 | LUA_SET_TABLE(L, literal, "INFO_COOKIELIST", number, CURLINFO_COOKIELIST); 1079 | #if CURL_NEWER(7,15,2) 1080 | LUA_SET_TABLE(L, literal, "INFO_LASTSOCKET", number, CURLINFO_LASTSOCKET); 1081 | #endif 1082 | } 1083 | 1084 | LUACURL_API int luaopen_luacurl (lua_State *L) 1085 | { 1086 | curl_global_init(CURL_GLOBAL_ALL); /* In windows, this will init the winsock stuff */ 1087 | createmeta(L); 1088 | luaL_openlib (L, 0, luacurl_meths, 0); 1089 | luaL_openlib (L, LUACURL_LIBNAME, luacurl_funcs, 0); 1090 | set_info(L); 1091 | setcurlerrors(L); 1092 | setcurloptions(L); 1093 | setcurlvalues(L); 1094 | setcurlinfo(L); 1095 | return 1; 1096 | } 1097 | 1098 | #ifdef __cplusplus 1099 | } 1100 | #endif 1101 | -------------------------------------------------------------------------------- /luacurl.def: -------------------------------------------------------------------------------- 1 | EXPORTS 2 | luaopen_luacurl 3 | --------------------------------------------------------------------------------