Skip to content

Commit 4074a9d

Browse files
committed
added OVERLOADS and HTML support
1 parent 0445772 commit 4074a9d

File tree

8 files changed

+104
-12
lines changed

8 files changed

+104
-12
lines changed

VVOSCQueryProtocol/VVOSCQueryConstants.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ extern NSString * kVVOSCQ_OptAttr_Range_Vals; // VALS
4040
extern NSString * kVVOSCQ_OptAttr_Clipmode; // CLIPMODE
4141
extern NSString * kVVOSCQ_OptAttr_Unit; // UNIT
4242
extern NSString * kVVOSCQ_OptAttr_Critical; // CRITICAL
43+
extern NSString * kVVOSCQ_OptAttr_Overloads; // OVERLOADS
44+
extern NSString * kVVOSCQ_OptAttr_HTML; // HTML
4345

4446
/* most OSC queries are "recursive" in the sense that they're going to return a full tree of
4547
OSC nodes, gathered recursively. really, the only queries that aren't recursive are queries

VVOSCQueryProtocol/VVOSCQueryConstants.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
NSString * kVVOSCQ_OptAttr_Clipmode = @"CLIPMODE";
3030
NSString * kVVOSCQ_OptAttr_Unit = @"UNIT";
3131
NSString * kVVOSCQ_OptAttr_Critical = @"CRITICAL";
32+
NSString * kVVOSCQ_OptAttr_Overloads = @"OVERLOADS";
33+
NSString * kVVOSCQ_OptAttr_HTML = @"HTML";
3234

3335

3436
NSArray<NSString*> * kVVOSCQ_NonRecursiveAttrs = nil;
@@ -68,14 +70,19 @@ @implementation VVOSCQueryConstants
6870

6971
+ (void) initialize {
7072
kVVOSCQ_NonRecursiveAttrs = @[
71-
kVVOSCQ_OptAttr_Tags,
73+
kVVOSCQ_ReqAttr_Desc,
7274
kVVOSCQ_ReqAttr_Type,
75+
kVVOSCQ_ReqAttr_HostInfo,
76+
kVVOSCQ_OptAttr_Tags,
77+
kVVOSCQ_OptAttr_Ext_Type,
7378
kVVOSCQ_OptAttr_Access,
7479
kVVOSCQ_OptAttr_Value,
7580
kVVOSCQ_OptAttr_Range,
7681
kVVOSCQ_OptAttr_Clipmode,
7782
kVVOSCQ_OptAttr_Unit,
78-
kVVOSCQ_OptAttr_Critical
83+
kVVOSCQ_OptAttr_Critical,
84+
kVVOSCQ_OptAttr_Overloads,
85+
kVVOSCQ_OptAttr_HTML
7986
];
8087
}
8188

VVOSCQueryProtocol/VVOSCQueryReply.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
range:(NSArray *)inRangeArray
3333
clipmode:(NSArray *)inClipmodeArray // nil by default. if non-nil, must be an array consisting only of instances of the kVVOSCQueryNodeClipMode* constants above.
3434
units:(NSArray *)inUnitsArray
35-
critical:(BOOL)inCritical; // NO by default. if YES, try to use a TCP connection to send data to this node.
35+
critical:(BOOL)inCritical // NO by default. if YES, try to use a TCP connection to send data to this node.
36+
overloads:(NSArray*)inOverloadsArray;
3637
#else
3738
+ (NSDictionary *) jsonObjectWithPath:(NSString *)inFullPath // full path to this node in the OSC address space
3839
contents:(NSArray*)inContents // subnodes (may be a hierarchy)
@@ -45,7 +46,8 @@
4546
range:(NSArray *)inRangeArray
4647
clipmode:(NSArray *)inClipmodeArray // nil by default. if non-nil, must be an array consisting only of instances of the kVVOSCQueryNodeClipMode* constants above.
4748
units:(NSArray *)inUnitsArray
48-
critical:(BOOL)inCritical; // NO by default. if YES, try to use a TCP connection to send data to this node.
49+
critical:(BOOL)inCritical // NO by default. if YES, try to use a TCP connection to send data to this node.
50+
overloads:(NSArray*)inOverloadsArray;
4951
#endif
5052
- (instancetype) initWithJSONObject:(NSDictionary *)jo;
5153
- (instancetype) initWithErrorCode:(int)ec;

VVOSCQueryProtocol/VVOSCQueryReply.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ + (NSDictionary *) jsonObjectWithPath:(NSString *)inFullPath
1717
range:(NSArray *)inRangeArray
1818
clipmode:(NSArray *)inClipmodeArray
1919
units:(NSArray *)inUnitsArray
20-
critical:(BOOL)inCritical {
20+
critical:(BOOL)inCritical
21+
overloads:(NSArray *)inOverloadsArray {
2122
NSMutableDictionary *returnMe = [NSMutableDictionary dictionaryWithCapacity:0];
2223
if (inFullPath != nil)
2324
[returnMe setObject:inFullPath forKey:kVVOSCQ_ReqAttr_Path];
@@ -45,6 +46,8 @@ + (NSDictionary *) jsonObjectWithPath:(NSString *)inFullPath
4546
[returnMe setObject:inUnitsArray forKey:kVVOSCQ_OptAttr_Unit];
4647
if (inCritical)
4748
[returnMe setObject:[NSNumber numberWithBool:inCritical] forKey:kVVOSCQ_OptAttr_Critical];
49+
if (inOverloadsArray)
50+
[returnMe setObject:inOverloadsArray forKey:kVVOSCQ_OptAttr_Overloads];
4851
return returnMe;
4952
}
5053
+ (void) initialize {

VVOSCQueryProtocol/VVOSCQueryServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
@property (readonly) int webServerPort;
4343
@property (retain) NSString * name;
4444
@property (retain) NSString * bonjourName;
45+
@property (retain,setter=setHTMLDirectory:) NSString * htmlDirectory;
4546
#if __has_feature(objc_arc)
4647
@property (weak) id<VVOSCQueryServerDelegate> delegate;
4748
#else

VVOSCQueryProtocol/VVOSCQueryServer.mm

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#import <SystemConfiguration/SystemConfiguration.h>
33
#import <VVOSCQueryProtocol/VVOSCQuery.h>
44
#import <VVOSCQueryProtocol/VVOSCQueryReply.h>
5-
5+
#import "NSStringAdditions.h"
66
#include "WSPPServer.hpp"
77

88

@@ -43,21 +43,42 @@ - (instancetype) init {
4343
NSArray *queryItems = [urlComponents queryItems];
4444
NSMutableDictionary *urlParams = [[NSMutableDictionary alloc] init];
4545
BOOL hasHostInfoQuery = NO;
46+
BOOL isHTMLQuery = NO;
4647
for (NSURLQueryItem *queryItem in queryItems) {
4748
NSString *tmpKey = [queryItem name];
4849
if (tmpKey == nil)
4950
continue;
5051
if ([tmpKey isEqualToString:kVVOSCQ_ReqAttr_HostInfo])
5152
hasHostInfoQuery = YES;
53+
else if ([tmpKey isEqualToString:kVVOSCQ_OptAttr_HTML])
54+
isHTMLQuery = YES;
5255
id tmpVal = [queryItem value];
5356
if (tmpVal == nil)
5457
tmpVal = [NSNull null];
5558
[urlParams setObject:tmpVal forKey:tmpKey];
5659
}
60+
61+
// if it's an HTML query, return a false along with the path of the resource to serve- we don't want to return a query reply, we want to serve a file. this is the only time the server returns a false reply (the client returns a false reply under different circumstances)
62+
if (isHTMLQuery) {
63+
NSString *basePath = [self htmlDirectory];
64+
if (basePath != nil)
65+
basePath = [basePath stringByDeletingLastAndAddingFirstSlash];
66+
NSString *frontPath = [urlComponents path];
67+
NSString *fullPath = nil;
68+
if (basePath == nil)
69+
fullPath = frontPath;
70+
else
71+
fullPath = [NSString stringWithFormat:@"%@%@",basePath,frontPath];
72+
73+
return WSPPQueryReply(false, std::string([fullPath UTF8String]));
74+
}
75+
5776
NSArray *urlParamKeys = [urlParams allKeys];
5877
// if the URL was querying a specific key, the query isn't looking for a recursive reply...
5978
BOOL isRecursive = YES;
60-
if (urlParamKeys!=nil && [urlParamKeys firstObjectCommonWithArray:kVVOSCQ_NonRecursiveAttrs]!=nil)
79+
if (hasHostInfoQuery || isHTMLQuery)
80+
isRecursive = NO;
81+
else if (urlParamKeys!=nil && [urlParamKeys firstObjectCommonWithArray:kVVOSCQ_NonRecursiveAttrs]!=nil)
6182
isRecursive = NO;
6283

6384
// make an VVOSCQuery that describes this HTTP request
@@ -298,7 +319,7 @@ - (void) setBonjourName:(NSString *)n {
298319
- (NSString *) bonjourName {
299320
return bonjourName;
300321
}
301-
322+
@synthesize htmlDirectory;
302323
- (void) _resetBonjourService {
303324
//NSLog(@"%s",__func__);
304325
if (bonjourService != nil) {

VVOSCQueryProtocol/WSPPQueryReply.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class WSPPQueryReply {
1212
WSPPQueryReply() {}
1313
WSPPQueryReply(const std::string & rs) : replyString(rs), replyCode(-1), performReply(true) {}
1414
WSPPQueryReply(const int & rc) : replyString(""), replyCode(rc), performReply(true) {}
15-
WSPPQueryReply(const bool & pr) : replyString(""), replyCode(-1), performReply(pr) {}
15+
WSPPQueryReply(const bool & pr, const std::string & rs="") : replyString(rs), replyCode(-1), performReply(pr) {}
1616
~WSPPQueryReply() {}
1717

1818
const std::string & getReplyString() const { return replyString; }

VVOSCQueryProtocol/WSPPServer.cpp

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#include "VVOSCQueryStringUtilities.hpp"
33
#include "rapidjson/document.h"
44
//#include <websocketpp/connection.hpp>
5+
#include <fstream>
6+
#include <iostream>
7+
#include <streambuf>
58

69

710
WSPPServer::WSPPServer() {
@@ -27,25 +30,78 @@ void WSPPServer::set_http_callback(HTTPCallback inCallback) {
2730
if (server != nullptr) {
2831
// pass a block to the websocketpp backend that will extract the URI from the connection nad pass it to the provided callback, which will return an WSPPQueryReply object that will be used to assemble a reply
2932
server->set_http_handler([=](connection_hdl hdl) {
33+
//cout << __PRETTY_FUNCTION__ << endl;
34+
3035
lib::error_code ec;
3136
server_t::connection_ptr conn = server->get_con_from_hdl(hdl, ec);
3237
// change some headers...
33-
conn->replace_header("Content-Type", "application/json; charset=utf-8");
34-
conn->replace_header("Connection", "close");
38+
//conn->replace_header("Content-Type", "application/json; charset=utf-8");
39+
//conn->replace_header("Connection", "close");
3540
// pull the full URI out, this is what we're going to pass to the callback
3641
uri_ptr uri = conn->get_uri();
3742
const string &fullURI = uri->str();
3843
// use the provided callback to get a reply for the URI
3944
const WSPPQueryReply &tmpReply = (httpCallback==nullptr) ? WSPPQueryReply(400) : httpCallback(fullURI);
4045
// use the WSPPQueryReply to reply to the http request
4146
try {
42-
if (tmpReply.getReplyCode()<0) {
47+
if (!tmpReply.getPerformReply()) {
48+
//cout << "should be vending a file here " << __PRETTY_FUNCTION__ << endl;
49+
50+
51+
52+
// get the resource from the query reply
53+
const std::string &tmpResource = tmpReply.getReplyString();
54+
//cout << "file i should be vending is " << tmpResource << endl;
55+
56+
std::ifstream file;
57+
std::string response;
58+
59+
file.open(tmpResource.c_str(), std::ios::in);
60+
if (!file) {
61+
conn->set_status(websocketpp::http::status_code::not_found);
62+
//conn->set_status((http::status_code::value)404);
63+
64+
// change some headers...
65+
//conn->replace_header("Content-Type", "application/json; charset=utf-8");
66+
conn->replace_header("Connection", "close");
67+
68+
}
69+
else {
70+
file.seekg(0, std::ios::end);
71+
response.reserve(file.tellg());
72+
file.seekg(0, std::ios::beg);
73+
74+
response.assign((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
75+
conn->set_body(response);
76+
conn->set_status(websocketpp::http::status_code::ok);
77+
conn->replace_header("Connection", "close");
78+
}
79+
80+
81+
82+
83+
// change some headers...
84+
//conn->replace_header("Content-Type", "application/json; charset=utf-8");
85+
//conn->replace_header("Connection", "close");
86+
}
87+
else if (tmpReply.getReplyCode()<0) {
88+
//cout << "\treply code is < 0..." << endl;
89+
4390
conn->set_body(std::move(tmpReply.getReplyString()));
4491
conn->set_status(websocketpp::http::status_code::ok);
92+
93+
// change some headers...
94+
conn->replace_header("Content-Type", "application/json; charset=utf-8");
95+
conn->replace_header("Connection", "close");
4596
}
4697
else {
98+
//cout << "\treturning a status code" << endl;
99+
47100
//conn->set_status(websocketpp::http::status_code::not_found);
48101
conn->set_status((http::status_code::value)tmpReply.getReplyCode());
102+
// change some headers...
103+
conn->replace_header("Content-Type", "application/json; charset=utf-8");
104+
conn->replace_header("Connection", "close");
49105
}
50106
}
51107
catch (const websocketpp::exception& e) {

0 commit comments

Comments
 (0)