From 2903bb44d11dbd04ac15a282fb4aec956f22dd4e Mon Sep 17 00:00:00 2001 From: Nathan Osman Date: Wed, 29 Apr 2015 11:40:34 -0700 Subject: [PATCH] Added QHttpEngine namespace with split() function. --- src/CMakeLists.txt | 2 + src/qhttpengine.cpp | 48 ++++++++++++++++++++++ src/qhttpengine.h | 47 ++++++++++++++++++++++ tests/CMakeLists.txt | 1 + tests/TestQHttpEngine.cpp | 85 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 183 insertions(+) create mode 100644 src/qhttpengine.cpp create mode 100644 src/qhttpengine.h create mode 100644 tests/TestQHttpEngine.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4bd4f85..2170d3a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,12 +3,14 @@ configure_file(qhttpengine.pc.in "${CMAKE_CURRENT_BINARY_DIR}/qhttpengine.pc" @O set(INCLUDE "${CMAKE_CURRENT_BINARY_DIR}/config.h" + qhttpengine.h qhttprequest.h qhttpresponse.h qjsonserver.h ) set(SRC + qhttpengine.cpp qhttprequest.cpp qhttpresponse.cpp qjsonserver.cpp diff --git a/src/qhttpengine.cpp b/src/qhttpengine.cpp new file mode 100644 index 0000000..58d1105 --- /dev/null +++ b/src/qhttpengine.cpp @@ -0,0 +1,48 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Nathan Osman + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include + +#include "qhttpengine.h" + +QList QHttpEngine::split(const QByteArray &data, const QByteArray &delim) +{ + QList parts; + int index = 0; + + forever { + int nextIndex = data.indexOf(delim, index); + + // If the delimiter wasn't found, the final part is the remainder of the string + if(nextIndex == -1) { + parts.append(data.mid(index)); + break; + } + + parts.append(data.mid(index, nextIndex - index)); + index = nextIndex + delim.length(); + } + + return parts; +} diff --git a/src/qhttpengine.h b/src/qhttpengine.h new file mode 100644 index 0000000..6457e71 --- /dev/null +++ b/src/qhttpengine.h @@ -0,0 +1,47 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Nathan Osman + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#ifndef QHTTPENGINE_QHTTPENGINE_H +#define QHTTPENGINE_QHTTPENGINE_H + +#include + +/** + * @brief Utility functions + */ +namespace QHttpEngine +{ + +/** + * @brief Split a QByteArray by the provided delimiter + * + * If the delimiter is not present in the QByteArray, a list will be returned + * containing the original QByteArray as its only element. The delimiter must + * not be empty. + */ +QList split(const QByteArray &data, const QByteArray &delim); + +} + +#endif // QHTTPENGINE_QHTTPENGINE_H diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 95beb9c..7667039 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,7 @@ find_package(Qt5Test REQUIRED) set(TESTS + TestQHttpEngine TestQHttpRequest TestQHttpResponse TestQJsonServer diff --git a/tests/TestQHttpEngine.cpp b/tests/TestQHttpEngine.cpp new file mode 100644 index 0000000..fdb3886 --- /dev/null +++ b/tests/TestQHttpEngine.cpp @@ -0,0 +1,85 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Nathan Osman + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include +#include +#include + +#include + +typedef QList QByteArrayList; + +class TestQHttpEngine : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + + void testSplit_data(); + void testSplit(); +}; + +void TestQHttpEngine::testSplit_data() +{ + QTest::addColumn("original"); + QTest::addColumn("delimiter"); + QTest::addColumn("list"); + + QTest::newRow("empty string") + << QByteArray() + << QByteArray(",") + << (QByteArrayList() << ""); + + QTest::newRow("no delimiter") + << QByteArray("a") + << QByteArray(",") + << (QByteArrayList() << "a"); + + QTest::newRow("single-char delimiter") + << QByteArray("a,b,c") + << QByteArray(",") + << (QByteArrayList() << "a" << "b" << "c"); + + QTest::newRow("multi-char delimiter") + << QByteArray("a::b::c") + << QByteArray("::") + << (QByteArrayList() << "a" << "b" << "c"); + + QTest::newRow("empty parts") + << QByteArray("a,,") + << QByteArray(",") + << (QByteArrayList() << "a" << "" << ""); +} + +void TestQHttpEngine::testSplit() +{ + QFETCH(QByteArray, original); + QFETCH(QByteArray, delimiter); + QFETCH(QByteArrayList, list); + + QCOMPARE(QHttpEngine::split(original, delimiter), list); +} + +QTEST_MAIN(TestQHttpEngine) +#include "TestQHttpEngine.moc"