code style
This commit is contained in:
@@ -1144,7 +1144,8 @@ void BusHandler::messageCompleted() {
|
||||
} else if (master) {
|
||||
logNotice(lf_update, "%s unknown MM cmd: %s", prefix, m_command.getStr().c_str());
|
||||
} else {
|
||||
logNotice(lf_update, "%s unknown MS cmd: %s / %s", prefix, m_command.getStr().c_str(), m_response.getStr().c_str());
|
||||
logNotice(lf_update, "%s unknown MS cmd: %s / %s", prefix, m_command.getStr().c_str(),
|
||||
m_response.getStr().c_str());
|
||||
}
|
||||
} else {
|
||||
m_messages->invalidateCache(message);
|
||||
@@ -1168,7 +1169,8 @@ void BusHandler::messageCompleted() {
|
||||
logNotice(lf_update, "%s %s %s QQ=%2.2x ZZ=%2.2x: %s", prefix, circuit.c_str(), name.c_str(), srcAddress,
|
||||
dstAddress, data.c_str());
|
||||
} else {
|
||||
logNotice(lf_update, "%s %s %s ZZ=%2.2x: %s", prefix, circuit.c_str(), name.c_str(), dstAddress, data.c_str());
|
||||
logNotice(lf_update, "%s %s %s ZZ=%2.2x: %s", prefix, circuit.c_str(), name.c_str(), dstAddress,
|
||||
data.c_str());
|
||||
}
|
||||
} else if (message->getSrcAddress() == SYN) { // any source
|
||||
logNotice(lf_update, "%s %s %s QQ=%2.2x: %s", prefix, circuit.c_str(), name.c_str(), srcAddress, data.c_str());
|
||||
|
||||
+3
-3
@@ -746,7 +746,7 @@ static result_t collectConfigFiles(const string& relPath, const string& prefix,
|
||||
if (!s_configUriPrefix.empty()) {
|
||||
string uri = s_configUriPrefix + relPathWithSlash + "?t=" + extension.substr(1) + query;
|
||||
string names;
|
||||
if (!s_configHttpClient.get(uri, "", names)) {
|
||||
if (!s_configHttpClient.get(uri, "", &names)) {
|
||||
return RESULT_ERR_NOTFOUND;
|
||||
}
|
||||
istringstream stream(names);
|
||||
@@ -955,7 +955,7 @@ result_t loadDefinitionsFromConfigPath(FileReader* reader, const string& filenam
|
||||
stream = FileReader::openFile(s_configLocalPrefix + filename, errorDescription, &mtime);
|
||||
} else {
|
||||
string content;
|
||||
if (s_configHttpClient.get(s_configUriPrefix + filename, "", content, &mtime)) {
|
||||
if (s_configHttpClient.get(s_configUriPrefix + filename, "", &content, &mtime)) {
|
||||
stream = new istringstream(content);
|
||||
}
|
||||
}
|
||||
@@ -1232,7 +1232,7 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
uint16_t configPort = 80;
|
||||
string proto, configHost;
|
||||
if (!HttpClient::parseUrl(configPath, proto, configHost, configPort, s_configUriPrefix)) {
|
||||
if (!HttpClient::parseUrl(configPath, &proto, &configHost, &configPort, &s_configUriPrefix)) {
|
||||
logError(lf_main, "invalid configPath URL");
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
@@ -340,7 +340,7 @@ void MainLoop::run() {
|
||||
m_busHandler->formatUpdateInfo(&ostr);
|
||||
ostr << "}";
|
||||
string response;
|
||||
if (!client.post("/updatecheck/", ostr.str(), response)) {
|
||||
if (!client.post("/updatecheck/", ostr.str(), &response)) {
|
||||
logError(lf_main, "update check error: %s", response.c_str());
|
||||
} else {
|
||||
m_updateCheck = response.empty() ? "unknown" : response;
|
||||
@@ -992,7 +992,7 @@ result_t MainLoop::executeWrite(const vector<string>& args, const string levels,
|
||||
argPos = 0; // print usage
|
||||
}
|
||||
|
||||
if (argPos == 0 || (!newDefinition && (circuit.empty() || (args.size() != argPos + 2 && args.size() != argPos + 1)))) {
|
||||
if (argPos == 0 || (!newDefinition && (circuit.empty() || (args.size() != argPos+2 && args.size() != argPos+1)))) {
|
||||
*ostream << "usage: write [-s QQ] [-d ZZ] -c CIRCUIT NAME [VALUE[;VALUE]*]\n"
|
||||
" or: write [-s QQ] [-d ZZ] -def DEFINITION [VALUE[;VALUE]*]\n"
|
||||
" or: write [-s QQ] [-c CIRCUIT] -h ZZPBSBNN[DD]*\n"
|
||||
|
||||
@@ -48,10 +48,12 @@ static const struct argp_option g_mqtt_argp_options[] = {
|
||||
{"mqttport", O_PORT, "PORT", 0, "Connect to MQTT broker on PORT (usually 1883), 0 to disable [0]", 0 },
|
||||
{"mqttuser", O_USER, "USER", 0, "Connect as USER to MQTT broker (no default)", 0 },
|
||||
{"mqttpass", O_PASS, "PASSWORD", 0, "Use PASSWORD when connecting to MQTT broker (no default)", 0 },
|
||||
{"mqtttopic", O_TOPI, "TOPIC", 0, "Use MQTT TOPIC (prefix before /%circuit/%name or complete format) [ebusd]", 0 },
|
||||
{"mqtttopic", O_TOPI, "TOPIC", 0,
|
||||
"Use MQTT TOPIC (prefix before /%circuit/%name or complete format) [ebusd]", 0 },
|
||||
{"mqttretain", O_RETA, nullptr, 0, "Retain all topics instead of only selected global ones", 0 },
|
||||
{"mqttjson", O_JSON, nullptr, 0, "Publish in JSON format instead of strings", 0 },
|
||||
{"mqttignoreinvalid", O_IGIN, nullptr, 0, "Ignore invalid parameters during init (e.g. for DNS not resolvable yet)", 0 },
|
||||
{"mqttignoreinvalid", O_IGIN, nullptr, 0,
|
||||
"Ignore invalid parameters during init (e.g. for DNS not resolvable yet)", 0 },
|
||||
|
||||
#if (LIBMOSQUITTO_MAJOR >= 1)
|
||||
{"mqttca", O_CAFI, "CA", 0, "Use CA file or dir (ending with '/') for MQTT TLS (no default)", 0 },
|
||||
@@ -65,15 +67,15 @@ static const struct argp_option g_mqtt_argp_options[] = {
|
||||
|
||||
static const char* g_host = "localhost"; //!< host name of MQTT broker [localhost]
|
||||
static uint16_t g_port = 0; //!< optional port of MQTT broker, 0 to disable [0]
|
||||
static const char* g_username = nullptr; //!< optional user name for MQTT broker (no default)
|
||||
static const char* g_password = nullptr; //!< optional password for MQTT broker (no default)
|
||||
static const char* g_username = nullptr; //!< optional user name for MQTT broker (no default)
|
||||
static const char* g_password = nullptr; //!< optional password for MQTT broker (no default)
|
||||
/** the MQTT topic string parts. */
|
||||
static vector<string> g_topicStrs;
|
||||
/** the MQTT topic field parts. */
|
||||
static vector<string> g_topicFields;
|
||||
static bool g_retain = false; //!< whether to retail all topics
|
||||
static OutputFormat g_publishFormat = 0; //!< the OutputFormat for publishing messages
|
||||
static bool g_ignoreInvalidParams = false; //!< ignore invalid parameters during init
|
||||
static bool g_ignoreInvalidParams = false; //!< ignore invalid parameters during init
|
||||
|
||||
#if (LIBMOSQUITTO_MAJOR >= 1)
|
||||
static const char* g_cafile = nullptr; //!< CA file for TLS
|
||||
@@ -195,7 +197,8 @@ static error_t mqtt_parse_opt(int key, char *arg, struct argp_state *state) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct argp g_mqtt_argp = { g_mqtt_argp_options, mqtt_parse_opt, nullptr, nullptr, nullptr, nullptr, nullptr };
|
||||
static const struct argp g_mqtt_argp = { g_mqtt_argp_options, mqtt_parse_opt, nullptr, nullptr, nullptr, nullptr,
|
||||
nullptr };
|
||||
static const struct argp_child g_mqtt_argp_child = {&g_mqtt_argp, 0, "", 1};
|
||||
|
||||
|
||||
|
||||
+2
-2
@@ -207,7 +207,7 @@ class DataField : public AttributedItem {
|
||||
* Return whether this is a @a DataFieldSet.
|
||||
* @return true if this is a @a DataFieldSet.
|
||||
*/
|
||||
virtual bool isSet() const { return false; };
|
||||
virtual bool isSet() const { return false; }
|
||||
|
||||
/**
|
||||
* Factory method for creating new instances.
|
||||
@@ -595,7 +595,7 @@ class ConstantDataField : public SingleDataField {
|
||||
* A set of @a DataField instances.
|
||||
*/
|
||||
class DataFieldSet : public DataField {
|
||||
friend class LoadableDataFieldSet;
|
||||
friend class LoadableDataFieldSet;
|
||||
public:
|
||||
/**
|
||||
* Get the @a DataFieldSet for parsing the identification message (service 0x07 0x04).
|
||||
|
||||
+33
-33
@@ -912,8 +912,8 @@ bool DataTypeList::s_contrib_initialized = libebus_contrib_register();
|
||||
DataTypeList::DataTypeList() {
|
||||
add(new StringDataType("STR", MAX_LEN*8, ADJ, ' ')); // >= 1 byte character string filled up with space
|
||||
// unsigned decimal in BCD, 0000 - 9999 (fixed length)
|
||||
add(new NumberDataType("PIN", 16, FIX|BCD|REV, 0xffff, 0, 0x9999, 1, nullptr));
|
||||
add(new NumberDataType("UCH", 8, 0, 0xff, 0, 0xfe, 1, nullptr)); // unsigned integer, 0 - 254
|
||||
add(new NumberDataType("PIN", 16, FIX|BCD|REV, 0xffff, 0, 0x9999, 1));
|
||||
add(new NumberDataType("UCH", 8, 0, 0xff, 0, 0xfe, 1)); // unsigned integer, 0 - 254
|
||||
add(new StringDataType("IGN", MAX_LEN*8, IGN|ADJ, 0)); // >= 1 byte ignored data
|
||||
// >= 1 byte character string filled up with 0x00 (null terminated string)
|
||||
add(new StringDataType("NTS", MAX_LEN*8, ADJ, 0));
|
||||
@@ -951,56 +951,56 @@ DataTypeList::DataTypeList() {
|
||||
add(new DateTimeDataType("TTH", 6, 0, 0, false, true, 30));
|
||||
// truncated time (only multiple of 15 minutes), 00:00 - 24:00 (minutes div 15 + hour * 4 as integer)
|
||||
add(new DateTimeDataType("TTQ", 7, 0, 0, false, true, 15));
|
||||
add(new NumberDataType("BDY", 8, DAY, 0x07, 0, 6, 1, nullptr)); // weekday, "Mon" - "Sun" (0x00 - 0x06) [eBUS type]
|
||||
add(new NumberDataType("HDY", 8, DAY, 0x00, 1, 7, 1, nullptr)); // weekday, "Mon" - "Sun" (0x01 - 0x07) [Vaillant type]
|
||||
add(new NumberDataType("BCD", 8, BCD, 0xff, 0, 99, 1, nullptr)); // unsigned decimal in BCD, 0 - 99
|
||||
add(new NumberDataType("BCD", 16, BCD, 0xffff, 0, 9999, 1, nullptr)); // unsigned decimal in BCD, 0 - 9999
|
||||
add(new NumberDataType("BCD", 24, BCD, 0xffffff, 0, 999999, 1, nullptr)); // unsigned decimal in BCD, 0 - 999999
|
||||
add(new NumberDataType("BCD", 32, BCD, 0xffffffff, 0, 99999999, 1, nullptr)); // unsigned decimal in BCD, 0 - 99999999
|
||||
add(new NumberDataType("HCD", 32, HCD|BCD|REQ, 0, 0, 99999999, 1, nullptr)); // unsigned decimal in HCD, 0 - 99999999
|
||||
add(new NumberDataType("HCD", 8, HCD|BCD|REQ, 0, 0, 99, 1, nullptr)); // unsigned decimal in HCD, 0 - 99
|
||||
add(new NumberDataType("HCD", 16, HCD|BCD|REQ, 0, 0, 9999, 1, nullptr)); // unsigned decimal in HCD, 0 - 9999
|
||||
add(new NumberDataType("HCD", 24, HCD|BCD|REQ, 0, 0, 999999, 1, nullptr)); // unsigned decimal in HCD, 0 - 999999
|
||||
add(new NumberDataType("SCH", 8, SIG, 0x80, 0x81, 0x7f, 1, nullptr)); // signed integer, -127 - +127
|
||||
add(new NumberDataType("D1B", 8, SIG, 0x80, 0x81, 0x7f, 1, nullptr)); // signed integer, -127 - +127
|
||||
add(new NumberDataType("BDY", 8, DAY, 0x07, 0, 6, 1)); // weekday, "Mon" - "Sun" (0x00 - 0x06) [eBUS type]
|
||||
add(new NumberDataType("HDY", 8, DAY, 0x00, 1, 7, 1)); // weekday, "Mon" - "Sun" (0x01 - 0x07) [Vaillant type]
|
||||
add(new NumberDataType("BCD", 8, BCD, 0xff, 0, 99, 1)); // unsigned decimal in BCD, 0 - 99
|
||||
add(new NumberDataType("BCD", 16, BCD, 0xffff, 0, 9999, 1)); // unsigned decimal in BCD, 0 - 9999
|
||||
add(new NumberDataType("BCD", 24, BCD, 0xffffff, 0, 999999, 1)); // unsigned decimal in BCD, 0 - 999999
|
||||
add(new NumberDataType("BCD", 32, BCD, 0xffffffff, 0, 99999999, 1)); // unsigned decimal in BCD, 0 - 99999999
|
||||
add(new NumberDataType("HCD", 32, HCD|BCD|REQ, 0, 0, 99999999, 1)); // unsigned decimal in HCD, 0 - 99999999
|
||||
add(new NumberDataType("HCD", 8, HCD|BCD|REQ, 0, 0, 99, 1)); // unsigned decimal in HCD, 0 - 99
|
||||
add(new NumberDataType("HCD", 16, HCD|BCD|REQ, 0, 0, 9999, 1)); // unsigned decimal in HCD, 0 - 9999
|
||||
add(new NumberDataType("HCD", 24, HCD|BCD|REQ, 0, 0, 999999, 1)); // unsigned decimal in HCD, 0 - 999999
|
||||
add(new NumberDataType("SCH", 8, SIG, 0x80, 0x81, 0x7f, 1)); // signed integer, -127 - +127
|
||||
add(new NumberDataType("D1B", 8, SIG, 0x80, 0x81, 0x7f, 1)); // signed integer, -127 - +127
|
||||
// unsigned number (fraction 1/2), 0 - 100 (0x00 - 0xc8, replacement 0xff)
|
||||
add(new NumberDataType("D1C", 8, 0, 0xff, 0x00, 0xc8, 2, nullptr));
|
||||
add(new NumberDataType("D1C", 8, 0, 0xff, 0x00, 0xc8, 2));
|
||||
// signed number (fraction 1/256), -127.99 - +127.99
|
||||
add(new NumberDataType("D2B", 16, SIG, 0x8000, 0x8001, 0x7fff, 256, nullptr));
|
||||
add(new NumberDataType("D2B", 16, SIG, 0x8000, 0x8001, 0x7fff, 256));
|
||||
// signed number (fraction 1/16), -2047.9 - +2047.9
|
||||
add(new NumberDataType("D2C", 16, SIG, 0x8000, 0x8001, 0x7fff, 16, nullptr));
|
||||
add(new NumberDataType("D2C", 16, SIG, 0x8000, 0x8001, 0x7fff, 16));
|
||||
// signed number (fraction 1/1000), -32.767 - +32.767, little endian
|
||||
add(new NumberDataType("FLT", 16, SIG, 0x8000, 0x8001, 0x7fff, 1000, nullptr));
|
||||
add(new NumberDataType("FLT", 16, SIG, 0x8000, 0x8001, 0x7fff, 1000));
|
||||
// signed number (fraction 1/1000), -32.767 - +32.767, big endian
|
||||
add(new NumberDataType("FLR", 16, SIG|REV, 0x8000, 0x8001, 0x7fff, 1000, nullptr));
|
||||
add(new NumberDataType("FLR", 16, SIG|REV, 0x8000, 0x8001, 0x7fff, 1000));
|
||||
// signed number (IEEE 754 binary32: 1 bit sign, 8 bits exponent, 23 bits significand), little endian
|
||||
add(new NumberDataType("EXP", 32, SIG|EXP, 0x7f800000, 0x00000000, 0xffffffff, 1, nullptr));
|
||||
add(new NumberDataType("EXP", 32, SIG|EXP, 0x7f800000, 0x00000000, 0xffffffff, 1));
|
||||
// signed number (IEEE 754 binary32: 1 bit sign, 8 bits exponent, 23 bits significand), big endian
|
||||
add(new NumberDataType("EXR", 32, SIG|EXP|REV, 0x7f800000, 0x00000000, 0xffffffff, 1, nullptr));
|
||||
add(new NumberDataType("EXR", 32, SIG|EXP|REV, 0x7f800000, 0x00000000, 0xffffffff, 1));
|
||||
// unsigned integer, 0 - 65534, little endian
|
||||
add(new NumberDataType("UIN", 16, 0, 0xffff, 0, 0xfffe, 1, nullptr));
|
||||
add(new NumberDataType("UIN", 16, 0, 0xffff, 0, 0xfffe, 1));
|
||||
// unsigned integer, 0 - 65534, big endian
|
||||
add(new NumberDataType("UIR", 16, REV, 0xffff, 0, 0xfffe, 1, nullptr));
|
||||
add(new NumberDataType("UIR", 16, REV, 0xffff, 0, 0xfffe, 1));
|
||||
// signed integer, -32767 - +32767, little endian
|
||||
add(new NumberDataType("SIN", 16, SIG, 0x8000, 0x8001, 0x7fff, 1, nullptr));
|
||||
add(new NumberDataType("SIN", 16, SIG, 0x8000, 0x8001, 0x7fff, 1));
|
||||
// signed integer, -32767 - +32767, big endian
|
||||
add(new NumberDataType("SIR", 16, SIG|REV, 0x8000, 0x8001, 0x7fff, 1, nullptr));
|
||||
add(new NumberDataType("SIR", 16, SIG|REV, 0x8000, 0x8001, 0x7fff, 1));
|
||||
// unsigned 3 bytes int, 0 - 16777214, little endian
|
||||
add(new NumberDataType("U3N", 24, 0, 0xffffff, 0, 0xfffffe, 1, nullptr));
|
||||
add(new NumberDataType("U3N", 24, 0, 0xffffff, 0, 0xfffffe, 1));
|
||||
// unsigned 3 bytes int, 0 - 16777214, big endian
|
||||
add(new NumberDataType("U3R", 24, REV, 0xffffff, 0, 0xfffffe, 1, nullptr));
|
||||
add(new NumberDataType("U3R", 24, REV, 0xffffff, 0, 0xfffffe, 1));
|
||||
// signed 3 bytes int, -8388607 - +8388607, little endian
|
||||
add(new NumberDataType("S3N", 24, SIG, 0x800000, 0x800001, 0xffffff, 1, nullptr));
|
||||
add(new NumberDataType("S3N", 24, SIG, 0x800000, 0x800001, 0xffffff, 1));
|
||||
// signed 3 bytes int, -8388607 - +8388607, big endian
|
||||
add(new NumberDataType("S3R", 24, SIG|REV, 0x800000, 0x800001, 0xffffff, 1, nullptr));
|
||||
add(new NumberDataType("S3R", 24, SIG|REV, 0x800000, 0x800001, 0xffffff, 1));
|
||||
// unsigned integer, 0 - 4294967294, little endian
|
||||
add(new NumberDataType("ULG", 32, 0, 0xffffffff, 0, 0xfffffffe, 1, nullptr));
|
||||
add(new NumberDataType("ULG", 32, 0, 0xffffffff, 0, 0xfffffffe, 1));
|
||||
// unsigned integer, 0 - 4294967294, big endian
|
||||
add(new NumberDataType("ULR", 32, REV, 0xffffffff, 0, 0xfffffffe, 1, nullptr));
|
||||
add(new NumberDataType("ULR", 32, REV, 0xffffffff, 0, 0xfffffffe, 1));
|
||||
// signed integer, -2147483647 - +2147483647, little endian
|
||||
add(new NumberDataType("SLG", 32, SIG, 0x80000000, 0x80000001, 0xffffffff, 1, nullptr));
|
||||
add(new NumberDataType("SLG", 32, SIG, 0x80000000, 0x80000001, 0xffffffff, 1));
|
||||
// signed integer, -2147483647 - +2147483647, big endian
|
||||
add(new NumberDataType("SLR", 32, SIG|REV, 0x80000000, 0x80000001, 0xffffffff, 1, nullptr));
|
||||
add(new NumberDataType("SLR", 32, SIG|REV, 0x80000000, 0x80000001, 0xffffffff, 1));
|
||||
add(new NumberDataType("BI0", 7, ADJ|REQ, 0, 0, 1)); // bit 0 (up to 7 bits until bit 6)
|
||||
add(new NumberDataType("BI1", 7, ADJ|REQ, 0, 1, 1)); // bit 1 (up to 7 bits until bit 7)
|
||||
add(new NumberDataType("BI2", 6, ADJ|REQ, 0, 2, 1)); // bit 2 (up to 6 bits until bit 7)
|
||||
|
||||
@@ -403,7 +403,7 @@ class NumberDataType : public DataType {
|
||||
*/
|
||||
NumberDataType(const string& id, size_t bitCount, uint16_t flags, unsigned int replacement,
|
||||
unsigned int minValue, unsigned int maxValue, int divisor,
|
||||
const NumberDataType* baseType)
|
||||
const NumberDataType* baseType = nullptr)
|
||||
: DataType(id, bitCount, flags|NUM, replacement), m_minValue(minValue), m_maxValue(maxValue), m_divisor(divisor),
|
||||
m_precision(calcPrecision(divisor)), m_firstBit(0), m_baseType(baseType) {}
|
||||
|
||||
|
||||
@@ -1276,7 +1276,8 @@ class MessageMap : public MappedFileReader {
|
||||
|
||||
// @copydoc
|
||||
bool extractDefaultsFromFilename(const string& filename, map<string, string>* defaults,
|
||||
symbol_t* destAddress = nullptr, unsigned int* software = nullptr, unsigned int* hardware = nullptr) const override;
|
||||
symbol_t* destAddress = nullptr, unsigned int* software = nullptr, unsigned int* hardware = nullptr) const
|
||||
override;
|
||||
|
||||
// @copydoc
|
||||
result_t readFromStream(istream* stream, const string& filename, const time_t& mtime, bool verbose,
|
||||
|
||||
@@ -28,14 +28,14 @@ using std::ostringstream;
|
||||
using std::dec;
|
||||
using std::hex;
|
||||
|
||||
bool HttpClient::parseUrl(const string& url, string& proto, string& host, uint16_t& port, string& uri) {
|
||||
bool HttpClient::parseUrl(const string& url, string* proto, string* host, uint16_t* port, string* uri) {
|
||||
size_t hostPos = url.find("://");
|
||||
if (hostPos == string::npos) {
|
||||
return false;
|
||||
}
|
||||
proto = url.substr(0, hostPos);
|
||||
*proto = url.substr(0, hostPos);
|
||||
hostPos += 3;
|
||||
if (proto != "http") {
|
||||
if (*proto != "http") {
|
||||
return false;
|
||||
}
|
||||
size_t pos = url.find('/', hostPos);
|
||||
@@ -43,28 +43,28 @@ bool HttpClient::parseUrl(const string& url, string& proto, string& host, uint16
|
||||
return false;
|
||||
}
|
||||
if (pos == string::npos) {
|
||||
host = url.substr(hostPos);
|
||||
uri = "/";
|
||||
*host = url.substr(hostPos);
|
||||
*uri = "/";
|
||||
} else {
|
||||
host = url.substr(hostPos, pos - hostPos);
|
||||
uri = url.substr(pos);
|
||||
if (uri[uri.length()-1] != '/') {
|
||||
uri += "/";
|
||||
*host = url.substr(hostPos, pos - hostPos);
|
||||
*uri = url.substr(pos);
|
||||
if ((*uri)[uri->length()-1] != '/') {
|
||||
*uri += "/";
|
||||
}
|
||||
}
|
||||
pos = host.find(':');
|
||||
pos = host->find(':');
|
||||
if (pos == 0) {
|
||||
return false;
|
||||
}
|
||||
port = 80;
|
||||
*port = 80;
|
||||
if (pos != string::npos) {
|
||||
char* strEnd = nullptr;
|
||||
unsigned long value = strtoul(host.c_str()+pos+1, &strEnd, 10);
|
||||
unsigned long value = strtoul(host->c_str()+pos+1, &strEnd, 10);
|
||||
if (strEnd == nullptr || *strEnd != '\0' || value < 1 || value > 65535) {
|
||||
return false;
|
||||
}
|
||||
port = static_cast<uint16_t>(value);
|
||||
host = host.substr(0, pos);
|
||||
*port = static_cast<uint16_t>(value);
|
||||
*host = host->substr(0, pos);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -108,11 +108,11 @@ void HttpClient::disconnect() {
|
||||
}
|
||||
}
|
||||
|
||||
bool HttpClient::get(const string& uri, const string& body, string& response, time_t* time) {
|
||||
bool HttpClient::get(const string& uri, const string& body, string* response, time_t* time) {
|
||||
return request("GET", uri, body, response, time);
|
||||
}
|
||||
|
||||
bool HttpClient::post(const string& uri, const string& body, string& response) {
|
||||
bool HttpClient::post(const string& uri, const string& body, string* response) {
|
||||
return request("POST", uri, body, response);
|
||||
}
|
||||
|
||||
@@ -123,9 +123,9 @@ const int indexToMonth[] = {
|
||||
-1, -1, 4, -1, -1, -1, -1, -1, // 24-31
|
||||
};
|
||||
|
||||
bool HttpClient::request(const string& method, const string& uri, const string& body, string& response, time_t* time) {
|
||||
bool HttpClient::request(const string& method, const string& uri, const string& body, string* response, time_t* time) {
|
||||
if (!ensureConnected()) {
|
||||
response = "not connected";
|
||||
*response = "not connected";
|
||||
return false;
|
||||
}
|
||||
ostringstream ostr;
|
||||
@@ -149,33 +149,33 @@ bool HttpClient::request(const string& method, const string& uri, const string&
|
||||
ssize_t sent = m_socket->send(cstr + pos, len - pos);
|
||||
if (sent < 0) {
|
||||
disconnect();
|
||||
response = "send error";
|
||||
*response = "send error";
|
||||
return false;
|
||||
}
|
||||
pos += sent;
|
||||
}
|
||||
string result;
|
||||
size_t pos = readUntil(" ", 4 * 1024, result); // max 4k headers
|
||||
size_t pos = readUntil(" ", 4 * 1024, &result); // max 4k headers
|
||||
if (pos == string::npos || pos > 8 || result.substr(0, 5) != "HTTP/") {
|
||||
disconnect();
|
||||
response = "receive error (headers)";
|
||||
*response = "receive error (headers)";
|
||||
return false;
|
||||
}
|
||||
if (result.substr(pos+1, 6) != "200 OK") {
|
||||
disconnect();
|
||||
size_t endpos = result.find("\r\n", pos+1);
|
||||
response = "receive error: " + result.substr(pos+1, endpos == string::npos ? endpos : endpos-pos-1);
|
||||
*response = "receive error: " + result.substr(pos+1, endpos == string::npos ? endpos : endpos-pos-1);
|
||||
return false;
|
||||
}
|
||||
pos = readUntil("\r\n\r\n", 4 * 1024, result); // max 4k headers
|
||||
pos = readUntil("\r\n\r\n", 4 * 1024, &result); // max 4k headers
|
||||
if (pos == string::npos) {
|
||||
disconnect();
|
||||
response = "receive error (headers)";
|
||||
*response = "receive error (headers)";
|
||||
return false;
|
||||
}
|
||||
string headers = result.substr(0, pos+2); // including final \r\n
|
||||
const char* hdrs = headers.c_str();
|
||||
response = result.substr(pos+4);
|
||||
*response = result.substr(pos+4);
|
||||
if (time) {
|
||||
pos = headers.find("\r\nLast-Modified: ");
|
||||
if (pos != string::npos && headers.substr(pos+42, 4) == " GMT") {
|
||||
@@ -224,7 +224,7 @@ bool HttpClient::request(const string& method, const string& uri, const string&
|
||||
unsigned long length = strtoul(hdrs + pos + strlen("\r\nContent-Length: "), &strEnd, 10);
|
||||
if (strEnd == nullptr || *strEnd != '\r') {
|
||||
disconnect();
|
||||
response = "invalid content length ";
|
||||
*response = "invalid content length ";
|
||||
return false;
|
||||
}
|
||||
pos = readUntil("", length, response);
|
||||
@@ -232,7 +232,7 @@ bool HttpClient::request(const string& method, const string& uri, const string&
|
||||
return pos == length;
|
||||
}
|
||||
|
||||
size_t HttpClient::readUntil(const string& delim, const size_t length, string& result) {
|
||||
size_t HttpClient::readUntil(const string& delim, const size_t length, string* result) {
|
||||
if (!m_buffer) {
|
||||
m_buffer = (char*)malloc(1024);
|
||||
if (!m_buffer) {
|
||||
@@ -241,8 +241,8 @@ size_t HttpClient::readUntil(const string& delim, const size_t length, string& r
|
||||
m_bufferSize = 1024;
|
||||
}
|
||||
bool findDelim = !delim.empty();
|
||||
size_t pos = findDelim ? result.find(delim) : string::npos;
|
||||
while (pos == string::npos && result.length() < length) {
|
||||
size_t pos = findDelim ? result->find(delim) : string::npos;
|
||||
while (pos == string::npos && result->length() < length) {
|
||||
ssize_t received = m_socket->recv(m_buffer, m_bufferSize);
|
||||
if (received < 0) {
|
||||
return string::npos;
|
||||
@@ -250,13 +250,13 @@ size_t HttpClient::readUntil(const string& delim, const size_t length, string& r
|
||||
if (received == 0) {
|
||||
break;
|
||||
}
|
||||
size_t oldLength = result.length();
|
||||
result += string(m_buffer, 0, static_cast<unsigned>(received));
|
||||
size_t oldLength = result->length();
|
||||
*result += string(m_buffer, 0, static_cast<unsigned>(received));
|
||||
if (findDelim) {
|
||||
pos = result.find(delim, oldLength - (delim.length() - 1));
|
||||
pos = result->find(delim, oldLength - (delim.length() - 1));
|
||||
}
|
||||
}
|
||||
return findDelim ? pos : result.length();
|
||||
return findDelim ? pos : result->length();
|
||||
}
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
+10
-11
@@ -16,8 +16,8 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef LIB_UTILS_HTTP_H_
|
||||
#define LIB_UTILS_HTTP_H_
|
||||
#ifndef LIB_UTILS_HTTPCLIENT_H_
|
||||
#define LIB_UTILS_HTTPCLIENT_H_
|
||||
|
||||
#include <unistd.h>
|
||||
#include <cstdint>
|
||||
@@ -39,7 +39,6 @@ using std::ifstream;
|
||||
* Helper class for handling HTTP client requests.
|
||||
*/
|
||||
class HttpClient {
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -66,7 +65,7 @@ class HttpClient {
|
||||
* @param uri the extracted URI starting with '/'.
|
||||
* @return true on success, false on failure.
|
||||
*/
|
||||
static bool parseUrl(const string& url, string& proto, string& host, uint16_t& port, string& uri);
|
||||
static bool parseUrl(const string& url, string* proto, string* host, uint16_t* port, string* uri);
|
||||
|
||||
/**
|
||||
* Connect to the specified server.
|
||||
@@ -103,7 +102,7 @@ class HttpClient {
|
||||
* @param time optional pointer to a @a time_t value for storing the modification time of the file, or nullptr.
|
||||
* @return true on success, false on error.
|
||||
*/
|
||||
bool get(const string& uri, const string& body, string& response, time_t* time = nullptr);
|
||||
bool get(const string& uri, const string& body, string* response, time_t* time = nullptr);
|
||||
|
||||
/**
|
||||
* Execute a POST request.
|
||||
@@ -112,7 +111,7 @@ class HttpClient {
|
||||
* @param response the response body from the server (or the HTTP header on error).
|
||||
* @return true on success, false on error.
|
||||
*/
|
||||
bool post(const string& uri, const string& body, string& response);
|
||||
bool post(const string& uri, const string& body, string* response);
|
||||
|
||||
/**
|
||||
* Execute an arbitrary request.
|
||||
@@ -122,9 +121,9 @@ class HttpClient {
|
||||
* @param time optional pointer to a @a time_t value for storing the modification time of the file, or nullptr.
|
||||
* @return true on success, false on error.
|
||||
*/
|
||||
bool request(const string& method, const string& uri, const string& body, string& response, time_t* time = nullptr);
|
||||
bool request(const string& method, const string& uri, const string& body, string* response, time_t* time = nullptr);
|
||||
|
||||
private:
|
||||
private:
|
||||
/**
|
||||
* Read from the connected socket until the specified delimiter is found or the specified number of bytes was received.
|
||||
* @param delim the delimiter to find, or empty for reading the specified number of bytes.
|
||||
@@ -132,9 +131,9 @@ private:
|
||||
* @param result the string to append the read data to and in which to find the delimiter.
|
||||
* @return the position of the delimiter if delimiter was set or the number of bytes received, or string::npos if not found.
|
||||
*/
|
||||
size_t readUntil(const string& delim, const size_t length, string& result);
|
||||
size_t readUntil(const string& delim, const size_t length, string* result);
|
||||
|
||||
private:
|
||||
private:
|
||||
/** the @a TCPClient handling the traffic. */
|
||||
TCPClient m_client;
|
||||
|
||||
@@ -162,4 +161,4 @@ private:
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
#endif //LIB_UTILS_HTTP_H_
|
||||
#endif // LIB_UTILS_HTTPCLIENT_H_
|
||||
|
||||
@@ -137,7 +137,6 @@ class TCPClient {
|
||||
* @return pointer to an opened tcp socket.
|
||||
*/
|
||||
TCPSocket* connect(const string& server, const uint16_t& port, int timeout = 0);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user