Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ environment:
VCVARS_COMMANDLINE: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86'
CMAKE_INSTALL_PATH: 'C:\Program Files (x86)\CMake'

- job_name: VS 2013 x86, Qt 5.6
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
CMAKE_GENERATOR: Visual Studio 12 2013
QT_DIR: C:\Qt\5.6\msvc2013
VCVARS_COMMANDLINE: '"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86'
CMAKE_INSTALL_PATH: 'C:\Program Files (x86)\CMake'

cache:
- C:\.hunter -> .appveyor.yml, **\CMakeLists.txt, **\*.cmake

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
/bin/bash -c "
cd /workspace
&& g++ --version
&& qmake --version
&& qmake6 --version
&& cd tests
&& mkdir _build
&& cd _build
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ This changelog follows the [Keep a Changelog](http://keepachangelog.com) format.

## [Unreleased]

### Added ###
- [#28] Added code `Invalid` (-1).
- [#28]{Qt C++11} Qt C++11 variant which uses `enum class` for the codes.

### Changed ###
- {Qt} `networkErrorToStatusCode()` now returns a `Code` instead of `int`.

### Removed ###
- Testing with Visual Studio 2013 because of outdated CMake.


---

Expand Down
2 changes: 2 additions & 0 deletions HttpStatusCodes_C++.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace HttpStatus
*/
enum Code
{
Invalid = -1, //!< An invalid status code.

/*####### 1xx - Informational #######*/
/* Indicates an interim response for communicating connection status
* or request progress prior to completing the requested action and
Expand Down
2 changes: 2 additions & 0 deletions HttpStatusCodes_C++11.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace HttpStatus
*/
enum class Code
{
Invalid = -1, //!< An invalid status code.

/*####### 1xx - Informational #######*/
/* Indicates an interim response for communicating connection status
* or request progress prior to completing the requested action and
Expand Down
2 changes: 2 additions & 0 deletions HttpStatusCodes_C.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
enum HttpStatus_Code
{
HttpStatus_Invalid = -1, //!< An invalid status code.

/*####### 1xx - Informational #######*/
/* Indicates an interim response for communicating connection status
* or request progress prior to completing the requested action and
Expand Down
14 changes: 8 additions & 6 deletions HttpStatusCodes_Qt.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* \copyright Licensed under Creative Commons CC0 (http://creativecommons.org/publicdomain/zero/1.0/)
*/

#ifndef HTTPSTATUSCODES_QT5_H_
#define HTTPSTATUSCODES_QT5_H_
#ifndef HTTPSTATUSCODES_QT_H_
#define HTTPSTATUSCODES_QT_H_

#include <QString>
#include <QObject>
Expand All @@ -33,6 +33,8 @@ namespace HttpStatus
*/
enum Code
{
Invalid = -1, //!< An invalid status code.

/*####### 1xx - Informational #######*/
/* Indicates an interim response for communicating connection status
* or request progress prior to completing the requested action and
Expand Down Expand Up @@ -232,12 +234,12 @@ inline QString reasonPhrase(int code)
* \return The HTTP status code corresponding to the given \p error if there is one.\n
* If there is no exact matching status code, the first code from the best matching status
* code class is returned (`200`, `400` or `500`).\n
* If no matching status code exists, an invalid status code (`-1`) is returned.
* If no matching status code exists, Invalid (`-1`) is returned.
* This is typically the case for errors concerning the OSI layers below HTTP.
*
* \sa [statusCodeFromHttp() in qhttpthreaddelegate.cpp](http://code.qt.io/cgit/qt/qtbase.git/tree/src/network/access/qhttpthreaddelegate.cpp#n57)
*/
inline int networkErrorToStatusCode(QNetworkReply::NetworkError error)
inline Code networkErrorToStatusCode(QNetworkReply::NetworkError error)
{
switch (error)
{
Expand Down Expand Up @@ -267,7 +269,7 @@ inline int networkErrorToStatusCode(QNetworkReply::NetworkError error)
* Therefore, we return an invalid code.
*/
default:
return -1;
return Invalid;
}
}

Expand Down Expand Up @@ -342,4 +344,4 @@ class DummyQGadget



#endif /* HTTPSTATUSCODES_QT5_8_H_ */
#endif /* HTTPSTATUSCODES_QT_H_ */
391 changes: 391 additions & 0 deletions HttpStatusCodes_Qt_C++11.h

Large diffs are not rendered by default.

39 changes: 25 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ Initially, the data was taken from [for-GET/know-your-http-well](https://github.

## Variants ##

| Variant | Name Scoping | Status Codes Type | Reason Phrases Type |
|----------------------------------|------------------------|------------------------------------------------------------------------------------------------|---------------------|
| [C](HttpStatusCodes_C.h) | Prefix `HttpStatus_` | `enum HttpStatus_Code` | `const char*` |
| [C++](HttpStatusCodes_C++.h) | Namespace `HttpStatus` | `enum Code` | `std::string` |
| [C++11](HttpStatusCodes_C++11.h) | Namespace `HttpStatus` | `enum class Code` | `std::string` |
| [Qt](HttpStatusCodes_Qt.h) | Namespace `HttpStatus` | `enum Code`<br>When using Qt 5.8 or later: registered in meta type system using `Q_ENUM_NS()` | `QString` |
| Variant | Name Scoping | Status Codes Type | Reason Phrases Type |
|----------------------------------------|------------------------|-----------------------------------------------------------------------------------------------------|---------------------|
| [C](HttpStatusCodes_C.h) | Prefix `HttpStatus_` | `enum HttpStatus_Code` | `const char*` |
| [C++](HttpStatusCodes_C++.h) | Namespace `HttpStatus` | `enum Code` | `std::string` |
| [C++11](HttpStatusCodes_C++11.h) | Namespace `HttpStatus` | `enum class Code` | `std::string` |
| [Qt](HttpStatusCodes_Qt.h) | Namespace `HttpStatus` | `enum Code`<br>When using Qt 5.8 or later: registered in meta type system using `Q_ENUM_NS()` | `QString` |
| [Qt C++11](HttpStatusCodes_Qt_C++11.h) | Namespace `HttpStatus` | `enum class Code`<br>When using Qt 5.8 or later: registered in meta type system using `Q_ENUM_NS()` | `QString` |


> Note regarding Qt variant: Oldest tested Qt version was Qt 5.2.0 with MinGW 4.8. However, should be working with any Qt 5.x version.
Expand Down Expand Up @@ -72,6 +73,7 @@ might be undefined behavior.
```c
enum HttpStatus_Code
{
HttpStatus_Invalid = -1,
HttpStatus_OK = 200,
HttpStatus_NotFound = 404
// ...
Expand All @@ -83,6 +85,7 @@ enum HttpStatus_Code
namespace HttpStatus {
enum class Code
{
Invalid = -1,
OK = 200,
NotFound = 404
// ...
Expand All @@ -95,6 +98,7 @@ enum class Code
namespace HttpStatus {
enum Code
{
Invalid = -1,
OK = 200,
NotFound = 404
// ...
Expand Down Expand Up @@ -124,14 +128,18 @@ Non-standard error codes are status codes with a value of 600 or higher.
Returns `0` otherwise.

##### Other Variants #####
> **Note:** The C++11 variant also provides overloads for `HttpStatus::Code`. So there is no need to cast.

```c++
bool HttpStatus::isInformational( int code );
bool HttpStatus::isSuccessful( int code );
bool HttpStatus::isRedirection( int code );
bool HttpStatus::isClientError( int code );
bool HttpStatus::isServerError( int code );

bool HttpStatus::isInformational( Code code ); // C++11 variants only
bool HttpStatus::isSuccessful( Code code ); // C++11 variants only
bool HttpStatus::isRedirection( Code code ); // C++11 variants only
bool HttpStatus::isClientError( Code code ); // C++11 variants only
bool HttpStatus::isServerError( Code code ); // C++11 variants only
```
Return `true` if the given _code_ belongs to the corresponding class of status codes (see [RFC7231](https://tools.ietf.org/html/rfc7231#section-6)).
Return `false` otherwise.
Expand All @@ -140,6 +148,7 @@ Return `false` otherwise.

```c++
bool HttpStatus::isError( int code );
bool HttpStatus::isError( Code code ); // C++11 variants only
```
Returns `true` if the given _code_ is either a client error, a server error or any non-standard error code.
Non-standard error codes are status codes with a value of 600 or higher.
Expand All @@ -155,37 +164,39 @@ const char* HttpStatus_reasonPhrase( int code );
Returns the HTTP reason phrase string corresponding to the given _code_.

##### C++/C++11 Variants #####
> **Note:** The C++11 variant also provides an overload for `HttpStatus::Code`. So there is no need to cast.
```c++
std::string HttpStatus::reasonPhrase( int code );
std::string HttpStatus::reasonPhrase( Code code ); // C++11 variants only
```
Returns the HTTP reason phrase string corresponding to the given _code_.

##### Qt Variant #####
##### Qt Variants #####
```c++
QString HttpStatus::reasonPhrase( int code );
QString HttpStatus::reasonPhrase( Code code ); // C++11 variant only
```
Returns the HTTP reason phrase string corresponding to the given _code_.


### Conversion Functions ###

##### C++11 Variant #####
##### C++11 Variants #####
```c++
int HttpStatus::toInt( HttpStatus::Code code );
```
Returns the integer value corresponding to a given a _code_.
This is a convenience function as replacement for a `static_cast<int>()`.

##### Qt Variant #####
##### Qt Variants #####
```c++
int HttpStatus::networkErrorToStatusCode( QNetworkReply::NetworkError error );
Code HttpStatus::networkErrorToStatusCode( QNetworkReply::NetworkError error );
```
Returns the HTTP status code corresponding to the given _error_ if there is one.
Otherwise, `-1` is returned.
Otherwise, `Code::Invalid` (`-1`) is returned.

```c++
QNetworkReply::NetworkError HttpStatus::statusCodeToNetworkError( int code );
QNetworkReply::NetworkError HttpStatus::statusCodeToNetworkError( Code code ); // C++11 variant only
```
Returns the `QNetworkReply::NetworkError` corresponding to the given _code_ if there is one.
For codes where there is no exact match, the best matching "catch all" code (`QNetworkReply::NoError`,
Expand Down
5 changes: 5 additions & 0 deletions tests/C++11VariantTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Cpp11VariantTests
//####### Enum Test #######
TEST(EnumTest, testEnumValues)
{
ASSERT_EQ(static_cast<int>(HttpStatus::Code::Invalid), -1);
ASSERT_EQ(static_cast<int>(HttpStatus::Code::OK), 200);
ASSERT_EQ(static_cast<int>(HttpStatus::Code::NotFound), 404);
ASSERT_EQ(static_cast<int>(HttpStatus::Code::InternalServerError), 500);
Expand All @@ -18,13 +19,15 @@ TEST(EnumTest, testEnumValues)
//####### Reason Phrase Test #######
TEST(ReasonPhraseTest, testEnumOverload)
{
ASSERT_EQ(HttpStatus::reasonPhrase(HttpStatus::Code::Invalid), std::string());
ASSERT_EQ(HttpStatus::reasonPhrase(HttpStatus::Code::OK), std::string("OK"));
ASSERT_EQ(HttpStatus::reasonPhrase(HttpStatus::Code::NotFound), std::string("Not Found"));
ASSERT_EQ(HttpStatus::reasonPhrase(HttpStatus::Code::InternalServerError), std::string("Internal Server Error"));
}

TEST(ReasonPhraseTest, testIntegerOverload)
{
ASSERT_EQ(HttpStatus::reasonPhrase(static_cast<int>(HttpStatus::Code::Invalid)), std::string());
ASSERT_EQ(HttpStatus::reasonPhrase(static_cast<int>(HttpStatus::Code::Accepted)), std::string("Accepted"));
ASSERT_EQ(HttpStatus::reasonPhrase(static_cast<int>(HttpStatus::Code::MethodNotAllowed)), std::string("Method Not Allowed"));
ASSERT_EQ(HttpStatus::reasonPhrase(static_cast<int>(HttpStatus::Code::ServiceUnavailable)), std::string("Service Unavailable"));
Expand All @@ -35,6 +38,7 @@ TEST(ReasonPhraseTest, testIntegerOverload)

INSTANTIATE_TEST_CASE_P(DefaultInstance, CategoryTesterTest, ::testing::Values(
// // code // info // success // redir // clientErr // serverErr // error
CategoryTesterParams(HttpStatus::Code::Invalid, false, false, false, false, false, false),
CategoryTesterParams(HttpStatus::Code::Processing, true, false, false, false, false, false),
CategoryTesterParams(HttpStatus::Code::ResetContent, false, true, false, false, false, false),
CategoryTesterParams(HttpStatus::Code::Found, false, false, true, false, false, false),
Expand All @@ -48,6 +52,7 @@ CategoryTesterParams(HttpStatus::Code::HTTPVersionNotSupported, false, fals
TEST(ConversionFunctionTest, testToInt)
{
ASSERT_EQ(HttpStatus::toInt(HttpStatus::Code::SwitchingProtocols), static_cast<int>(HttpStatus::Code::SwitchingProtocols));
ASSERT_EQ(HttpStatus::toInt(HttpStatus::Code::Invalid), static_cast<int>(HttpStatus::Code::Invalid));
ASSERT_EQ(HttpStatus::toInt(HttpStatus::Code::OK), static_cast<int>(HttpStatus::Code::OK));
ASSERT_EQ(HttpStatus::toInt(HttpStatus::Code::NotExtended), static_cast<int>(HttpStatus::Code::NotExtended));
}
Expand Down
4 changes: 4 additions & 0 deletions tests/C++VariantTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace CppVariantTests
//####### Enum Test #######
TEST(EnumTest, testEnumValues)
{
ASSERT_EQ(HttpStatus::Invalid, -1);
ASSERT_EQ(HttpStatus::OK, 200);
ASSERT_EQ(HttpStatus::NotFound, 404);
ASSERT_EQ(HttpStatus::InternalServerError, 500);
Expand All @@ -18,13 +19,15 @@ TEST(EnumTest, testEnumValues)
//####### Reason Phrase Test #######
TEST(ReasonPhraseTest, testEnumParameter)
{
ASSERT_EQ(HttpStatus::reasonPhrase(HttpStatus::Invalid), std::string());
ASSERT_EQ(HttpStatus::reasonPhrase(HttpStatus::OK), std::string("OK"));
ASSERT_EQ(HttpStatus::reasonPhrase(HttpStatus::NotFound), std::string("Not Found"));
ASSERT_EQ(HttpStatus::reasonPhrase(HttpStatus::InternalServerError), std::string("Internal Server Error"));
}

TEST(ReasonPhraseTest, testIntegerParameter)
{
ASSERT_EQ(HttpStatus::reasonPhrase(static_cast<int>(HttpStatus::Invalid)), std::string());
ASSERT_EQ(HttpStatus::reasonPhrase(static_cast<int>(HttpStatus::Created)), std::string("Created"));
ASSERT_EQ(HttpStatus::reasonPhrase(static_cast<int>(HttpStatus::Unauthorized)), std::string("Unauthorized"));
ASSERT_EQ(HttpStatus::reasonPhrase(static_cast<int>(HttpStatus::GatewayTimeout)), std::string("Gateway Timeout"));
Expand All @@ -34,6 +37,7 @@ TEST(ReasonPhraseTest, testIntegerParameter)
//####### Category Tester Test #######
INSTANTIATE_TEST_CASE_P(DefaultInstance, CategoryTesterTest, ::testing::Values(
// // code // info // success // redir // clientErr // serverErr // error
CategoryTesterParams(HttpStatus::Invalid, false, false, false, false, false, false),
CategoryTesterParams(HttpStatus::SwitchingProtocols, true, false, false, false, false, false),
CategoryTesterParams(HttpStatus::NoContent, false, true, false, false, false, false),
CategoryTesterParams(HttpStatus::SeeOther, false, false, true, false, false, false),
Expand Down
Loading