From 1f91be988bbc0ad01ac2cf5df496ce7e6a90ea32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Mon, 8 Dec 2014 10:56:00 +0100 Subject: [PATCH 1/7] add user agent header fixes #628 --- {src => include}/mbgl/util/constants.hpp | 1 + platform/darwin/http_request_baton_cocoa.mm | 3 +++ platform/default/http_request_baton_curl.cpp | 2 ++ src/mbgl/util/constants.cpp | 1 + 4 files changed, 7 insertions(+) rename {src => include}/mbgl/util/constants.hpp (94%) diff --git a/src/mbgl/util/constants.hpp b/include/mbgl/util/constants.hpp similarity index 94% rename from src/mbgl/util/constants.hpp rename to include/mbgl/util/constants.hpp index 98365e0f3..2270f5307 100644 --- a/src/mbgl/util/constants.hpp +++ b/include/mbgl/util/constants.hpp @@ -8,6 +8,7 @@ namespace mbgl { namespace util { extern const float tileSize; +extern const char *userAgent; } diff --git a/platform/darwin/http_request_baton_cocoa.mm b/platform/darwin/http_request_baton_cocoa.mm index 1c256d0ba..403013897 100644 --- a/platform/darwin/http_request_baton_cocoa.mm +++ b/platform/darwin/http_request_baton_cocoa.mm @@ -2,6 +2,7 @@ #include #include #include +#include #include @@ -40,6 +41,8 @@ void HTTPRequestBaton::start(const util::ptr &ptr) { } } + [request addValue:@(util::userAgent) forHTTPHeaderField:@"User-Agent"]; + NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *res, NSError *error) { if (error) { diff --git a/platform/default/http_request_baton_curl.cpp b/platform/default/http_request_baton_curl.cpp index f05ee5acf..42915d3e7 100644 --- a/platform/default/http_request_baton_curl.cpp +++ b/platform/default/http_request_baton_curl.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -444,6 +445,7 @@ void start_request(void *const ptr) { curl_easy_setopt(context->handle, CURLOPT_HEADERFUNCTION, curl_header_cb); curl_easy_setopt(context->handle, CURLOPT_HEADERDATA, &context->baton->response); curl_easy_setopt(context->handle, CURLOPT_ACCEPT_ENCODING, "gzip, deflate"); + curl_easy_setopt(context->handle, CURLOPT_USERAGENT, util::userAgent); curl_easy_setopt(context->handle, CURLOPT_SHARE, share); // Start requesting the information. diff --git a/src/mbgl/util/constants.cpp b/src/mbgl/util/constants.cpp index 3d1422e6c..c37bc1480 100644 --- a/src/mbgl/util/constants.cpp +++ b/src/mbgl/util/constants.cpp @@ -1,6 +1,7 @@ #include const float mbgl::util::tileSize = 512.0f; +const char *mbgl::util::userAgent = "MapboxGL/1.0 (+https://mapbox.com/mapbox-gl/)"; #if defined(DEBUG) const bool mbgl::debug::tileParseWarnings = false; From e11c073e518139ca1729ea1e4a76c215b9abca53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Mon, 8 Dec 2014 16:21:15 +0100 Subject: [PATCH 2/7] report version number in user agent string refs #628 --- gyp/mbgl-core.gypi | 1 + gyp/mbgl-ios.gypi | 3 + gyp/mbgl-linux.gypi | 3 + gyp/mbgl-osx.gypi | 3 + gyp/version.gypi | 31 ++++++++ include/mbgl/util/constants.hpp | 2 - mapboxgl.gyp | 1 + platform/darwin/http_request_baton_cocoa.mm | 14 +++- platform/default/http_request_baton_curl.cpp | 14 +++- scripts/build-version.py | 80 ++++++++++++++++++++ src/mbgl/util/constants.cpp | 1 - 11 files changed, 146 insertions(+), 7 deletions(-) create mode 100644 gyp/version.gypi create mode 100644 scripts/build-version.py diff --git a/gyp/mbgl-core.gypi b/gyp/mbgl-core.gypi index 279051968..ea279f2da 100644 --- a/gyp/mbgl-core.gypi +++ b/gyp/mbgl-core.gypi @@ -7,6 +7,7 @@ 'hard_dependency': 1, 'dependencies': [ 'shaders', + 'version', ], 'variables': { 'cflags_cc': [ diff --git a/gyp/mbgl-ios.gypi b/gyp/mbgl-ios.gypi index ec31869ad..df6896e1e 100644 --- a/gyp/mbgl-ios.gypi +++ b/gyp/mbgl-ios.gypi @@ -30,6 +30,9 @@ 'type': 'static_library', 'standalone_static_library': 1, 'hard_dependency': 1, + 'dependencies': [ + 'version', + ], 'sources': [ '../platform/ios/cache_database_library.mm', '../platform/ios/shader_cache_library.mm', diff --git a/gyp/mbgl-linux.gypi b/gyp/mbgl-linux.gypi index 7af08242e..fdea368ae 100644 --- a/gyp/mbgl-linux.gypi +++ b/gyp/mbgl-linux.gypi @@ -5,6 +5,9 @@ 'type': 'static_library', 'standalone_static_library': 1, 'hard_dependency': 1, + 'dependencies': [ + 'version', + ], 'variables': { 'cflags_cc': [ '<@(png_cflags)', diff --git a/gyp/mbgl-osx.gypi b/gyp/mbgl-osx.gypi index 09c96807a..4e8a131da 100644 --- a/gyp/mbgl-osx.gypi +++ b/gyp/mbgl-osx.gypi @@ -5,6 +5,9 @@ 'type': 'static_library', 'standalone_static_library': 1, 'hard_dependency': 1, + 'dependencies': [ + 'version', + ], 'sources': [ '../platform/osx/cache_database_application_support.mm', '../platform/osx/shader_cache_application_support.mm', diff --git a/gyp/version.gypi b/gyp/version.gypi new file mode 100644 index 000000000..2670c190c --- /dev/null +++ b/gyp/version.gypi @@ -0,0 +1,31 @@ +{ + 'targets': [ + { + 'target_name': 'version', + 'type': 'none', + 'hard_dependency': 1, + 'actions': [ + { + 'action_name': 'Build Version File', + 'inputs': [ + '../scripts/build-version.py', + ], + 'outputs': [ + '<(SHARED_INTERMEDIATE_DIR)/include/mbgl/util/version.hpp', + '<(SHARED_INTERMEDIATE_DIR)/src/mbgl/util/mbgl_version.cpp', + ], + 'action': ['<@(python)', 'scripts/build-version.py', '<(SHARED_INTERMEDIATE_DIR)', ' #include #include -#include +#include #include @@ -15,6 +15,8 @@ namespace mbgl { dispatch_once_t request_initialize = 0; NSURLSession *session = nullptr; +NSString *userAgent = nil; + void HTTPRequestBaton::start(const util::ptr &ptr) { assert(std::this_thread::get_id() == ptr->thread_id); @@ -29,6 +31,14 @@ void HTTPRequestBaton::start(const util::ptr &ptr) { sessionConfig.URLCache = nil; session = [NSURLSession sessionWithConfiguration:sessionConfig]; + + // Write user agent string + NSDictionary *systemVersion = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]; + userAgent = [NSString stringWithFormat:@"MapboxGL/%d.%d.%d (+https://mapbox.com/mapbox-gl/; %s; %@ %@)", + version::major, version::minor, version::patch, version::revision, + [systemVersion objectForKey:@"ProductName"], + [systemVersion objectForKey:@"ProductVersion"] + ]; }); NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@(baton->path.c_str())]]; @@ -41,7 +51,7 @@ void HTTPRequestBaton::start(const util::ptr &ptr) { } } - [request addValue:@(util::userAgent) forHTTPHeaderField:@"User-Agent"]; + [request addValue:userAgent forHTTPHeaderField:@"User-Agent"]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *res, NSError *error) { diff --git a/platform/default/http_request_baton_curl.cpp b/platform/default/http_request_baton_curl.cpp index 42915d3e7..b993ced6a 100644 --- a/platform/default/http_request_baton_curl.cpp +++ b/platform/default/http_request_baton_curl.cpp @@ -3,11 +3,13 @@ #include #include #include -#include +#include #include #include +#include + #include #include #include @@ -406,9 +408,17 @@ size_t curl_header_cb(char * const buffer, const size_t size, const size_t nmemb return length; } +std::string buildUserAgentString() { + utsname name; + uname(&name); + return util::sprintf<128>("MapboxGL/%d.%d.%d (+https://mapbox.com/mapbox-gl/; %s; %s %s)", + version::major, version::minor, version::patch, version::revision, name.sysname, name.release); +} + // This function must run in the CURL thread. void start_request(void *const ptr) { assert(std::this_thread::get_id() == thread_id); + static const std::string userAgent = buildUserAgentString(); // The Context object stores information that we need to retain throughout the request, such // as the actual CURL easy handle, the baton, and the list of headers. The Context itself is @@ -445,7 +455,7 @@ void start_request(void *const ptr) { curl_easy_setopt(context->handle, CURLOPT_HEADERFUNCTION, curl_header_cb); curl_easy_setopt(context->handle, CURLOPT_HEADERDATA, &context->baton->response); curl_easy_setopt(context->handle, CURLOPT_ACCEPT_ENCODING, "gzip, deflate"); - curl_easy_setopt(context->handle, CURLOPT_USERAGENT, util::userAgent); + curl_easy_setopt(context->handle, CURLOPT_USERAGENT, userAgent.c_str()); curl_easy_setopt(context->handle, CURLOPT_SHARE, share); // Start requesting the information. diff --git a/scripts/build-version.py b/scripts/build-version.py new file mode 100644 index 000000000..3b70f9374 --- /dev/null +++ b/scripts/build-version.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python + +import sys, os, errno + + +output_dir = sys.argv[1] +tag = map(int, sys.argv[2].split('.')) +rev = sys.argv[3] + + +def mkdir_p(path): + try: + os.makedirs(path) + except OSError as exc: # Python >2.5 + if exc.errno == errno.EEXIST and os.path.isdir(path): + pass + else: raise + + +header = """// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. +#ifndef MBGL_UTIL_VERSION +#define MBGL_UTIL_VERSION + +#define MBGL_VERSION 0x{major:02x}{minor:02x}{patch:02x} +#define MBGL_VERSION_STRING "{major}.{minor}.{patch}" +#define MBGL_VERSION_MAJOR {major} +#define MBGL_VERSION_MINOR {minor} +#define MBGL_VERSION_PATCH {patch} +#define MBGL_VERSION_REV "{rev}" + +namespace mbgl {{ +namespace version {{ + +extern const int major, minor, patch; +extern const char *revision; +extern const char *string; +extern const unsigned int number; + +}} +}} + +#endif +""".format( + major = tag[0], + minor = tag[1], + patch = tag[2], + rev = rev +) + +header_path = os.path.join(output_dir, 'include/mbgl/util/version.hpp') +mkdir_p(os.path.dirname(header_path)) +with open(header_path, 'w') as f: f.write(header) + +source = """// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. +#include + +namespace mbgl {{ +namespace version {{ + +const int major = {major}; +const int minor = {minor}; +const int patch = {patch}; +const char *revision = "{rev}"; +const char *string = "{major}.{minor}.{patch}"; +const unsigned int number = 0x{major:02x}{minor:02x}{patch:02x}; + +}} +}} +""".format( + major = tag[0], + minor = tag[1], + patch = tag[2], + rev = rev +) + +# Note: We can't use version.cpp since libuv already has a file named version.c. +# Having another one here would overwrite that file and cause missing symbols errors. +source_path = os.path.join(output_dir, 'src/mbgl/util/mbgl_version.cpp') +mkdir_p(os.path.dirname(source_path)) +with open(source_path, 'w') as f: f.write(source) diff --git a/src/mbgl/util/constants.cpp b/src/mbgl/util/constants.cpp index c37bc1480..3d1422e6c 100644 --- a/src/mbgl/util/constants.cpp +++ b/src/mbgl/util/constants.cpp @@ -1,7 +1,6 @@ #include const float mbgl::util::tileSize = 512.0f; -const char *mbgl::util::userAgent = "MapboxGL/1.0 (+https://mapbox.com/mapbox-gl/)"; #if defined(DEBUG) const bool mbgl::debug::tileParseWarnings = false; From 0a84deef91c88e382091f0d428bc1192afbe0adc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Mon, 8 Dec 2014 16:47:24 +0100 Subject: [PATCH 3/7] make sure it also works when there are no git tags --- gyp/version.gypi | 2 +- scripts/build-version.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/gyp/version.gypi b/gyp/version.gypi index 2670c190c..73b2f668d 100644 --- a/gyp/version.gypi +++ b/gyp/version.gypi @@ -14,7 +14,7 @@ '<(SHARED_INTERMEDIATE_DIR)/include/mbgl/util/version.hpp', '<(SHARED_INTERMEDIATE_DIR)/src/mbgl/util/mbgl_version.cpp', ], - 'action': ['<@(python)', 'scripts/build-version.py', '<(SHARED_INTERMEDIATE_DIR)', ' Date: Mon, 8 Dec 2014 16:59:02 +0100 Subject: [PATCH 4/7] don't print more information when a tag actually is available --- gyp/version.gypi | 2 +- scripts/build-version.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gyp/version.gypi b/gyp/version.gypi index 73b2f668d..ca68f0eb5 100644 --- a/gyp/version.gypi +++ b/gyp/version.gypi @@ -14,7 +14,7 @@ '<(SHARED_INTERMEDIATE_DIR)/include/mbgl/util/version.hpp', '<(SHARED_INTERMEDIATE_DIR)/src/mbgl/util/mbgl_version.cpp', ], - 'action': ['<@(python)', 'scripts/build-version.py', '<(SHARED_INTERMEDIATE_DIR)', ' Date: Mon, 8 Dec 2014 17:21:40 +0100 Subject: [PATCH 5/7] support fewer parameters --- scripts/build-version.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/scripts/build-version.py b/scripts/build-version.py index 8db647c88..8261aea53 100644 --- a/scripts/build-version.py +++ b/scripts/build-version.py @@ -5,13 +5,16 @@ import sys, os, errno output_dir = sys.argv[1] -# When they're identical, the git describe can't find a tag and reports the rev instead. -if sys.argv[2] == sys.argv[3]: +if len(sys.argv) <= 3: tag = [0, 0, 0] + rev = sys.argv[2][0:8] else: - tag = map(int, sys.argv[2].split('.')) - -rev = sys.argv[3][0:8] + # When they're identical, the git describe can't find a tag and reports the rev instead. + if sys.argv[2] == sys.argv[3]: + tag = [0, 0, 0] + else: + tag = map(int, sys.argv[2].split('.')) + rev = sys.argv[3][0:8] def mkdir_p(path): From c61537af8fb0e6beaecebd4ce0989b838e0ce92a Mon Sep 17 00:00:00 2001 From: Mike Morris Date: Mon, 8 Dec 2014 12:24:57 -0800 Subject: [PATCH 6/7] nu_static_libs --- gyp/install.gypi | 1 + 1 file changed, 1 insertion(+) diff --git a/gyp/install.gypi b/gyp/install.gypi index d46900dff..56591d27d 100644 --- a/gyp/install.gypi +++ b/gyp/install.gypi @@ -22,6 +22,7 @@ 'conditions': [ ['OS == "linux"', { 'other_ldflags': [ + '<@(nu_static_libs)', '<@(png_static_libs)', '<@(jpeg_static_libs)', '<@(glfw3_static_libs)', From 8fd04ebc44d14440b96f0bcf391f9bee730efbac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Mon, 8 Dec 2014 22:35:03 +0100 Subject: [PATCH 7/7] add missing gyp file --- test/test.gyp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test.gyp b/test/test.gyp index 5aec5ac3a..e33cf3689 100644 --- a/test/test.gyp +++ b/test/test.gyp @@ -1,6 +1,7 @@ { 'includes': [ '../gyp/common.gypi', + '../gyp/version.gypi', '../gyp/mbgl-platform.gypi', ], 'variables' : {