summaryrefslogtreecommitdiffabout
authorMichael Krelin <hacker@klever.net>2008-06-29 16:08:01 (UTC)
committer Michael Krelin <hacker@klever.net>2008-06-29 16:11:44 (UTC)
commit12837594b705ad10fdadfd0ba1bfc2249b3b1264 (patch) (unidiff)
treee0502e678b09668ec59828237ebf5972014d0ce0
parent362678728b8232c9490e14ba14ff323d9a92d6be (diff)
downloadlibopkele-12837594b705ad10fdadfd0ba1bfc2249b3b1264.zip
libopkele-12837594b705ad10fdadfd0ba1bfc2249b3b1264.tar.gz
libopkele-12837594b705ad10fdadfd0ba1bfc2249b3b1264.tar.bz2
Fixed w3c to unix timestamp conversion for FreeBSD
Thanks to Göran Löwkrantz for pointing both to the problem and possible solution. Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--configure.ac1
-rw-r--r--lib/util.cc21
2 files changed, 19 insertions, 3 deletions
diff --git a/configure.ac b/configure.ac
index 3194718..3484146 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,258 +1,259 @@
1AC_INIT([libopkele], [2.0], [libopkele-bugs@klever.net]) 1AC_INIT([libopkele], [2.0], [libopkele-bugs@klever.net])
2AC_CONFIG_SRCDIR([include/opkele/opkele-config.h]) 2AC_CONFIG_SRCDIR([include/opkele/opkele-config.h])
3AC_CONFIG_HEADERS([config.h include/opkele/acconfig.h]) 3AC_CONFIG_HEADERS([config.h include/opkele/acconfig.h])
4AM_INIT_AUTOMAKE([dist-bzip2]) 4AM_INIT_AUTOMAKE([dist-bzip2])
5 5
6AC_PROG_INSTALL 6AC_PROG_INSTALL
7AC_PROG_CXX 7AC_PROG_CXX
8AC_PROG_CC 8AC_PROG_CC
9AC_PROG_LIBTOOL 9AC_PROG_LIBTOOL
10PKG_PROG_PKG_CONFIG 10PKG_PROG_PKG_CONFIG
11 11
12AC_HEADER_STDC 12AC_HEADER_STDC
13AC_CHECK_FUNCS([timegm])
13 14
14AC_PATH_PROG([XSLTPROC],[xsltproc],[true]) 15AC_PATH_PROG([XSLTPROC],[xsltproc],[true])
15 16
16AC_MSG_CHECKING([for source tree version]) 17AC_MSG_CHECKING([for source tree version])
17if headrev=$(cd $srcdir && git rev-parse --verify HEAD 2>/dev/null) ; then 18if headrev=$(cd $srcdir && git rev-parse --verify HEAD 2>/dev/null) ; then
18 PACKAGE_SRC_VERSION="$(cd $srcdir && git describe --tags $headrev)" 19 PACKAGE_SRC_VERSION="$(cd $srcdir && git describe --tags $headrev)"
19 test "$PACKAGE_SRC_VERSION" = "$PACKAGE_VERSION" \ 20 test "$PACKAGE_SRC_VERSION" = "$PACKAGE_VERSION" \
20 -o "${PACKAGE_SRC_VERSION#${PACKAGE_VERSION}-}" != "$PACKAGE_SRC_VERSION" || PACKAGE_SRC_VERSION="${PACKAGE_VERSION}:${PACKAGE_SRC_VERSION}" 21 -o "${PACKAGE_SRC_VERSION#${PACKAGE_VERSION}-}" != "$PACKAGE_SRC_VERSION" || PACKAGE_SRC_VERSION="${PACKAGE_VERSION}:${PACKAGE_SRC_VERSION}"
21 ( cd $srcdir && git diff-index $headrev | read dirt ) && PACKAGE_SRC_VERSION="${PACKAGE_SRC_VERSION}-dirty" 22 ( cd $srcdir && git diff-index $headrev | read dirt ) && PACKAGE_SRC_VERSION="${PACKAGE_SRC_VERSION}-dirty"
22else 23else
23 PACKAGE_SRC_VERSION="$PACKAGE_VERSION" 24 PACKAGE_SRC_VERSION="$PACKAGE_VERSION"
24fi 25fi
25AC_MSG_RESULT([$PACKAGE_SRC_VERSION]) 26AC_MSG_RESULT([$PACKAGE_SRC_VERSION])
26AC_SUBST([PACKAGE_SRC_VERSION]) 27AC_SUBST([PACKAGE_SRC_VERSION])
27AC_DEFINE_UNQUOTED([PACKAGE_SRC_VERSION],["$PACKAGE_SRC_VERSION"],[more or less precise source tree version]) 28AC_DEFINE_UNQUOTED([PACKAGE_SRC_VERSION],["$PACKAGE_SRC_VERSION"],[more or less precise source tree version])
28 29
29tr1_mem_std="false" 30tr1_mem_std="false"
30tr1_mem_boost="false" 31tr1_mem_boost="false"
31AC_CHECK_SHAREDPTR(std::tr1,tr1/memory,[ tr1_mem_std=true ]) 32AC_CHECK_SHAREDPTR(std::tr1,tr1/memory,[ tr1_mem_std=true ])
32AC_CHECK_SHAREDPTR(boost,boost/shared_ptr.hpp,[ tr1_mem_boost=true ]) 33AC_CHECK_SHAREDPTR(boost,boost/shared_ptr.hpp,[ tr1_mem_boost=true ])
33tr1_mem="" 34tr1_mem=""
34AC_ARG_WITH([tr1-memory], 35AC_ARG_WITH([tr1-memory],
35 AC_HELP_STRING([--with-tr1-memory=<boost|std>],[select tr1/memory (shared_ptr<>) implementation to use]), 36 AC_HELP_STRING([--with-tr1-memory=<boost|std>],[select tr1/memory (shared_ptr<>) implementation to use]),
36 [ tr1_mem="$withval" ] 37 [ tr1_mem="$withval" ]
37) 38)
38AC_MSG_CHECKING([for tr1/memory implementation to use]) 39AC_MSG_CHECKING([for tr1/memory implementation to use])
39test -z "$tr1_mem" && $tr1_mem_std && tr1_mem=std 40test -z "$tr1_mem" && $tr1_mem_std && tr1_mem=std
40test -z "$tr1_mem" && $tr1_mem_boost && tr1_mem=boost 41test -z "$tr1_mem" && $tr1_mem_boost && tr1_mem=boost
41if test -z "$tr1_mem" ; then 42if test -z "$tr1_mem" ; then
42 AC_MSG_RESULT([none found]) 43 AC_MSG_RESULT([none found])
43else 44else
44 AC_MSG_RESULT([$tr1_mem]) 45 AC_MSG_RESULT([$tr1_mem])
45fi 46fi
46case "$tr1_mem" in 47case "$tr1_mem" in
47 std) 48 std)
48 $tr1_mem_std || AC_MSG_ERROR([std implementation requested, but not found]) 49 $tr1_mem_std || AC_MSG_ERROR([std implementation requested, but not found])
49 OPKELE_TR1_MEM_NS=std::tr1 50 OPKELE_TR1_MEM_NS=std::tr1
50 OPKELE_TR1_MEM_HEADER=tr1/memory 51 OPKELE_TR1_MEM_HEADER=tr1/memory
51 ;; 52 ;;
52 boost) 53 boost)
53 $tr1_mem_boost || AC_MSG_ERROR([boost implementation requested, but not found]) 54 $tr1_mem_boost || AC_MSG_ERROR([boost implementation requested, but not found])
54 OPKELE_TR1_MEM_NS=boost 55 OPKELE_TR1_MEM_NS=boost
55 OPKELE_TR1_MEM_HEADER=boost/shared_ptr.hpp 56 OPKELE_TR1_MEM_HEADER=boost/shared_ptr.hpp
56 ;; 57 ;;
57 *) 58 *)
58 AC_MSG_ERROR([no shared_ptr<> implementation found]) 59 AC_MSG_ERROR([no shared_ptr<> implementation found])
59 ;; 60 ;;
60esac 61esac
61AC_SUBST([OPKELE_TR1_MEM_NS]) 62AC_SUBST([OPKELE_TR1_MEM_NS])
62AC_SUBST([OPKELE_TR1_MEM_HEADER]) 63AC_SUBST([OPKELE_TR1_MEM_HEADER])
63 64
64AC_MSG_CHECKING([for deprecated attribute support]) 65AC_MSG_CHECKING([for deprecated attribute support])
65AC_COMPILE_IFELSE([ 66AC_COMPILE_IFELSE([
66 int __attribute__((deprecated)) deprecated_function(); 67 int __attribute__((deprecated)) deprecated_function();
67 ],[ 68 ],[
68 AC_MSG_RESULT([yes]) 69 AC_MSG_RESULT([yes])
69 AC_DEFINE([OPKELE_DEPRECATE],[__attribute__((deprecated))],[deprecated function attribute]) 70 AC_DEFINE([OPKELE_DEPRECATE],[__attribute__((deprecated))],[deprecated function attribute])
70 ],[ 71 ],[
71 AC_MSG_RESULT([no]) 72 AC_MSG_RESULT([no])
72 AC_DEFINE([OPKELE_DEPRECATE],,[deprecated function attribute]) 73 AC_DEFINE([OPKELE_DEPRECATE],,[deprecated function attribute])
73 ] 74 ]
74) 75)
75 76
76AC_LANG_PUSH([C++]) 77AC_LANG_PUSH([C++])
77AC_MSG_CHECKING([for abi::__cxa_demangle]) 78AC_MSG_CHECKING([for abi::__cxa_demangle])
78AC_COMPILE_IFELSE([ 79AC_COMPILE_IFELSE([
79 #include <typeinfo> 80 #include <typeinfo>
80 using namespace std; 81 using namespace std;
81 #include <cxxabi.h> 82 #include <cxxabi.h>
82 int main(int c,char **v) { 83 int main(int c,char **v) {
83 int dstat; 84 int dstat;
84 char *demangled = abi::__cxa_demangle(typeid(dstat).name(),0,0,&dstat); 85 char *demangled = abi::__cxa_demangle(typeid(dstat).name(),0,0,&dstat);
85 return 0; 86 return 0;
86 } 87 }
87 ],[ 88 ],[
88 AC_MSG_RESULT([yes]) 89 AC_MSG_RESULT([yes])
89 AC_DEFINE([HAVE_DEMANGLE],,[defined if abi::__cxa_demangle is available]) 90 AC_DEFINE([HAVE_DEMANGLE],,[defined if abi::__cxa_demangle is available])
90 ],[ 91 ],[
91 AC_MSG_RESULT([no]) 92 AC_MSG_RESULT([no])
92 ] 93 ]
93) 94)
94AC_LANG_POP([C++]) 95AC_LANG_POP([C++])
95 96
96 97
97 98
98 99
99PKG_CHECK_MODULES([OPENSSL],[openssl],,[ 100PKG_CHECK_MODULES([OPENSSL],[openssl],,[
100 AC_MSG_ERROR([no openssl library found. get one from http://www.openssl.org/]) 101 AC_MSG_ERROR([no openssl library found. get one from http://www.openssl.org/])
101]) 102])
102 103
103WANT_KONFORKA="yes" 104WANT_KONFORKA="yes"
104AC_ARG_ENABLE([konforka], 105AC_ARG_ENABLE([konforka],
105 AC_HELP_STRING([--disable-konforka],[do not use konforka library (default: use if found)]), 106 AC_HELP_STRING([--disable-konforka],[do not use konforka library (default: use if found)]),
106 [ 107 [
107 test "${enableval}" = "no" && WANT_KONFORKA="no" 108 test "${enableval}" = "no" && WANT_KONFORKA="no"
108 ] 109 ]
109) 110)
110if test "${WANT_KONFORKA}" = "yes" ; then 111if test "${WANT_KONFORKA}" = "yes" ; then
111 PKG_CHECK_MODULES([KONFORKA],[konforka],[ 112 PKG_CHECK_MODULES([KONFORKA],[konforka],[
112 AC_SUBST([KONFORKA_CFLAGS]) 113 AC_SUBST([KONFORKA_CFLAGS])
113 AC_SUBST([KONFORKA_LIBS]) 114 AC_SUBST([KONFORKA_LIBS])
114 AC_DEFINE([HAVE_KONFORKA],,[defined in presence of konforka library]) 115 AC_DEFINE([HAVE_KONFORKA],,[defined in presence of konforka library])
115 AC_DEFINE([OPKELE_HAVE_KONFORKA],,[defined in presence of konforka library]) 116 AC_DEFINE([OPKELE_HAVE_KONFORKA],,[defined in presence of konforka library])
116 AC_SUBST([KONFORKA_KONFORKA],[konforka]) 117 AC_SUBST([KONFORKA_KONFORKA],[konforka])
117 ],[true]) 118 ],[true])
118fi 119fi
119 120
120WANT_DOXYGEN="yes" 121WANT_DOXYGEN="yes"
121AC_ARG_ENABLE([doxygen], 122AC_ARG_ENABLE([doxygen],
122 AC_HELP_STRING([--disable-doxygen],[do not generate documentation]), 123 AC_HELP_STRING([--disable-doxygen],[do not generate documentation]),
123 [ 124 [
124 test "${enableval}" = "no" && WANT_DOXYGEN="no" 125 test "${enableval}" = "no" && WANT_DOXYGEN="no"
125 ] 126 ]
126) 127)
127if test "${WANT_DOXYGEN}" = "yes" ; then 128if test "${WANT_DOXYGEN}" = "yes" ; then
128 AC_WITH_DOXYGEN 129 AC_WITH_DOXYGEN
129 AC_WITH_DOT 130 AC_WITH_DOT
130else 131else
131 AM_CONDITIONAL([HAVE_DOXYGEN],[false]) 132 AM_CONDITIONAL([HAVE_DOXYGEN],[false])
132 AM_CONDITIONAL([HAVE_DOT],[false]) 133 AM_CONDITIONAL([HAVE_DOT],[false])
133fi 134fi
134 135
135LIBCURL_CHECK_CONFIG(,,,[ 136LIBCURL_CHECK_CONFIG(,,,[
136 AC_MSG_ERROR([no required libcurl library. get one from http://curl.haxx.se/]) 137 AC_MSG_ERROR([no required libcurl library. get one from http://curl.haxx.se/])
137]) 138])
138 139
139AC_CHECK_HEADER([expat.h],[ 140AC_CHECK_HEADER([expat.h],[
140 AC_CHECK_LIB([expat],[XML_ParserCreate],[ 141 AC_CHECK_LIB([expat],[XML_ParserCreate],[
141 EXPAT_LIBS=-lexpat 142 EXPAT_LIBS=-lexpat
142 EXPAT_CFLAGS= 143 EXPAT_CFLAGS=
143 AC_SUBST([EXPAT_LIBS]) 144 AC_SUBST([EXPAT_LIBS])
144 AC_SUBST([EXPAT_CFLAGS]) 145 AC_SUBST([EXPAT_CFLAGS])
145 ],[ 146 ],[
146 AC_MSG_ERROR([no required expat library. get one from http://expat.sourceforge.net/]) 147 AC_MSG_ERROR([no required expat library. get one from http://expat.sourceforge.net/])
147 ]) 148 ])
148],[ 149],[
149 AC_MSG_ERROR([no required expat library. get one from http://expat.sourceforge.net/]) 150 AC_MSG_ERROR([no required expat library. get one from http://expat.sourceforge.net/])
150]) 151])
151 152
152AC_CHECK_HEADERS([tidy.h tidy/tidy.h],[ 153AC_CHECK_HEADERS([tidy.h tidy/tidy.h],[
153 AC_CHECK_LIB([tidy],[tidyParseBuffer],[ 154 AC_CHECK_LIB([tidy],[tidyParseBuffer],[
154 TIDY_LIBS=-ltidy 155 TIDY_LIBS=-ltidy
155 TIDY_CFLAGS= 156 TIDY_CFLAGS=
156 AC_SUBST([TIDY_LIBS]) 157 AC_SUBST([TIDY_LIBS])
157 AC_SUBST([TIDY_CFLAGS]) 158 AC_SUBST([TIDY_CFLAGS])
158 ],[ 159 ],[
159 AC_MSG_ERROR([no required htmltidy library found. get one from http://tidy.sourceforge.net/]) 160 AC_MSG_ERROR([no required htmltidy library found. get one from http://tidy.sourceforge.net/])
160 ]) 161 ])
161],[ 162],[
162 test "$ac_header" = "tidy/tidy.h" \ 163 test "$ac_header" = "tidy/tidy.h" \
163 && AC_MSG_ERROR([no required htmltidy library found. get one from http://tidy.sourceforge.net/]) 164 && AC_MSG_ERROR([no required htmltidy library found. get one from http://tidy.sourceforge.net/])
164]) 165])
165 166
166if test -n "$PCRE_LIBS" -a -n "$PCRE_CFLAGS" ; then 167if test -n "$PCRE_LIBS" -a -n "$PCRE_CFLAGS" ; then
167 AC_SUBST([PCRE_CFLAGS]) 168 AC_SUBST([PCRE_CFLAGS])
168 AC_SUBST([PCRE_LIBS]) 169 AC_SUBST([PCRE_LIBS])
169 : 170 :
170else 171else
171 PKG_CHECK_MODULES([PCRE],[libpcre],,[ 172 PKG_CHECK_MODULES([PCRE],[libpcre],,[
172 AC_MSG_ERROR([no libpcre found, go get it at http://www.pcre.org/]) 173 AC_MSG_ERROR([no libpcre found, go get it at http://www.pcre.org/])
173 ]) 174 ])
174fi 175fi
175 176
176PKG_CHECK_MODULES([SQLITE3],[sqlite3],[have_sqlite3=true],[have_sqlite3=false]) 177PKG_CHECK_MODULES([SQLITE3],[sqlite3],[have_sqlite3=true],[have_sqlite3=false])
177AM_CONDITIONAL([HAVE_SQLITE3],[$have_sqlite3]) 178AM_CONDITIONAL([HAVE_SQLITE3],[$have_sqlite3])
178PKG_CHECK_MODULES([KINGATE],[kingate-plaincgi],[have_kingate=true],[have_kingate=false]) 179PKG_CHECK_MODULES([KINGATE],[kingate-plaincgi],[have_kingate=true],[have_kingate=false])
179AM_CONDITIONAL([HAVE_KINGATE],[$have_kingate]) 180AM_CONDITIONAL([HAVE_KINGATE],[$have_kingate])
180PKG_CHECK_MODULES([UUID],[uuid],[have_uuid=true],[have_uuid=false]) 181PKG_CHECK_MODULES([UUID],[uuid],[have_uuid=true],[have_uuid=false])
181AM_CONDITIONAL([HAVE_UUID],[$have_uuid]) 182AM_CONDITIONAL([HAVE_UUID],[$have_uuid])
182if $have_uuid ; then 183if $have_uuid ; then
183 AC_DEFINE([HAVE_LIBUUID],,[defined in presence of libuuid]) 184 AC_DEFINE([HAVE_LIBUUID],,[defined in presence of libuuid])
184 AC_SUBST([UUID_UUID],[uuid]) 185 AC_SUBST([UUID_UUID],[uuid])
185fi 186fi
186 187
187curl_ssl_verify_host="true" 188curl_ssl_verify_host="true"
188AC_ARG_ENABLE([ssl-verify-host], 189AC_ARG_ENABLE([ssl-verify-host],
189 AC_HELP_STRING([--disable-ssl-verify-host],[disable cURL cert/host relationships verification]), 190 AC_HELP_STRING([--disable-ssl-verify-host],[disable cURL cert/host relationships verification]),
190 [ test "${enableval}" = "no" && curl_ssl_verify_host="false" ] 191 [ test "${enableval}" = "no" && curl_ssl_verify_host="false" ]
191) 192)
192${curl_ssl_verify_host} || AC_DEFINE([DISABLE_CURL_SSL_VERIFYHOST],,[defined if cURL is not to verify cert/host]) 193${curl_ssl_verify_host} || AC_DEFINE([DISABLE_CURL_SSL_VERIFYHOST],,[defined if cURL is not to verify cert/host])
193 194
194curl_ssl_verify_peer="true" 195curl_ssl_verify_peer="true"
195AC_ARG_ENABLE([ssl-verify-peer], 196AC_ARG_ENABLE([ssl-verify-peer],
196 AC_HELP_STRING([--disable-ssl-verify-peer],[disable cURL cert validity verification]), 197 AC_HELP_STRING([--disable-ssl-verify-peer],[disable cURL cert validity verification]),
197 [ test "${enableval}" = "no" && curl_ssl_verify_peer="false" ] 198 [ test "${enableval}" = "no" && curl_ssl_verify_peer="false" ]
198) 199)
199${curl_ssl_verify_peer} || AC_DEFINE([DISABLE_CURL_SSL_VERIFYPEER],,[defined if cURL is not to verify cert validity]) 200${curl_ssl_verify_peer} || AC_DEFINE([DISABLE_CURL_SSL_VERIFYPEER],,[defined if cURL is not to verify cert validity])
200 201
201postels_law=true 202postels_law=true
202AC_ARG_ENABLE([postels-law], 203AC_ARG_ENABLE([postels-law],
203 AC_HELP_STRING([--disable-postels-law],[Be strict, do not adhere to Postel's Law ("be conservative in what you do, be liberal in what you accept from others", RFC 793)]), 204 AC_HELP_STRING([--disable-postels-law],[Be strict, do not adhere to Postel's Law ("be conservative in what you do, be liberal in what you accept from others", RFC 793)]),
204 [ test "${enableval}" = "no" && postels_law=false ] 205 [ test "${enableval}" = "no" && postels_law=false ]
205) 206)
206$postels_law && AC_DEFINE([POSTELS_LAW],,[defined if we want to adhere to Postel's Law]) 207$postels_law && AC_DEFINE([POSTELS_LAW],,[defined if we want to adhere to Postel's Law])
207 208
208AC_DEFINE_UNQUOTED([OPKELE_SRC_DIR],["$PWD"],[source directory]) 209AC_DEFINE_UNQUOTED([OPKELE_SRC_DIR],["$PWD"],[source directory])
209 210
210nitpick=false 211nitpick=false
211AC_ARG_ENABLE([nitpicking], 212AC_ARG_ENABLE([nitpicking],
212 AC_HELP_STRING([--enable-nitpicking],[make compiler somewhat overly fastidious about the code it deals with]), 213 AC_HELP_STRING([--enable-nitpicking],[make compiler somewhat overly fastidious about the code it deals with]),
213 [ test "$enableval" = "no" || nitpick=true ] 214 [ test "$enableval" = "no" || nitpick=true ]
214) 215)
215if $nitpick ; then 216if $nitpick ; then
216 CPP_NITPICK="-pedantic -Wall -Wextra -Wundef -Wshadow \ 217 CPP_NITPICK="-pedantic -Wall -Wextra -Wundef -Wshadow \
217 -Wunsafe-loop-optimizations -Wconversion -Wmissing-format-attribute \ 218 -Wunsafe-loop-optimizations -Wconversion -Wmissing-format-attribute \
218 -Wredundant-decls -ansi" 219 -Wredundant-decls -ansi"
219 # -Wlogical-op -Wmissing-noreturn 220 # -Wlogical-op -Wmissing-noreturn
220 C_NITPICK="$CPP_NITPICK" 221 C_NITPICK="$CPP_NITPICK"
221 CXX_NITPICK="$C_NITPICK" 222 CXX_NITPICK="$C_NITPICK"
222 223
223 CPPFLAGS="$CPPFLAGS $CPP_NITPICK" 224 CPPFLAGS="$CPPFLAGS $CPP_NITPICK"
224 CFLAGS="$CFLAGS $C_NITPICK" 225 CFLAGS="$CFLAGS $C_NITPICK"
225 CXXFLAGS="$CXXFLAGS $CXX_NITPICK" 226 CXXFLAGS="$CXXFLAGS $CXX_NITPICK"
226fi 227fi
227 228
228ndebug=true 229ndebug=true
229AC_ARG_ENABLE([debug], 230AC_ARG_ENABLE([debug],
230 AC_HELP_STRING([--enable-debug],[enable debugging code]), 231 AC_HELP_STRING([--enable-debug],[enable debugging code]),
231 [ test "$enableval" = "no" || ndebug=false ] 232 [ test "$enableval" = "no" || ndebug=false ]
232) 233)
233if $ndebug ; then 234if $ndebug ; then
234 CPPFLAGS_DEBUG="-DNDEBUG" 235 CPPFLAGS_DEBUG="-DNDEBUG"
235else 236else
236 CPPFLAGS_DEBUG="" 237 CPPFLAGS_DEBUG=""
237fi 238fi
238AC_SUBST([CPPFLAGS_DEBUG]) 239AC_SUBST([CPPFLAGS_DEBUG])
239 240
240xri_proxy_url="https://xri.net/" 241xri_proxy_url="https://xri.net/"
241AC_MSG_CHECKING([for XRI resolver proxy]) 242AC_MSG_CHECKING([for XRI resolver proxy])
242AC_ARG_ENABLE([xri-proxy], 243AC_ARG_ENABLE([xri-proxy],
243 AC_HELP_STRING([--with-xri-proxy=url],[set xri proxy for use when resolving xri identities, default is https://xri.net/]), 244 AC_HELP_STRING([--with-xri-proxy=url],[set xri proxy for use when resolving xri identities, default is https://xri.net/]),
244 [ xri_proxy_url="$withval" ] 245 [ xri_proxy_url="$withval" ]
245) 246)
246AC_MSG_RESULT([$xri_proxy_url]) 247AC_MSG_RESULT([$xri_proxy_url])
247AC_DEFINE_UNQUOTED([XRI_PROXY_URL],["$xri_proxy_url"],[XRI proxy resolver URL]) 248AC_DEFINE_UNQUOTED([XRI_PROXY_URL],["$xri_proxy_url"],[XRI proxy resolver URL])
248 249
249AC_CONFIG_FILES([ 250AC_CONFIG_FILES([
250 Makefile 251 Makefile
251 libopkele.pc 252 libopkele.pc
252 Doxyfile 253 Doxyfile
253 include/Makefile 254 include/Makefile
254 include/opkele/tr1-mem.h 255 include/opkele/tr1-mem.h
255 lib/Makefile 256 lib/Makefile
256 test/Makefile 257 test/Makefile
257]) 258])
258AC_OUTPUT 259AC_OUTPUT
diff --git a/lib/util.cc b/lib/util.cc
index d979502..a46ba2a 100644
--- a/lib/util.cc
+++ b/lib/util.cc
@@ -1,525 +1,540 @@
1#include <errno.h> 1#include <errno.h>
2#include <cassert> 2#include <cassert>
3#include <cctype> 3#include <cctype>
4#include <cstring> 4#include <cstring>
5#include <vector> 5#include <vector>
6#include <string> 6#include <string>
7#include <stack> 7#include <stack>
8#include <algorithm> 8#include <algorithm>
9#include <openssl/bio.h> 9#include <openssl/bio.h>
10#include <openssl/evp.h> 10#include <openssl/evp.h>
11#include <openssl/sha.h> 11#include <openssl/sha.h>
12#include <openssl/hmac.h> 12#include <openssl/hmac.h>
13#include <opkele/util.h> 13#include <opkele/util.h>
14#include <opkele/exception.h> 14#include <opkele/exception.h>
15#include <opkele/data.h> 15#include <opkele/data.h>
16#include <opkele/debug.h> 16#include <opkele/debug.h>
17 17
18#include <config.h> 18#include <config.h>
19#ifdef HAVE_DEMANGLE 19#ifdef HAVE_DEMANGLE
20# include <cxxabi.h> 20# include <cxxabi.h>
21#endif 21#endif
22 22
23namespace opkele { 23namespace opkele {
24 using namespace std; 24 using namespace std;
25 25
26 namespace util { 26 namespace util {
27 27
28 /* 28 /*
29 * base64 29 * base64
30 */ 30 */
31 string encode_base64(const void *data,size_t length) { 31 string encode_base64(const void *data,size_t length) {
32 BIO *b64 = 0, *bmem = 0; 32 BIO *b64 = 0, *bmem = 0;
33 try { 33 try {
34 b64 = BIO_new(BIO_f_base64()); 34 b64 = BIO_new(BIO_f_base64());
35 if(!b64) 35 if(!b64)
36 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); 36 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder");
37 BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); 37 BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
38 bmem = BIO_new(BIO_s_mem()); 38 bmem = BIO_new(BIO_s_mem());
39 BIO_set_flags(b64,BIO_CLOSE); 39 BIO_set_flags(b64,BIO_CLOSE);
40 if(!bmem) 40 if(!bmem)
41 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); 41 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer");
42 BIO_push(b64,bmem); 42 BIO_push(b64,bmem);
43 if(((size_t)BIO_write(b64,data,length))!=length) 43 if(((size_t)BIO_write(b64,data,length))!=length)
44 throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); 44 throw exception_openssl(OPKELE_CP_ "failed to BIO_write()");
45 if(BIO_flush(b64)!=1) 45 if(BIO_flush(b64)!=1)
46 throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); 46 throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()");
47 char *rvd; 47 char *rvd;
48 long rvl = BIO_get_mem_data(bmem,&rvd); 48 long rvl = BIO_get_mem_data(bmem,&rvd);
49 string rv(rvd,rvl); 49 string rv(rvd,rvl);
50 BIO_free_all(b64); 50 BIO_free_all(b64);
51 return rv; 51 return rv;
52 }catch(...) { 52 }catch(...) {
53 if(b64) BIO_free_all(b64); 53 if(b64) BIO_free_all(b64);
54 throw; 54 throw;
55 } 55 }
56 } 56 }
57 57
58 void decode_base64(const string& data,vector<unsigned char>& rv) { 58 void decode_base64(const string& data,vector<unsigned char>& rv) {
59 BIO *b64 = 0, *bmem = 0; 59 BIO *b64 = 0, *bmem = 0;
60 rv.clear(); 60 rv.clear();
61 try { 61 try {
62 bmem = BIO_new_mem_buf((void*)data.data(),data.size()); 62 bmem = BIO_new_mem_buf((void*)data.data(),data.size());
63 if(!bmem) 63 if(!bmem)
64 throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); 64 throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()");
65 b64 = BIO_new(BIO_f_base64()); 65 b64 = BIO_new(BIO_f_base64());
66 if(!b64) 66 if(!b64)
67 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); 67 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder");
68 BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); 68 BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
69 BIO_push(b64,bmem); 69 BIO_push(b64,bmem);
70 unsigned char tmp[512]; 70 unsigned char tmp[512];
71 size_t rb = 0; 71 size_t rb = 0;
72 while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) 72 while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0)
73 rv.insert(rv.end(),tmp,&tmp[rb]); 73 rv.insert(rv.end(),tmp,&tmp[rb]);
74 BIO_free_all(b64); 74 BIO_free_all(b64);
75 }catch(...) { 75 }catch(...) {
76 if(b64) BIO_free_all(b64); 76 if(b64) BIO_free_all(b64);
77 throw; 77 throw;
78 } 78 }
79 } 79 }
80 80
81 /* 81 /*
82 * big numerics 82 * big numerics
83 */ 83 */
84 84
85 BIGNUM *base64_to_bignum(const string& b64) { 85 BIGNUM *base64_to_bignum(const string& b64) {
86 vector<unsigned char> bin; 86 vector<unsigned char> bin;
87 decode_base64(b64,bin); 87 decode_base64(b64,bin);
88 BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); 88 BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0);
89 if(!rv) 89 if(!rv)
90 throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); 90 throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()");
91 return rv; 91 return rv;
92 } 92 }
93 93
94 BIGNUM *dec_to_bignum(const string& dec) { 94 BIGNUM *dec_to_bignum(const string& dec) {
95 BIGNUM *rv = 0; 95 BIGNUM *rv = 0;
96 if(!BN_dec2bn(&rv,dec.c_str())) 96 if(!BN_dec2bn(&rv,dec.c_str()))
97 throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); 97 throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()");
98 return rv; 98 return rv;
99 } 99 }
100 100
101 string bignum_to_base64(const BIGNUM *bn) { 101 string bignum_to_base64(const BIGNUM *bn) {
102 vector<unsigned char> bin(BN_num_bytes(bn)+1); 102 vector<unsigned char> bin(BN_num_bytes(bn)+1);
103 unsigned char *binptr = &(bin.front())+1; 103 unsigned char *binptr = &(bin.front())+1;
104 int l = BN_bn2bin(bn,binptr); 104 int l = BN_bn2bin(bn,binptr);
105 if(l && (*binptr)&0x80){ 105 if(l && (*binptr)&0x80){
106 (*(--binptr)) = 0; ++l; 106 (*(--binptr)) = 0; ++l;
107 } 107 }
108 return encode_base64(binptr,l); 108 return encode_base64(binptr,l);
109 } 109 }
110 110
111 /* 111 /*
112 * w3c times 112 * w3c times
113 */ 113 */
114 114
115 string time_to_w3c(time_t t) { 115 string time_to_w3c(time_t t) {
116 struct tm tm_t; 116 struct tm tm_t;
117 if(!gmtime_r(&t,&tm_t)) 117 if(!gmtime_r(&t,&tm_t))
118 throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); 118 throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()");
119 char rv[25]; 119 char rv[25];
120 if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) 120 if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t))
121 throw failed_conversion(OPKELE_CP_ "failed to strftime()"); 121 throw failed_conversion(OPKELE_CP_ "failed to strftime()");
122 return rv; 122 return rv;
123 } 123 }
124 124
125#ifndef HAVE_TIMEGM
126 static time_t timegm(struct tm *t) {
127 char *tz = getenv("TZ");
128 setenv("TZ","",1); tzset();
129 time_t rv = mktime(t);
130 if(tz)
131 setenv("TZ",tz,1);
132 else
133 unsetenv("TZ");
134 tzset();
135 return rv;
136 }
137 #define timegm opkele::util::timegm
138#endif /* HAVE_TIMEGM */
139
125 time_t w3c_to_time(const string& w) { 140 time_t w3c_to_time(const string& w) {
126 int fraction; 141 int fraction;
127 struct tm tm_t; 142 struct tm tm_t;
128 memset(&tm_t,0,sizeof(tm_t)); 143 memset(&tm_t,0,sizeof(tm_t));
129 if( ( 144 if( (
130 sscanf( 145 sscanf(
131 w.c_str(), 146 w.c_str(),
132 "%04d-%02d-%02dT%02d:%02d:%02dZ", 147 "%04d-%02d-%02dT%02d:%02d:%02dZ",
133 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, 148 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday,
134 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec 149 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec
135 ) != 6 150 ) != 6
136 ) && ( 151 ) && (
137 sscanf( 152 sscanf(
138 w.c_str(), 153 w.c_str(),
139 "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", 154 "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
140 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, 155 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday,
141 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, 156 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec,
142 &fraction 157 &fraction
143 ) != 7 158 ) != 7
144 ) ) 159 ) )
145 throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); 160 throw failed_conversion(OPKELE_CP_ "failed to sscanf()");
146 tm_t.tm_mon--; 161 tm_t.tm_mon--;
147 tm_t.tm_year-=1900; 162 tm_t.tm_year-=1900;
148 time_t rv = mktime(&tm_t); 163 time_t rv = timegm(&tm_t);
149 if(rv==(time_t)-1) 164 if(rv==(time_t)-1)
150 throw failed_conversion(OPKELE_CP_ "failed to mktime()"); 165 throw failed_conversion(OPKELE_CP_ "failed to gmtime()");
151 return rv-timezone; 166 return rv;
152 } 167 }
153 168
154 /* 169 /*
155 * 170 *
156 */ 171 */
157 172
158 static inline bool isrfc3986unreserved(int c) { 173 static inline bool isrfc3986unreserved(int c) {
159 if(c<'-') return false; 174 if(c<'-') return false;
160 if(c<='.') return true; 175 if(c<='.') return true;
161 if(c<'0') return false; if(c<='9') return true; 176 if(c<'0') return false; if(c<='9') return true;
162 if(c<'A') return false; if(c<='Z') return true; 177 if(c<'A') return false; if(c<='Z') return true;
163 if(c<'_') return false; 178 if(c<'_') return false;
164 if(c=='_') return true; 179 if(c=='_') return true;
165 if(c<'a') return false; if(c<='z') return true; 180 if(c<'a') return false; if(c<='z') return true;
166 if(c=='~') return true; 181 if(c=='~') return true;
167 return false; 182 return false;
168 } 183 }
169 184
170 struct __url_encoder : public unary_function<char,void> { 185 struct __url_encoder : public unary_function<char,void> {
171 public: 186 public:
172 string& rv; 187 string& rv;
173 188
174 __url_encoder(string& r) : rv(r) { } 189 __url_encoder(string& r) : rv(r) { }
175 190
176 result_type operator()(argument_type c) { 191 result_type operator()(argument_type c) {
177 if(isrfc3986unreserved(c)) 192 if(isrfc3986unreserved(c))
178 rv += c; 193 rv += c;
179 else{ 194 else{
180 char tmp[4]; 195 char tmp[4];
181 snprintf(tmp,sizeof(tmp),"%%%02X", 196 snprintf(tmp,sizeof(tmp),"%%%02X",
182 (c&0xff)); 197 (c&0xff));
183 rv += tmp; 198 rv += tmp;
184 } 199 }
185 } 200 }
186 }; 201 };
187 202
188 string url_encode(const string& str) { 203 string url_encode(const string& str) {
189 string rv; 204 string rv;
190 for_each(str.begin(),str.end(), 205 for_each(str.begin(),str.end(),
191 __url_encoder(rv)); 206 __url_encoder(rv));
192 return rv; 207 return rv;
193 } 208 }
194 209
195 string url_decode(const string& str) { 210 string url_decode(const string& str) {
196 string rv; 211 string rv;
197 back_insert_iterator<string> ii(rv); 212 back_insert_iterator<string> ii(rv);
198 for(string::const_iterator i=str.begin(),ie=str.end(); 213 for(string::const_iterator i=str.begin(),ie=str.end();
199 i!=ie;++i) { 214 i!=ie;++i) {
200 switch(*i) { 215 switch(*i) {
201 case '+': 216 case '+':
202 *(ii++) = ' '; break; 217 *(ii++) = ' '; break;
203 case '%': 218 case '%':
204 ++i; 219 ++i;
205 static char tmp[3] = {0,0,0}; 220 static char tmp[3] = {0,0,0};
206 if(i==ie) 221 if(i==ie)
207 throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string"); 222 throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string");
208 tmp[0] = *(i++); 223 tmp[0] = *(i++);
209 if(i==ie) 224 if(i==ie)
210 throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string"); 225 throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string");
211 tmp[1] = *i; 226 tmp[1] = *i;
212 if(!(isxdigit(tmp[0]) && isxdigit(tmp[1]))) 227 if(!(isxdigit(tmp[0]) && isxdigit(tmp[1])))
213 throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string"); 228 throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string");
214 *(ii++) = (char)strtol(tmp,0,16); 229 *(ii++) = (char)strtol(tmp,0,16);
215 break; 230 break;
216 default: 231 default:
217 *(ii++) = *i; break; 232 *(ii++) = *i; break;
218 } 233 }
219 } 234 }
220 return rv; 235 return rv;
221 } 236 }
222 237
223 string attr_escape(const string& str) { 238 string attr_escape(const string& str) {
224 static const char *unsafechars = "<>&\n\"'"; 239 static const char *unsafechars = "<>&\n\"'";
225 string rv; 240 string rv;
226 string::size_type p=0; 241 string::size_type p=0;
227 while(true) { 242 while(true) {
228 string::size_type us = str.find_first_of(unsafechars,p); 243 string::size_type us = str.find_first_of(unsafechars,p);
229 if(us==string::npos) { 244 if(us==string::npos) {
230 if(p!=str.length()) 245 if(p!=str.length())
231 rv.append(str,p,str.length()-p); 246 rv.append(str,p,str.length()-p);
232 return rv; 247 return rv;
233 } 248 }
234 rv.append(str,p,us-p); 249 rv.append(str,p,us-p);
235 rv += "&#"; 250 rv += "&#";
236 rv += long_to_string((long)str[us]); 251 rv += long_to_string((long)str[us]);
237 rv += ';'; 252 rv += ';';
238 p = us+1; 253 p = us+1;
239 } 254 }
240 } 255 }
241 256
242 string long_to_string(long l) { 257 string long_to_string(long l) {
243 char rv[32]; 258 char rv[32];
244 int r=snprintf(rv,sizeof(rv),"%ld",l); 259 int r=snprintf(rv,sizeof(rv),"%ld",l);
245 if(r<0 || r>=(int)sizeof(rv)) 260 if(r<0 || r>=(int)sizeof(rv))
246 throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); 261 throw failed_conversion(OPKELE_CP_ "failed to snprintf()");
247 return rv; 262 return rv;
248 } 263 }
249 264
250 long string_to_long(const string& s) { 265 long string_to_long(const string& s) {
251 char *endptr = 0; 266 char *endptr = 0;
252 long rv = strtol(s.c_str(),&endptr,10); 267 long rv = strtol(s.c_str(),&endptr,10);
253 if((!endptr) || endptr==s.c_str()) 268 if((!endptr) || endptr==s.c_str())
254 throw failed_conversion(OPKELE_CP_ "failed to strtol()"); 269 throw failed_conversion(OPKELE_CP_ "failed to strtol()");
255 return rv; 270 return rv;
256 } 271 }
257 272
258 /* 273 /*
259 * Normalize URL according to the rules, described in rfc 3986, section 6 274 * Normalize URL according to the rules, described in rfc 3986, section 6
260 * 275 *
261 * - uppercase hex triplets (e.g. %ab -> %AB) 276 * - uppercase hex triplets (e.g. %ab -> %AB)
262 * - lowercase scheme and host 277 * - lowercase scheme and host
263 * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, 278 * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3,
264 * that is - [:alpha:][:digit:]._~- 279 * that is - [:alpha:][:digit:]._~-
265 * - remove dot segments 280 * - remove dot segments
266 * - remove empty and default ports 281 * - remove empty and default ports
267 * - if there's no path component, add '/' 282 * - if there's no path component, add '/'
268 */ 283 */
269 string rfc_3986_normalize_uri(const string& uri) { 284 string rfc_3986_normalize_uri(const string& uri) {
270 string rv; 285 string rv;
271 string::size_type ns = uri.find_first_not_of(data::_whitespace_chars); 286 string::size_type ns = uri.find_first_not_of(data::_whitespace_chars);
272 if(ns==string::npos) 287 if(ns==string::npos)
273 throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); 288 throw bad_input(OPKELE_CP_ "Can't normalize empty URI");
274 string::size_type colon = uri.find(':',ns); 289 string::size_type colon = uri.find(':',ns);
275 if(colon==string::npos) 290 if(colon==string::npos)
276 throw bad_input(OPKELE_CP_ "No scheme specified in URI"); 291 throw bad_input(OPKELE_CP_ "No scheme specified in URI");
277 transform( 292 transform(
278 uri.begin()+ns, uri.begin()+colon+1, 293 uri.begin()+ns, uri.begin()+colon+1,
279 back_inserter(rv), ::tolower ); 294 back_inserter(rv), ::tolower );
280 bool s; 295 bool s;
281 string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1; 296 string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1;
282 if(ul <= (colon+3)) 297 if(ul <= (colon+3))
283 throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); 298 throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered");
284 if(uri[colon+1]!='/' || uri[colon+2]!='/') 299 if(uri[colon+1]!='/' || uri[colon+2]!='/')
285 throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); 300 throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component");
286 if(rv=="http:") 301 if(rv=="http:")
287 s = false; 302 s = false;
288 else if(rv=="https:") 303 else if(rv=="https:")
289 s = true; 304 s = true;
290 else{ 305 else{
291 /* TODO: support more schemes. e.g. xri. How do we normalize 306 /* TODO: support more schemes. e.g. xri. How do we normalize
292 * xri? 307 * xri?
293 */ 308 */
294 rv.append(uri,colon+1,ul-colon-1); 309 rv.append(uri,colon+1,ul-colon-1);
295 return rv; 310 return rv;
296 } 311 }
297 rv += "//"; 312 rv += "//";
298 string::size_type interesting = uri.find_first_of(":/#?",colon+3); 313 string::size_type interesting = uri.find_first_of(":/#?",colon+3);
299 if(interesting==string::npos) { 314 if(interesting==string::npos) {
300 transform( 315 transform(
301 uri.begin()+colon+3,uri.begin()+ul, 316 uri.begin()+colon+3,uri.begin()+ul,
302 back_inserter(rv), ::tolower ); 317 back_inserter(rv), ::tolower );
303 rv += '/'; return rv; 318 rv += '/'; return rv;
304 } 319 }
305 transform( 320 transform(
306 uri.begin()+colon+3,uri.begin()+interesting, 321 uri.begin()+colon+3,uri.begin()+interesting,
307 back_inserter(rv), ::tolower ); 322 back_inserter(rv), ::tolower );
308 bool qf = false; 323 bool qf = false;
309 char ic = uri[interesting]; 324 char ic = uri[interesting];
310 if(ic==':') { 325 if(ic==':') {
311 string::size_type ni = uri.find_first_of("/#?%",interesting+1); 326 string::size_type ni = uri.find_first_of("/#?%",interesting+1);
312 const char *nptr = uri.data()+interesting+1; 327 const char *nptr = uri.data()+interesting+1;
313 char *eptr = 0; 328 char *eptr = 0;
314 long port = strtol(nptr,&eptr,10); 329 long port = strtol(nptr,&eptr,10);
315 if( (port>0) && (port<65535) && port!=(s?443:80) ) { 330 if( (port>0) && (port<65535) && port!=(s?443:80) ) {
316 char tmp[8]; 331 char tmp[8];
317 snprintf(tmp,sizeof(tmp),":%ld",port); 332 snprintf(tmp,sizeof(tmp),":%ld",port);
318 rv += tmp; 333 rv += tmp;
319 } 334 }
320 if(ni==string::npos) { 335 if(ni==string::npos) {
321 rv += '/'; return rv; 336 rv += '/'; return rv;
322 } 337 }
323 interesting = ni; 338 interesting = ni;
324 }else if(ic!='/') { 339 }else if(ic!='/') {
325 rv += '/'; rv += ic; 340 rv += '/'; rv += ic;
326 qf = true; 341 qf = true;
327 ++interesting; 342 ++interesting;
328 } 343 }
329 string::size_type n = interesting; 344 string::size_type n = interesting;
330 char tmp[3] = { 0,0,0 }; 345 char tmp[3] = { 0,0,0 };
331 stack<string::size_type> psegs; psegs.push(rv.length()); 346 stack<string::size_type> psegs; psegs.push(rv.length());
332 string pseg; 347 string pseg;
333 for(;n<ul;) { 348 for(;n<ul;) {
334 string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); 349 string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n);
335 if(unsafe==string::npos) { 350 if(unsafe==string::npos) {
336 pseg.append(uri,n,ul-n-1); n = ul-1; 351 pseg.append(uri,n,ul-n-1); n = ul-1;
337 }else{ 352 }else{
338 pseg.append(uri,n,unsafe-n); 353 pseg.append(uri,n,unsafe-n);
339 n = unsafe; 354 n = unsafe;
340 } 355 }
341 char c = uri[n++]; 356 char c = uri[n++];
342 if(c=='%') { 357 if(c=='%') {
343 if((n+1)>=ul) 358 if((n+1)>=ul)
344 throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); 359 throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character");
345 tmp[0] = uri[n++]; 360 tmp[0] = uri[n++];
346 tmp[1] = uri[n++]; 361 tmp[1] = uri[n++];
347 if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) 362 if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) ))
348 throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); 363 throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized");
349 int cc = strtol(tmp,0,16); 364 int cc = strtol(tmp,0,16);
350 if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) 365 if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) )
351 pseg += (char)cc; 366 pseg += (char)cc;
352 else{ 367 else{
353 pseg += '%'; 368 pseg += '%';
354 pseg += (char)toupper(tmp[0]); pseg += (char)toupper(tmp[1]); 369 pseg += (char)toupper(tmp[0]); pseg += (char)toupper(tmp[1]);
355 } 370 }
356 }else if(qf) { 371 }else if(qf) {
357 rv += pseg; rv += c; 372 rv += pseg; rv += c;
358 pseg.clear(); 373 pseg.clear();
359 }else if(n>=ul || strchr("?/#",c)) { 374 }else if(n>=ul || strchr("?/#",c)) {
360 if(pseg.empty() || pseg==".") { 375 if(pseg.empty() || pseg==".") {
361 }else if(pseg=="..") { 376 }else if(pseg=="..") {
362 if(psegs.size()>1) { 377 if(psegs.size()>1) {
363 rv.resize(psegs.top()); psegs.pop(); 378 rv.resize(psegs.top()); psegs.pop();
364 } 379 }
365 }else{ 380 }else{
366 psegs.push(rv.length()); 381 psegs.push(rv.length());
367 if(c!='/') { 382 if(c!='/') {
368 pseg += c; 383 pseg += c;
369 qf = true; 384 qf = true;
370 } 385 }
371 rv += '/'; rv += pseg; 386 rv += '/'; rv += pseg;
372 } 387 }
373 if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { 388 if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) {
374 rv += '/'; 389 rv += '/';
375 if(n<ul) 390 if(n<ul)
376 qf = true; 391 qf = true;
377 }else if(strchr("?#",c)) { 392 }else if(strchr("?#",c)) {
378 if(psegs.size()==1 && psegs.top()==rv.length()) 393 if(psegs.size()==1 && psegs.top()==rv.length())
379 rv += '/'; 394 rv += '/';
380 if(pseg.empty()) 395 if(pseg.empty())
381 rv += c; 396 rv += c;
382 qf = true; 397 qf = true;
383 } 398 }
384 pseg.clear(); 399 pseg.clear();
385 }else{ 400 }else{
386 pseg += c; 401 pseg += c;
387 } 402 }
388 } 403 }
389 if(!pseg.empty()) { 404 if(!pseg.empty()) {
390 if(!qf) rv += '/'; 405 if(!qf) rv += '/';
391 rv += pseg; 406 rv += pseg;
392 } 407 }
393 return rv; 408 return rv;
394 } 409 }
395 410
396 string& strip_uri_fragment_part(string& u) { 411 string& strip_uri_fragment_part(string& u) {
397 string::size_type q = u.find('?'), f = u.find('#'); 412 string::size_type q = u.find('?'), f = u.find('#');
398 if(q==string::npos) { 413 if(q==string::npos) {
399 if(f!=string::npos) 414 if(f!=string::npos)
400 u.erase(f); 415 u.erase(f);
401 }else{ 416 }else{
402 if(f!=string::npos) { 417 if(f!=string::npos) {
403 if(f<q) 418 if(f<q)
404 u.erase(f,q-f); 419 u.erase(f,q-f);
405 else 420 else
406 u.erase(f); 421 u.erase(f);
407 } 422 }
408 } 423 }
409 return u; 424 return u;
410 } 425 }
411 426
412 bool uri_matches_realm(const string& uri,const string& realm) { 427 bool uri_matches_realm(const string& uri,const string& realm) {
413 string nrealm = opkele::util::rfc_3986_normalize_uri(realm); 428 string nrealm = opkele::util::rfc_3986_normalize_uri(realm);
414 string nu = opkele::util::rfc_3986_normalize_uri(uri); 429 string nu = opkele::util::rfc_3986_normalize_uri(uri);
415 string::size_type pr = nrealm.find("://"); 430 string::size_type pr = nrealm.find("://");
416 string::size_type pu = nu.find("://"); 431 string::size_type pu = nu.find("://");
417 assert(!(pr==string::npos || pu==string::npos)); 432 assert(!(pr==string::npos || pu==string::npos));
418 pr += sizeof("://")-1; 433 pr += sizeof("://")-1;
419 pu += sizeof("://")-1; 434 pu += sizeof("://")-1;
420 if(!strncmp(nrealm.c_str()+pr,"*.",2)) { 435 if(!strncmp(nrealm.c_str()+pr,"*.",2)) {
421 pr = nrealm.find('.',pr); 436 pr = nrealm.find('.',pr);
422 pu = nu.find('.',pu); 437 pu = nu.find('.',pu);
423 assert(pr!=string::npos); 438 assert(pr!=string::npos);
424 if(pu==string::npos) 439 if(pu==string::npos)
425 return false; 440 return false;
426 // TODO: check for overgeneralized realm 441 // TODO: check for overgeneralized realm
427 } 442 }
428 string::size_type lr = nrealm.length(); 443 string::size_type lr = nrealm.length();
429 string::size_type lu = nu.length(); 444 string::size_type lu = nu.length();
430 if( (lu-pu) < (lr-pr) ) 445 if( (lu-pu) < (lr-pr) )
431 return false; 446 return false;
432 pair<const char*,const char*> mp = mismatch( 447 pair<const char*,const char*> mp = mismatch(
433 nrealm.c_str()+pr,nrealm.c_str()+lr, 448 nrealm.c_str()+pr,nrealm.c_str()+lr,
434 nu.c_str()+pu); 449 nu.c_str()+pu);
435 if( (*(mp.first-1))!='/' 450 if( (*(mp.first-1))!='/'
436 && !strchr("/?#",*mp.second) ) 451 && !strchr("/?#",*mp.second) )
437 return false; 452 return false;
438 return true; 453 return true;
439 } 454 }
440 455
441 string abi_demangle(const char *mn) { 456 string abi_demangle(const char *mn) {
442#ifndef HAVE_DEMANGLE 457#ifndef HAVE_DEMANGLE
443 return mn; 458 return mn;
444#else /* !HAVE_DEMANGLE */ 459#else /* !HAVE_DEMANGLE */
445 int dstat; 460 int dstat;
446 char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); 461 char *demangled = abi::__cxa_demangle(mn,0,0,&dstat);
447 if(dstat) 462 if(dstat)
448 return mn; 463 return mn;
449 string rv = demangled; 464 string rv = demangled;
450 free(demangled); 465 free(demangled);
451 return rv; 466 return rv;
452#endif /* !HAVE_DEMANGLE */ 467#endif /* !HAVE_DEMANGLE */
453 } 468 }
454 469
455 string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { 470 string base64_signature(const assoc_t& assoc,const basic_openid_message& om) {
456 const string& slist = om.get_field("signed"); 471 const string& slist = om.get_field("signed");
457 string kv; 472 string kv;
458 string::size_type p=0; 473 string::size_type p=0;
459 while(true) { 474 while(true) {
460 string::size_type co = slist.find(',',p); 475 string::size_type co = slist.find(',',p);
461 string f = (co==string::npos) 476 string f = (co==string::npos)
462 ?slist.substr(p):slist.substr(p,co-p); 477 ?slist.substr(p):slist.substr(p,co-p);
463 kv += f; 478 kv += f;
464 kv += ':'; 479 kv += ':';
465 kv += om.get_field(f); 480 kv += om.get_field(f);
466 kv += '\n'; 481 kv += '\n';
467 if(co==string::npos) break; 482 if(co==string::npos) break;
468 p = co+1; 483 p = co+1;
469 } 484 }
470 const secret_t& secret = assoc->secret(); 485 const secret_t& secret = assoc->secret();
471 const EVP_MD *evpmd; 486 const EVP_MD *evpmd;
472 const string& at = assoc->assoc_type(); 487 const string& at = assoc->assoc_type();
473 if(at=="HMAC-SHA256") 488 if(at=="HMAC-SHA256")
474 evpmd = EVP_sha256(); 489 evpmd = EVP_sha256();
475 else if(at=="HMAC-SHA1") 490 else if(at=="HMAC-SHA1")
476 evpmd = EVP_sha1(); 491 evpmd = EVP_sha1();
477 else 492 else
478 throw unsupported(OPKELE_CP_ "unknown association type"); 493 throw unsupported(OPKELE_CP_ "unknown association type");
479 unsigned int md_len = 0; 494 unsigned int md_len = 0;
480 unsigned char md[SHA256_DIGEST_LENGTH]; 495 unsigned char md[SHA256_DIGEST_LENGTH];
481 HMAC(evpmd, 496 HMAC(evpmd,
482 &(secret.front()),secret.size(), 497 &(secret.front()),secret.size(),
483 (const unsigned char*)kv.data(),kv.length(), 498 (const unsigned char*)kv.data(),kv.length(),
484 md,&md_len); 499 md,&md_len);
485 return encode_base64(md,md_len); 500 return encode_base64(md,md_len);
486 } 501 }
487 502
488 string normalize_identifier(const string& usi,bool strip_fragment) { 503 string normalize_identifier(const string& usi,bool strip_fragment) {
489 if(usi.empty()) 504 if(usi.empty())
490 return usi; 505 return usi;
491 string rv; 506 string rv;
492 string::size_type fsc = usi.find_first_not_of(data::_whitespace_chars); 507 string::size_type fsc = usi.find_first_not_of(data::_whitespace_chars);
493 if(fsc==string::npos) 508 if(fsc==string::npos)
494 return rv; 509 return rv;
495 string::size_type lsc = usi.find_last_not_of(data::_whitespace_chars); 510 string::size_type lsc = usi.find_last_not_of(data::_whitespace_chars);
496 assert(lsc!=string::npos); 511 assert(lsc!=string::npos);
497 if(!strncasecmp(usi.c_str()+fsc,"xri://",sizeof("xri://")-1)) 512 if(!strncasecmp(usi.c_str()+fsc,"xri://",sizeof("xri://")-1))
498 fsc += sizeof("xri://")-1; 513 fsc += sizeof("xri://")-1;
499 if( (fsc+1) >= lsc ) 514 if( (fsc+1) >= lsc )
500 return rv; 515 return rv;
501 rv.assign(usi,fsc,lsc-fsc+1); 516 rv.assign(usi,fsc,lsc-fsc+1);
502 if(strchr(data::_iname_leaders,rv[0])) { 517 if(strchr(data::_iname_leaders,rv[0])) {
503 /* TODO: further normalize xri identity, fold case or 518 /* TODO: further normalize xri identity, fold case or
504 * whatever... */ 519 * whatever... */
505 }else{ 520 }else{
506 if(rv.find("://")==string::npos) 521 if(rv.find("://")==string::npos)
507 rv.insert(0,"http://"); 522 rv.insert(0,"http://");
508 if(strip_fragment) { 523 if(strip_fragment) {
509 string::size_type fp = rv.find('#'); 524 string::size_type fp = rv.find('#');
510 if(fp!=string::npos) { 525 if(fp!=string::npos) {
511 string::size_type qp = rv.find('?'); 526 string::size_type qp = rv.find('?');
512 if(qp==string::npos || qp<fp) 527 if(qp==string::npos || qp<fp)
513 rv.erase(fp); 528 rv.erase(fp);
514 else if(qp>fp) 529 else if(qp>fp)
515 rv.erase(fp,qp-fp); 530 rv.erase(fp,qp-fp);
516 } 531 }
517 } 532 }
518 rv = rfc_3986_normalize_uri(rv); 533 rv = rfc_3986_normalize_uri(rv);
519 } 534 }
520 return rv; 535 return rv;
521 } 536 }
522 537
523 } 538 }
524 539
525} 540}