mirror of
https://github.com/libevent/libevent.git
synced 2025-01-09 00:56:20 +08:00
68120d9b00
C reserves all identifiers beginning with an underscore for system use. But we had been mangling our autoconf identifiers with the prefix "_EVENT_" to avoid conflict with other programs. Instead, we will now use the prefix "EVENT__". With any luck, the double-underscore will still hint "here be dragons" to anybody tempted to think that event-config.h is a stable api. This is an automatically generated patch. The script that produced it was made by running this script over config.h.in: ===== #!/usr/bin/perl -w # Run this on config.h.in use strict; my %macros = (); while (<>) { if (/^# *undef +([A-Za-z0-9_]+)/) { $macros{$1} = 1; } } print "#!/usr/bin/perl -w -i -p\n\n"; for my $k (sort keys %macros) { print "s/(?<![A-Za-z0-9_])_EVENT_$k(?![A-Za-z0-9_])/EVENT__$k/g;\n"; } == And the script that it generated was then run over all .c and .h files: #!/usr/bin/perl -w -i -p s/(?<![A-Za-z0-9_])_EVENT_DISABLE_DEBUG_MODE(?![A-Za-z0-9_])/EVENT__DISABLE_DEBUG_MODE/g; s/(?<![A-Za-z0-9_])_EVENT_DISABLE_MM_REPLACEMENT(?![A-Za-z0-9_])/EVENT__DISABLE_MM_REPLACEMENT/g; s/(?<![A-Za-z0-9_])_EVENT_DISABLE_THREAD_SUPPORT(?![A-Za-z0-9_])/EVENT__DISABLE_THREAD_SUPPORT/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_ACCEPT4(?![A-Za-z0-9_])/EVENT__HAVE_ACCEPT4/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_ARC4RANDOM(?![A-Za-z0-9_])/EVENT__HAVE_ARC4RANDOM/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_ARC4RANDOM_BUF(?![A-Za-z0-9_])/EVENT__HAVE_ARC4RANDOM_BUF/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_ARPA_INET_H(?![A-Za-z0-9_])/EVENT__HAVE_ARPA_INET_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_CLOCK_GETTIME(?![A-Za-z0-9_])/EVENT__HAVE_CLOCK_GETTIME/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_DECL_CTL_KERN(?![A-Za-z0-9_])/EVENT__HAVE_DECL_CTL_KERN/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_DECL_KERN_ARND(?![A-Za-z0-9_])/EVENT__HAVE_DECL_KERN_ARND/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_DECL_KERN_RANDOM(?![A-Za-z0-9_])/EVENT__HAVE_DECL_KERN_RANDOM/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_DECL_RANDOM_UUID(?![A-Za-z0-9_])/EVENT__HAVE_DECL_RANDOM_UUID/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_DEVPOLL(?![A-Za-z0-9_])/EVENT__HAVE_DEVPOLL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_DLFCN_H(?![A-Za-z0-9_])/EVENT__HAVE_DLFCN_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_EPOLL(?![A-Za-z0-9_])/EVENT__HAVE_EPOLL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_EPOLL_CREATE1(?![A-Za-z0-9_])/EVENT__HAVE_EPOLL_CREATE1/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_EPOLL_CTL(?![A-Za-z0-9_])/EVENT__HAVE_EPOLL_CTL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_EVENTFD(?![A-Za-z0-9_])/EVENT__HAVE_EVENTFD/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_EVENT_PORTS(?![A-Za-z0-9_])/EVENT__HAVE_EVENT_PORTS/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_FCNTL(?![A-Za-z0-9_])/EVENT__HAVE_FCNTL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_FCNTL_H(?![A-Za-z0-9_])/EVENT__HAVE_FCNTL_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_FD_MASK(?![A-Za-z0-9_])/EVENT__HAVE_FD_MASK/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETADDRINFO(?![A-Za-z0-9_])/EVENT__HAVE_GETADDRINFO/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETEGID(?![A-Za-z0-9_])/EVENT__HAVE_GETEGID/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETEUID(?![A-Za-z0-9_])/EVENT__HAVE_GETEUID/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETHOSTBYNAME_R(?![A-Za-z0-9_])/EVENT__HAVE_GETHOSTBYNAME_R/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETHOSTBYNAME_R_3_ARG(?![A-Za-z0-9_])/EVENT__HAVE_GETHOSTBYNAME_R_3_ARG/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETHOSTBYNAME_R_5_ARG(?![A-Za-z0-9_])/EVENT__HAVE_GETHOSTBYNAME_R_5_ARG/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETHOSTBYNAME_R_6_ARG(?![A-Za-z0-9_])/EVENT__HAVE_GETHOSTBYNAME_R_6_ARG/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETIFADDRS(?![A-Za-z0-9_])/EVENT__HAVE_GETIFADDRS/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETNAMEINFO(?![A-Za-z0-9_])/EVENT__HAVE_GETNAMEINFO/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETPROTOBYNUMBER(?![A-Za-z0-9_])/EVENT__HAVE_GETPROTOBYNUMBER/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETSERVBYNAME(?![A-Za-z0-9_])/EVENT__HAVE_GETSERVBYNAME/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETTIMEOFDAY(?![A-Za-z0-9_])/EVENT__HAVE_GETTIMEOFDAY/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_IFADDRS_H(?![A-Za-z0-9_])/EVENT__HAVE_IFADDRS_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_INET_ATON(?![A-Za-z0-9_])/EVENT__HAVE_INET_ATON/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_INET_NTOP(?![A-Za-z0-9_])/EVENT__HAVE_INET_NTOP/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_INET_PTON(?![A-Za-z0-9_])/EVENT__HAVE_INET_PTON/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_INTTYPES_H(?![A-Za-z0-9_])/EVENT__HAVE_INTTYPES_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_ISSETUGID(?![A-Za-z0-9_])/EVENT__HAVE_ISSETUGID/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_KQUEUE(?![A-Za-z0-9_])/EVENT__HAVE_KQUEUE/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_LIBZ(?![A-Za-z0-9_])/EVENT__HAVE_LIBZ/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_MEMORY_H(?![A-Za-z0-9_])/EVENT__HAVE_MEMORY_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_MMAP(?![A-Za-z0-9_])/EVENT__HAVE_MMAP/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_NANOSLEEP(?![A-Za-z0-9_])/EVENT__HAVE_NANOSLEEP/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_NETDB_H(?![A-Za-z0-9_])/EVENT__HAVE_NETDB_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_NETINET_IN6_H(?![A-Za-z0-9_])/EVENT__HAVE_NETINET_IN6_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_NETINET_IN_H(?![A-Za-z0-9_])/EVENT__HAVE_NETINET_IN_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_NETINET_TCP_H(?![A-Za-z0-9_])/EVENT__HAVE_NETINET_TCP_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_OPENSSL(?![A-Za-z0-9_])/EVENT__HAVE_OPENSSL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_PIPE(?![A-Za-z0-9_])/EVENT__HAVE_PIPE/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_PIPE2(?![A-Za-z0-9_])/EVENT__HAVE_PIPE2/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_POLL(?![A-Za-z0-9_])/EVENT__HAVE_POLL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_POLL_H(?![A-Za-z0-9_])/EVENT__HAVE_POLL_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_PORT_CREATE(?![A-Za-z0-9_])/EVENT__HAVE_PORT_CREATE/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_PORT_H(?![A-Za-z0-9_])/EVENT__HAVE_PORT_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_PTHREAD(?![A-Za-z0-9_])/EVENT__HAVE_PTHREAD/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_PTHREADS(?![A-Za-z0-9_])/EVENT__HAVE_PTHREADS/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_PUTENV(?![A-Za-z0-9_])/EVENT__HAVE_PUTENV/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SA_FAMILY_T(?![A-Za-z0-9_])/EVENT__HAVE_SA_FAMILY_T/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SELECT(?![A-Za-z0-9_])/EVENT__HAVE_SELECT/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SENDFILE(?![A-Za-z0-9_])/EVENT__HAVE_SENDFILE/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SETENV(?![A-Za-z0-9_])/EVENT__HAVE_SETENV/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SETFD(?![A-Za-z0-9_])/EVENT__HAVE_SETFD/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SETRLIMIT(?![A-Za-z0-9_])/EVENT__HAVE_SETRLIMIT/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SIGACTION(?![A-Za-z0-9_])/EVENT__HAVE_SIGACTION/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SIGNAL(?![A-Za-z0-9_])/EVENT__HAVE_SIGNAL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SPLICE(?![A-Za-z0-9_])/EVENT__HAVE_SPLICE/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STDARG_H(?![A-Za-z0-9_])/EVENT__HAVE_STDARG_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STDDEF_H(?![A-Za-z0-9_])/EVENT__HAVE_STDDEF_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STDINT_H(?![A-Za-z0-9_])/EVENT__HAVE_STDINT_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STDLIB_H(?![A-Za-z0-9_])/EVENT__HAVE_STDLIB_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRINGS_H(?![A-Za-z0-9_])/EVENT__HAVE_STRINGS_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRING_H(?![A-Za-z0-9_])/EVENT__HAVE_STRING_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRLCPY(?![A-Za-z0-9_])/EVENT__HAVE_STRLCPY/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRSEP(?![A-Za-z0-9_])/EVENT__HAVE_STRSEP/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRTOK_R(?![A-Za-z0-9_])/EVENT__HAVE_STRTOK_R/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRTOLL(?![A-Za-z0-9_])/EVENT__HAVE_STRTOLL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_ADDRINFO(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_ADDRINFO/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_IN6_ADDR(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_IN6_ADDR/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_IN6_ADDR_S6_ADDR16(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_IN6_ADDR_S6_ADDR16/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_IN6_ADDR_S6_ADDR32(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_IN6_ADDR_S6_ADDR32/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_SOCKADDR_IN6(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_SOCKADDR_IN6/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_SOCKADDR_IN_SIN_LEN(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_SOCKADDR_STORAGE(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_SOCKADDR_STORAGE/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYSCTL(?![A-Za-z0-9_])/EVENT__HAVE_SYSCTL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_DEVPOLL_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_DEVPOLL_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_EPOLL_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_EPOLL_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_EVENTFD_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_EVENTFD_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_EVENT_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_EVENT_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_IOCTL_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_IOCTL_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_MMAN_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_MMAN_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_PARAM_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_PARAM_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_QUEUE_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_QUEUE_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_RESOURCE_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_RESOURCE_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_SELECT_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_SELECT_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_SENDFILE_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_SENDFILE_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_SOCKET_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_SOCKET_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_STAT_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_STAT_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_SYSCTL_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_SYSCTL_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_TIME_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_TIME_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_TYPES_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_TYPES_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_UIO_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_UIO_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_WAIT_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_WAIT_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_TAILQFOREACH(?![A-Za-z0-9_])/EVENT__HAVE_TAILQFOREACH/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_TIMERADD(?![A-Za-z0-9_])/EVENT__HAVE_TIMERADD/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_TIMERCLEAR(?![A-Za-z0-9_])/EVENT__HAVE_TIMERCLEAR/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_TIMERCMP(?![A-Za-z0-9_])/EVENT__HAVE_TIMERCMP/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_TIMERISSET(?![A-Za-z0-9_])/EVENT__HAVE_TIMERISSET/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_UINT16_T(?![A-Za-z0-9_])/EVENT__HAVE_UINT16_T/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_UINT32_T(?![A-Za-z0-9_])/EVENT__HAVE_UINT32_T/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_UINT64_T(?![A-Za-z0-9_])/EVENT__HAVE_UINT64_T/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_UINT8_T(?![A-Za-z0-9_])/EVENT__HAVE_UINT8_T/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_UINTPTR_T(?![A-Za-z0-9_])/EVENT__HAVE_UINTPTR_T/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_UNISTD_H(?![A-Za-z0-9_])/EVENT__HAVE_UNISTD_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_UNSETENV(?![A-Za-z0-9_])/EVENT__HAVE_UNSETENV/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_USLEEP(?![A-Za-z0-9_])/EVENT__HAVE_USLEEP/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_VASPRINTF(?![A-Za-z0-9_])/EVENT__HAVE_VASPRINTF/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_WORKING_KQUEUE(?![A-Za-z0-9_])/EVENT__HAVE_WORKING_KQUEUE/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_ZLIB_H(?![A-Za-z0-9_])/EVENT__HAVE_ZLIB_H/g; s/(?<![A-Za-z0-9_])_EVENT_LT_OBJDIR(?![A-Za-z0-9_])/EVENT__LT_OBJDIR/g; s/(?<![A-Za-z0-9_])_EVENT_NO_MINUS_C_MINUS_O(?![A-Za-z0-9_])/EVENT__NO_MINUS_C_MINUS_O/g; s/(?<![A-Za-z0-9_])_EVENT_NUMERIC_VERSION(?![A-Za-z0-9_])/EVENT__NUMERIC_VERSION/g; s/(?<![A-Za-z0-9_])_EVENT_PACKAGE(?![A-Za-z0-9_])/EVENT__PACKAGE/g; s/(?<![A-Za-z0-9_])_EVENT_PACKAGE_BUGREPORT(?![A-Za-z0-9_])/EVENT__PACKAGE_BUGREPORT/g; s/(?<![A-Za-z0-9_])_EVENT_PACKAGE_NAME(?![A-Za-z0-9_])/EVENT__PACKAGE_NAME/g; s/(?<![A-Za-z0-9_])_EVENT_PACKAGE_STRING(?![A-Za-z0-9_])/EVENT__PACKAGE_STRING/g; s/(?<![A-Za-z0-9_])_EVENT_PACKAGE_TARNAME(?![A-Za-z0-9_])/EVENT__PACKAGE_TARNAME/g; s/(?<![A-Za-z0-9_])_EVENT_PACKAGE_URL(?![A-Za-z0-9_])/EVENT__PACKAGE_URL/g; s/(?<![A-Za-z0-9_])_EVENT_PACKAGE_VERSION(?![A-Za-z0-9_])/EVENT__PACKAGE_VERSION/g; s/(?<![A-Za-z0-9_])_EVENT_PTHREAD_CREATE_JOINABLE(?![A-Za-z0-9_])/EVENT__PTHREAD_CREATE_JOINABLE/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_INT(?![A-Za-z0-9_])/EVENT__SIZEOF_INT/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_LONG(?![A-Za-z0-9_])/EVENT__SIZEOF_LONG/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_LONG_LONG(?![A-Za-z0-9_])/EVENT__SIZEOF_LONG_LONG/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_OFF_T(?![A-Za-z0-9_])/EVENT__SIZEOF_OFF_T/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_PTHREAD_T(?![A-Za-z0-9_])/EVENT__SIZEOF_PTHREAD_T/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_SHORT(?![A-Za-z0-9_])/EVENT__SIZEOF_SHORT/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_SIZE_T(?![A-Za-z0-9_])/EVENT__SIZEOF_SIZE_T/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_VOID_P(?![A-Za-z0-9_])/EVENT__SIZEOF_VOID_P/g; s/(?<![A-Za-z0-9_])_EVENT_STDC_HEADERS(?![A-Za-z0-9_])/EVENT__STDC_HEADERS/g; s/(?<![A-Za-z0-9_])_EVENT_TIME_WITH_SYS_TIME(?![A-Za-z0-9_])/EVENT__TIME_WITH_SYS_TIME/g; s/(?<![A-Za-z0-9_])_EVENT_VERSION(?![A-Za-z0-9_])/EVENT__VERSION/g; s/(?<![A-Za-z0-9_])_EVENT__ALL_SOURCE(?![A-Za-z0-9_])/EVENT___ALL_SOURCE/g; s/(?<![A-Za-z0-9_])_EVENT__FILE_OFFSET_BITS(?![A-Za-z0-9_])/EVENT___FILE_OFFSET_BITS/g; s/(?<![A-Za-z0-9_])_EVENT__GNU_SOURCE(?![A-Za-z0-9_])/EVENT___GNU_SOURCE/g; s/(?<![A-Za-z0-9_])_EVENT__LARGE_FILES(?![A-Za-z0-9_])/EVENT___LARGE_FILES/g; s/(?<![A-Za-z0-9_])_EVENT__MINIX(?![A-Za-z0-9_])/EVENT___MINIX/g; s/(?<![A-Za-z0-9_])_EVENT__POSIX_1_SOURCE(?![A-Za-z0-9_])/EVENT___POSIX_1_SOURCE/g; s/(?<![A-Za-z0-9_])_EVENT__POSIX_PTHREAD_SEMANTICS(?![A-Za-z0-9_])/EVENT___POSIX_PTHREAD_SEMANTICS/g; s/(?<![A-Za-z0-9_])_EVENT__POSIX_SOURCE(?![A-Za-z0-9_])/EVENT___POSIX_SOURCE/g; s/(?<![A-Za-z0-9_])_EVENT__TANDEM_SOURCE(?![A-Za-z0-9_])/EVENT___TANDEM_SOURCE/g; s/(?<![A-Za-z0-9_])_EVENT___EXTENSIONS__(?![A-Za-z0-9_])/EVENT____EXTENSIONS__/g; s/(?<![A-Za-z0-9_])_EVENT___func__(?![A-Za-z0-9_])/EVENT____func__/g; s/(?<![A-Za-z0-9_])_EVENT_const(?![A-Za-z0-9_])/EVENT__const/g; s/(?<![A-Za-z0-9_])_EVENT_inline(?![A-Za-z0-9_])/EVENT__inline/g; s/(?<![A-Za-z0-9_])_EVENT_pid_t(?![A-Za-z0-9_])/EVENT__pid_t/g; s/(?<![A-Za-z0-9_])_EVENT_size_t(?![A-Za-z0-9_])/EVENT__size_t/g; s/(?<![A-Za-z0-9_])_EVENT_socklen_t(?![A-Za-z0-9_])/EVENT__socklen_t/g; s/(?<![A-Za-z0-9_])_EVENT_ssize_t(?![A-Za-z0-9_])/EVENT__ssize_t/g;
406 lines
9.9 KiB
C
406 lines
9.9 KiB
C
/*
|
|
A trivial static http webserver using Libevent's evhttp.
|
|
|
|
This is not the best code in the world, and it does some fairly stupid stuff
|
|
that you would never want to do in a production webserver. Caveat hackor!
|
|
|
|
*/
|
|
|
|
/* Compatibility for possible missing IPv6 declarations */
|
|
#include "../util-internal.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
|
|
#ifdef _WIN32
|
|
#include <winsock2.h>
|
|
#include <ws2tcpip.h>
|
|
#include <windows.h>
|
|
#include <io.h>
|
|
#include <fcntl.h>
|
|
#ifndef S_ISDIR
|
|
#define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
|
|
#endif
|
|
#else
|
|
#include <sys/stat.h>
|
|
#include <sys/socket.h>
|
|
#include <signal.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <dirent.h>
|
|
#endif
|
|
|
|
#include <event2/event.h>
|
|
#include <event2/http.h>
|
|
#include <event2/buffer.h>
|
|
#include <event2/util.h>
|
|
#include <event2/keyvalq_struct.h>
|
|
|
|
#ifdef EVENT__HAVE_NETINET_IN_H
|
|
#include <netinet/in.h>
|
|
# ifdef _XOPEN_SOURCE_EXTENDED
|
|
# include <arpa/inet.h>
|
|
# endif
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
|
#define stat _stat
|
|
#define fstat _fstat
|
|
#define open _open
|
|
#define close _close
|
|
#define O_RDONLY _O_RDONLY
|
|
#endif
|
|
|
|
char uri_root[512];
|
|
|
|
static const struct table_entry {
|
|
const char *extension;
|
|
const char *content_type;
|
|
} content_type_table[] = {
|
|
{ "txt", "text/plain" },
|
|
{ "c", "text/plain" },
|
|
{ "h", "text/plain" },
|
|
{ "html", "text/html" },
|
|
{ "htm", "text/htm" },
|
|
{ "css", "text/css" },
|
|
{ "gif", "image/gif" },
|
|
{ "jpg", "image/jpeg" },
|
|
{ "jpeg", "image/jpeg" },
|
|
{ "png", "image/png" },
|
|
{ "pdf", "application/pdf" },
|
|
{ "ps", "application/postsript" },
|
|
{ NULL, NULL },
|
|
};
|
|
|
|
/* Try to guess a good content-type for 'path' */
|
|
static const char *
|
|
guess_content_type(const char *path)
|
|
{
|
|
const char *last_period, *extension;
|
|
const struct table_entry *ent;
|
|
last_period = strrchr(path, '.');
|
|
if (!last_period || strchr(last_period, '/'))
|
|
goto not_found; /* no exension */
|
|
extension = last_period + 1;
|
|
for (ent = &content_type_table[0]; ent->extension; ++ent) {
|
|
if (!evutil_ascii_strcasecmp(ent->extension, extension))
|
|
return ent->content_type;
|
|
}
|
|
|
|
not_found:
|
|
return "application/misc";
|
|
}
|
|
|
|
/* Callback used for the /dump URI, and for every non-GET request:
|
|
* dumps all information to stdout and gives back a trivial 200 ok */
|
|
static void
|
|
dump_request_cb(struct evhttp_request *req, void *arg)
|
|
{
|
|
const char *cmdtype;
|
|
struct evkeyvalq *headers;
|
|
struct evkeyval *header;
|
|
struct evbuffer *buf;
|
|
|
|
switch (evhttp_request_get_command(req)) {
|
|
case EVHTTP_REQ_GET: cmdtype = "GET"; break;
|
|
case EVHTTP_REQ_POST: cmdtype = "POST"; break;
|
|
case EVHTTP_REQ_HEAD: cmdtype = "HEAD"; break;
|
|
case EVHTTP_REQ_PUT: cmdtype = "PUT"; break;
|
|
case EVHTTP_REQ_DELETE: cmdtype = "DELETE"; break;
|
|
case EVHTTP_REQ_OPTIONS: cmdtype = "OPTIONS"; break;
|
|
case EVHTTP_REQ_TRACE: cmdtype = "TRACE"; break;
|
|
case EVHTTP_REQ_CONNECT: cmdtype = "CONNECT"; break;
|
|
case EVHTTP_REQ_PATCH: cmdtype = "PATCH"; break;
|
|
default: cmdtype = "unknown"; break;
|
|
}
|
|
|
|
printf("Received a %s request for %s\nHeaders:\n",
|
|
cmdtype, evhttp_request_get_uri(req));
|
|
|
|
headers = evhttp_request_get_input_headers(req);
|
|
for (header = headers->tqh_first; header;
|
|
header = header->next.tqe_next) {
|
|
printf(" %s: %s\n", header->key, header->value);
|
|
}
|
|
|
|
buf = evhttp_request_get_input_buffer(req);
|
|
puts("Input data: <<<");
|
|
while (evbuffer_get_length(buf)) {
|
|
int n;
|
|
char cbuf[128];
|
|
n = evbuffer_remove(buf, cbuf, sizeof(buf)-1);
|
|
if (n > 0)
|
|
(void) fwrite(cbuf, 1, n, stdout);
|
|
}
|
|
puts(">>>");
|
|
|
|
evhttp_send_reply(req, 200, "OK", NULL);
|
|
}
|
|
|
|
/* This callback gets invoked when we get any http request that doesn't match
|
|
* any other callback. Like any evhttp server callback, it has a simple job:
|
|
* it must eventually call evhttp_send_error() or evhttp_send_reply().
|
|
*/
|
|
static void
|
|
send_document_cb(struct evhttp_request *req, void *arg)
|
|
{
|
|
struct evbuffer *evb = NULL;
|
|
const char *docroot = arg;
|
|
const char *uri = evhttp_request_get_uri(req);
|
|
struct evhttp_uri *decoded = NULL;
|
|
const char *path;
|
|
char *decoded_path;
|
|
char *whole_path = NULL;
|
|
size_t len;
|
|
int fd = -1;
|
|
struct stat st;
|
|
|
|
if (evhttp_request_get_command(req) != EVHTTP_REQ_GET) {
|
|
dump_request_cb(req, arg);
|
|
return;
|
|
}
|
|
|
|
printf("Got a GET request for <%s>\n", uri);
|
|
|
|
/* Decode the URI */
|
|
decoded = evhttp_uri_parse(uri);
|
|
if (!decoded) {
|
|
printf("It's not a good URI. Sending BADREQUEST\n");
|
|
evhttp_send_error(req, HTTP_BADREQUEST, 0);
|
|
return;
|
|
}
|
|
|
|
/* Let's see what path the user asked for. */
|
|
path = evhttp_uri_get_path(decoded);
|
|
if (!path) path = "/";
|
|
|
|
/* We need to decode it, to see what path the user really wanted. */
|
|
decoded_path = evhttp_uridecode(path, 0, NULL);
|
|
if (decoded_path == NULL)
|
|
goto err;
|
|
/* Don't allow any ".."s in the path, to avoid exposing stuff outside
|
|
* of the docroot. This test is both overzealous and underzealous:
|
|
* it forbids aceptable paths like "/this/one..here", but it doesn't
|
|
* do anything to prevent symlink following." */
|
|
if (strstr(decoded_path, ".."))
|
|
goto err;
|
|
|
|
len = strlen(decoded_path)+strlen(docroot)+2;
|
|
if (!(whole_path = malloc(len))) {
|
|
perror("malloc");
|
|
goto err;
|
|
}
|
|
evutil_snprintf(whole_path, len, "%s/%s", docroot, decoded_path);
|
|
|
|
if (stat(whole_path, &st)<0) {
|
|
goto err;
|
|
}
|
|
|
|
/* This holds the content we're sending. */
|
|
evb = evbuffer_new();
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
/* If it's a directory, read the comments and make a little
|
|
* index page */
|
|
#ifdef _WIN32
|
|
HANDLE d;
|
|
WIN32_FIND_DATAA ent;
|
|
char *pattern;
|
|
size_t dirlen;
|
|
#else
|
|
DIR *d;
|
|
struct dirent *ent;
|
|
#endif
|
|
const char *trailing_slash = "";
|
|
|
|
if (!strlen(path) || path[strlen(path)-1] != '/')
|
|
trailing_slash = "/";
|
|
|
|
#ifdef _WIN32
|
|
dirlen = strlen(whole_path);
|
|
pattern = malloc(dirlen+3);
|
|
memcpy(pattern, whole_path, dirlen);
|
|
pattern[dirlen] = '\\';
|
|
pattern[dirlen+1] = '*';
|
|
pattern[dirlen+2] = '\0';
|
|
d = FindFirstFileA(pattern, &ent);
|
|
free(pattern);
|
|
if (d == INVALID_HANDLE_VALUE)
|
|
goto err;
|
|
#else
|
|
if (!(d = opendir(whole_path)))
|
|
goto err;
|
|
#endif
|
|
|
|
evbuffer_add_printf(evb, "<html>\n <head>\n"
|
|
" <title>%s</title>\n"
|
|
" <base href='%s%s%s'>\n"
|
|
" </head>\n"
|
|
" <body>\n"
|
|
" <h1>%s</h1>\n"
|
|
" <ul>\n",
|
|
decoded_path, /* XXX html-escape this. */
|
|
uri_root, path, /* XXX html-escape this? */
|
|
trailing_slash,
|
|
decoded_path /* XXX html-escape this */);
|
|
#ifdef _WIN32
|
|
do {
|
|
const char *name = ent.cFileName;
|
|
#else
|
|
while ((ent = readdir(d))) {
|
|
const char *name = ent->d_name;
|
|
#endif
|
|
evbuffer_add_printf(evb,
|
|
" <li><a href=\"%s\">%s</a>\n",
|
|
name, name);/* XXX escape this */
|
|
#ifdef _WIN32
|
|
} while (FindNextFileA(d, &ent));
|
|
#else
|
|
}
|
|
#endif
|
|
evbuffer_add_printf(evb, "</ul></body></html>\n");
|
|
#ifdef _WIN32
|
|
CloseHandle(d);
|
|
#else
|
|
closedir(d);
|
|
#endif
|
|
evhttp_add_header(evhttp_request_get_output_headers(req),
|
|
"Content-Type", "text/html");
|
|
} else {
|
|
/* Otherwise it's a file; add it to the buffer to get
|
|
* sent via sendfile */
|
|
const char *type = guess_content_type(decoded_path);
|
|
if ((fd = open(whole_path, O_RDONLY)) < 0) {
|
|
perror("open");
|
|
goto err;
|
|
}
|
|
|
|
if (fstat(fd, &st)<0) {
|
|
/* Make sure the length still matches, now that we
|
|
* opened the file :/ */
|
|
perror("fstat");
|
|
goto err;
|
|
}
|
|
evhttp_add_header(evhttp_request_get_output_headers(req),
|
|
"Content-Type", type);
|
|
evbuffer_add_file(evb, fd, 0, st.st_size);
|
|
}
|
|
|
|
evhttp_send_reply(req, 200, "OK", evb);
|
|
goto done;
|
|
err:
|
|
evhttp_send_error(req, 404, "Document was not found");
|
|
if (fd>=0)
|
|
close(fd);
|
|
done:
|
|
if (decoded)
|
|
evhttp_uri_free(decoded);
|
|
if (decoded_path)
|
|
free(decoded_path);
|
|
if (whole_path)
|
|
free(whole_path);
|
|
if (evb)
|
|
evbuffer_free(evb);
|
|
}
|
|
|
|
static void
|
|
syntax(void)
|
|
{
|
|
fprintf(stdout, "Syntax: http-server <docroot>\n");
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
struct event_base *base;
|
|
struct evhttp *http;
|
|
struct evhttp_bound_socket *handle;
|
|
|
|
unsigned short port = 0;
|
|
#ifdef _WIN32
|
|
WSADATA WSAData;
|
|
WSAStartup(0x101, &WSAData);
|
|
#else
|
|
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
|
|
return (1);
|
|
#endif
|
|
if (argc < 2) {
|
|
syntax();
|
|
return 1;
|
|
}
|
|
|
|
base = event_base_new();
|
|
if (!base) {
|
|
fprintf(stderr, "Couldn't create an event_base: exiting\n");
|
|
return 1;
|
|
}
|
|
|
|
/* Create a new evhttp object to handle requests. */
|
|
http = evhttp_new(base);
|
|
if (!http) {
|
|
fprintf(stderr, "couldn't create evhttp. Exiting.\n");
|
|
return 1;
|
|
}
|
|
|
|
/* The /dump URI will dump all requests to stdout and say 200 ok. */
|
|
evhttp_set_cb(http, "/dump", dump_request_cb, NULL);
|
|
|
|
/* We want to accept arbitrary requests, so we need to set a "generic"
|
|
* cb. We can also add callbacks for specific paths. */
|
|
evhttp_set_gencb(http, send_document_cb, argv[1]);
|
|
|
|
/* Now we tell the evhttp what port to listen on */
|
|
handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", port);
|
|
if (!handle) {
|
|
fprintf(stderr, "couldn't bind to port %d. Exiting.\n",
|
|
(int)port);
|
|
return 1;
|
|
}
|
|
|
|
{
|
|
/* Extract and display the address we're listening on. */
|
|
struct sockaddr_storage ss;
|
|
evutil_socket_t fd;
|
|
ev_socklen_t socklen = sizeof(ss);
|
|
char addrbuf[128];
|
|
void *inaddr;
|
|
const char *addr;
|
|
int got_port = -1;
|
|
fd = evhttp_bound_socket_get_fd(handle);
|
|
memset(&ss, 0, sizeof(ss));
|
|
if (getsockname(fd, (struct sockaddr *)&ss, &socklen)) {
|
|
perror("getsockname() failed");
|
|
return 1;
|
|
}
|
|
if (ss.ss_family == AF_INET) {
|
|
got_port = ntohs(((struct sockaddr_in*)&ss)->sin_port);
|
|
inaddr = &((struct sockaddr_in*)&ss)->sin_addr;
|
|
} else if (ss.ss_family == AF_INET6) {
|
|
got_port = ntohs(((struct sockaddr_in6*)&ss)->sin6_port);
|
|
inaddr = &((struct sockaddr_in6*)&ss)->sin6_addr;
|
|
} else {
|
|
fprintf(stderr, "Weird address family %d\n",
|
|
ss.ss_family);
|
|
return 1;
|
|
}
|
|
addr = evutil_inet_ntop(ss.ss_family, inaddr, addrbuf,
|
|
sizeof(addrbuf));
|
|
if (addr) {
|
|
printf("Listening on %s:%d\n", addr, got_port);
|
|
evutil_snprintf(uri_root, sizeof(uri_root),
|
|
"http://%s:%d",addr,got_port);
|
|
} else {
|
|
fprintf(stderr, "evutil_inet_ntop failed\n");
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
event_base_dispatch(base);
|
|
|
|
return 0;
|
|
}
|