Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
refact: get app version & set curl user-agent
Browse files Browse the repository at this point in the history
  • Loading branch information
zengxs committed Dec 21, 2018
1 parent 5f74ab3 commit 4a28a5e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
6 changes: 6 additions & 0 deletions EasyHautX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -59,6 +60,8 @@
FB08F74C21CBA383006E5DE4 /* encrypt.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = encrypt.cc; sourceTree = "<group>"; };
FB08F75021CBA5DA006E5DE4 /* httpclient.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = httpclient.cc; sourceTree = "<group>"; };
FB08F75121CBA5DA006E5DE4 /* httpclient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = httpclient.h; sourceTree = "<group>"; };
FB08F75321CCBB3E006E5DE4 /* version.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = version.m; sourceTree = "<group>"; };
FB08F75521CCBB54006E5DE4 /* version.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = version.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -124,6 +127,8 @@
FB08F71521CB8D74006E5DE4 /* Info.plist */,
FB08F71621CB8D74006E5DE4 /* main.m */,
FB08F71821CB8D74006E5DE4 /* EasyHautX.entitlements */,
FB08F75321CCBB3E006E5DE4 /* version.m */,
FB08F75521CCBB54006E5DE4 /* version.h */,
);
path = EasyHautX;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
4 changes: 2 additions & 2 deletions EasyHautX/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "AppDelegate.h"
#import "httpclient.h"
#import "version.h"

@interface AppDelegate ()

Expand Down Expand Up @@ -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];
}
}
Expand Down
6 changes: 6 additions & 0 deletions EasyHautX/httpclient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "encrypt.h"
#include "httpclient.h"
#include "version.h"

using namespace std;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions EasyHautX/version.h
Original file line number Diff line number Diff line change
@@ -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__ */
33 changes: 33 additions & 0 deletions EasyHautX/version.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// version.m
// EasyHautX
//
// Created by zengxs on 2018/12/21.
// Copyright © 2018 ehaut. All rights reserved.
//

#import <Foundation/Foundation.h>

#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;
}

0 comments on commit 4a28a5e

Please sign in to comment.