prefix http parser to prevent symbol clashes

This commit is contained in:
Johannes Weiß 2018-01-30 11:14:03 +00:00
parent 606df54ee1
commit b2c63337d9
2 changed files with 55 additions and 4 deletions

View File

@ -0,0 +1,51 @@
#!/bin/bash
set -eu
here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
case "$(uname -s)" in
Darwin)
sed=gsed
;;
*)
sed=sed
;;
esac
for f in http_parser.c http_parser.h; do
( echo "/* Additional changes for SwiftNIO:"
echo " - prefixed all symbols by 'c_nio_'"
echo "*/"
curl -Ls "https://raw.githubusercontent.com/nodejs/http-parser/master/$f"
) > "$here/c_nio_$f"
"$sed" -i \
-e 's#"http_parser.h"#"include/c_nio_http_parser.h"#g' \
-e 's/\b\(http_body_is_final\)/c_nio_\1/g' \
-e 's/\b\(http_errno_description\)/c_nio_\1/g' \
-e 's/\b\(http_errno_name\)/c_nio_\1/g' \
-e 's/\b\(http_message_needs_eof\)/c_nio_\1/g' \
-e 's/\b\(http_method_str\)/c_nio_\1/g' \
-e 's/\b\(http_parser_execute\)/c_nio_\1/g' \
-e 's/\b\(http_parser_init\)/c_nio_\1/g' \
-e 's/\b\(http_parser_parse_url\)/c_nio_\1/g' \
-e 's/\b\(http_parser_pause\)/c_nio_\1/g' \
-e 's/\b\(http_parser_settings_init\)/c_nio_\1/g' \
-e 's/\b\(http_parser_url_init\)/c_nio_\1/g' \
-e 's/\b\(http_parser_version\)/c_nio_\1/g' \
-e 's/\b\(http_should_keep_alive\)/c_nio_\1/g' \
"$here/c_nio_$f"
tmp=$(mktemp -d /tmp/.test_compile_XXXXXX)
clang -o "$tmp/test.o" -c "$here/c_nio_http_parser.c"
num_non_nio=$(nm "$tmp/test.o" | grep ' T ' | grep -v c_nio | wc -l)
test 0 -eq $num_non_nio || {
echo "ERROR: $num_non_nio exported non-prefixed symbols found"
exit 1
}
rm -rf "$tmp"
done

View File

@ -244,16 +244,16 @@ public class HTTPDecoder<HTTPMessageT>: ByteToMessageDecoder, AnyHTTPDecoder {
public func decoderAdded(ctx: ChannelHandlerContext) throws { public func decoderAdded(ctx: ChannelHandlerContext) throws {
if HTTPMessageT.self == HTTPServerRequestPart.self { if HTTPMessageT.self == HTTPServerRequestPart.self {
http_parser_init(&parser, HTTP_REQUEST) c_nio_http_parser_init(&parser, HTTP_REQUEST)
} else if HTTPMessageT.self == HTTPClientResponsePart.self { } else if HTTPMessageT.self == HTTPClientResponsePart.self {
http_parser_init(&parser, HTTP_RESPONSE) c_nio_http_parser_init(&parser, HTTP_RESPONSE)
} else { } else {
fatalError("the impossible happened: MsgT neither HTTPClientRequestPart nor HTTPClientResponsePart but \(HTTPMessageT.self)") fatalError("the impossible happened: MsgT neither HTTPClientRequestPart nor HTTPClientResponsePart but \(HTTPMessageT.self)")
} }
parser.data = Unmanaged.passUnretained(ctx).toOpaque() parser.data = Unmanaged.passUnretained(ctx).toOpaque()
http_parser_settings_init(&settings) c_nio_http_parser_settings_init(&settings)
settings.on_message_begin = { parser in settings.on_message_begin = { parser in
let handler = evacuateHTTPDecoder(parser) let handler = evacuateHTTPDecoder(parser)
@ -412,7 +412,7 @@ public class HTTPDecoder<HTTPMessageT>: ByteToMessageDecoder, AnyHTTPDecoder {
state.baseAddress = pointer.baseAddress!.assumingMemoryBound(to: UInt8.self) state.baseAddress = pointer.baseAddress!.assumingMemoryBound(to: UInt8.self)
let result = state.baseAddress!.withMemoryRebound(to: Int8.self, capacity: pointer.count, { p in let result = state.baseAddress!.withMemoryRebound(to: Int8.self, capacity: pointer.count, { p in
http_parser_execute(&parser, &settings, p.advanced(by: buffer.readerIndex), buffer.readableBytes) c_nio_http_parser_execute(&parser, &settings, p.advanced(by: buffer.readerIndex), buffer.readableBytes)
}) })
state.baseAddress = nil state.baseAddress = nil