From 4a28a5e3d6f9d0d3998913573ec93b1a2e619f3a Mon Sep 17 00:00:00 2001 From: zengxs Date: Fri, 21 Dec 2018 14:30:00 +0800 Subject: [PATCH] refact: get app version & set curl user-agent --- EasyHautX.xcodeproj/project.pbxproj | 6 ++++++ EasyHautX/AppDelegate.m | 4 ++-- EasyHautX/httpclient.cc | 6 ++++++ EasyHautX/version.h | 24 +++++++++++++++++++++ EasyHautX/version.m | 33 +++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 EasyHautX/version.h create mode 100644 EasyHautX/version.m diff --git a/EasyHautX.xcodeproj/project.pbxproj b/EasyHautX.xcodeproj/project.pbxproj index 7bf94e6..bdcdcb1 100644 --- a/EasyHautX.xcodeproj/project.pbxproj +++ b/EasyHautX.xcodeproj/project.pbxproj @@ -18,6 +18,7 @@ FB08F74021CB8DC1006E5DE4 /* libcurl.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = FB08F73F21CB8DC1006E5DE4 /* libcurl.tbd */; }; FB08F74D21CBA383006E5DE4 /* encrypt.cc in Sources */ = {isa = PBXBuildFile; fileRef = FB08F74C21CBA383006E5DE4 /* encrypt.cc */; }; FB08F75221CBA5DB006E5DE4 /* httpclient.cc in Sources */ = {isa = PBXBuildFile; fileRef = FB08F75021CBA5DA006E5DE4 /* httpclient.cc */; }; + FB08F75421CCBB3E006E5DE4 /* version.m in Sources */ = {isa = PBXBuildFile; fileRef = FB08F75321CCBB3E006E5DE4 /* version.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -59,6 +60,8 @@ FB08F74C21CBA383006E5DE4 /* encrypt.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = encrypt.cc; sourceTree = ""; }; FB08F75021CBA5DA006E5DE4 /* httpclient.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = httpclient.cc; sourceTree = ""; }; FB08F75121CBA5DA006E5DE4 /* httpclient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = httpclient.h; sourceTree = ""; }; + FB08F75321CCBB3E006E5DE4 /* version.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = version.m; sourceTree = ""; }; + FB08F75521CCBB54006E5DE4 /* version.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = version.h; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -124,6 +127,8 @@ FB08F71521CB8D74006E5DE4 /* Info.plist */, FB08F71621CB8D74006E5DE4 /* main.m */, FB08F71821CB8D74006E5DE4 /* EasyHautX.entitlements */, + FB08F75321CCBB3E006E5DE4 /* version.m */, + FB08F75521CCBB54006E5DE4 /* version.h */, ); path = EasyHautX; sourceTree = ""; @@ -290,6 +295,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + FB08F75421CCBB3E006E5DE4 /* version.m in Sources */, FB08F75221CBA5DB006E5DE4 /* httpclient.cc in Sources */, FB08F71721CB8D74006E5DE4 /* main.m in Sources */, FB08F74D21CBA383006E5DE4 /* encrypt.cc in Sources */, diff --git a/EasyHautX/AppDelegate.m b/EasyHautX/AppDelegate.m index c1bf761..9f1fbf8 100644 --- a/EasyHautX/AppDelegate.m +++ b/EasyHautX/AppDelegate.m @@ -8,6 +8,7 @@ #import "AppDelegate.h" #import "httpclient.h" +#import "version.h" @interface AppDelegate () @@ -36,8 +37,7 @@ @implementation AppDelegate - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Insert code here to initialize your application @autoreleasepool { - NSString *version = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleShortVersionString"]; - NSString *labelVersion = [NSString stringWithFormat:@"Version %@", version]; + NSString *labelVersion = [NSString stringWithUTF8String:ehautx_version()]; [self.versionLabel setStringValue:labelVersion]; } } diff --git a/EasyHautX/httpclient.cc b/EasyHautX/httpclient.cc index 23825b0..684d366 100644 --- a/EasyHautX/httpclient.cc +++ b/EasyHautX/httpclient.cc @@ -15,6 +15,7 @@ #include "encrypt.h" #include "httpclient.h" +#include "version.h" using namespace std; @@ -84,6 +85,9 @@ size_t srun3k_login(const char *url, payload_t *payload, const char *key, char * readBuffer.clear(); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); + // 设置 User-Agent + curl_easy_setopt(curl, CURLOPT_USERAGENT, ehautx_user_agent()); + // 发送 http 请求 res = curl_easy_perform(curl); @@ -121,6 +125,8 @@ size_t srun3k_logout(const char *url, Payload payload, char **response) { readBuffer.clear(); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); + curl_easy_setopt(curl, CURLOPT_USERAGENT, ehautx_user_agent()); + res = curl_easy_perform(curl); curl_easy_cleanup(curl); diff --git a/EasyHautX/version.h b/EasyHautX/version.h new file mode 100644 index 0000000..726bce1 --- /dev/null +++ b/EasyHautX/version.h @@ -0,0 +1,24 @@ +// +// version.h +// EasyHautX +// +// Created by zengxs on 2018/12/21. +// Copyright © 2018 ehaut. All rights reserved. +// + +#ifndef __VERSION_H__ +#define __VERSION_H__ 1 + +#ifdef __cplusplus +extern "C" { +#endif + +const char *ehautx_version(void); +const char *ehautx_short_version(void); +const char *ehautx_user_agent(void); + +#ifdef __cplusplus +} +#endif + +#endif /* __VERSION_H__ */ diff --git a/EasyHautX/version.m b/EasyHautX/version.m new file mode 100644 index 0000000..433aede --- /dev/null +++ b/EasyHautX/version.m @@ -0,0 +1,33 @@ +// +// version.m +// EasyHautX +// +// Created by zengxs on 2018/12/21. +// Copyright © 2018 ehaut. All rights reserved. +// + +#import + +#import "version.h" + +static char short_vesrion[30]; +static char full_version[60]; +static char user_agent[60]; + +const char *ehautx_short_version(void) { + @autoreleasepool { + NSString *version = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleShortVersionString"]; + snprintf(short_vesrion, sizeof(short_vesrion), "%s", [version UTF8String]); + return short_vesrion; + } +} + +const char *ehautx_version(void) { + snprintf(full_version, sizeof(full_version), "EasyHautX v%s", ehautx_short_version()); + return full_version; +} + +const char *ehautx_user_agent(void) { + snprintf(user_agent, sizeof(user_agent), "EasyHautX/%s", ehautx_short_version()); + return user_agent; +}