Skip to content

Commit fdc7fc8

Browse files
committed
Fixes missing QNetworkReply::NetworkError codes in Qt before 5.3
1 parent 38969bc commit fdc7fc8

File tree

7 files changed

+42
-8
lines changed

7 files changed

+42
-8
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77
This changelog follows the [Keep a Changelog](http://keepachangelog.com) format.
88

99

10+
---
11+
12+
13+
## [1.1.1] - 2018-11-14 ##
14+
15+
### Fixed ###
16+
- Some QNetworkReply::NetworkError codes were not available in Qt before 5.3.
17+
18+
1019
---
1120

1221
## [1.1.0] - 2018-10-06 ##
@@ -37,4 +46,5 @@ Initial (actually unversioned) release.
3746
---
3847

3948

49+
[1.1.1]: https://github.com/j-ulrich/http-status-codes-cpp/releases/tag/1.1.1
4050
[1.1.0]: https://github.com/j-ulrich/http-status-codes-cpp/releases/tag/1.1.0

HttpStatusCodes_C++.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* https://github.com/j-ulrich/http-status-codes-cpp
66
*
7-
* \version 1.1.0
7+
* \version 1.1.1
88
* \author Jochen Ulrich <[email protected]>
99
* \copyright Licensed under Creative Commons CC0 (http://creativecommons.org/publicdomain/zero/1.0/)
1010
*/

HttpStatusCodes_C++11.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* https://github.com/j-ulrich/http-status-codes-cpp
66
*
7-
* \version 1.1.0
7+
* \version 1.1.1
88
* \author Jochen Ulrich <[email protected]>
99
* \copyright Licensed under Creative Commons CC0 (http://creativecommons.org/publicdomain/zero/1.0/)
1010
*/

HttpStatusCodes_C.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* https://github.com/j-ulrich/http-status-codes-cpp
66
*
7-
* \version 1.1.0
7+
* \version 1.1.1
88
* \author Jochen Ulrich <[email protected]>
99
* \copyright Licensed under Creative Commons CC0 (http://creativecommons.org/publicdomain/zero/1.0/)
1010
*/

HttpStatusCodes_Qt.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* https://github.com/j-ulrich/http-status-codes-cpp
66
*
7-
* \version 1.1.0
7+
* \version 1.1.1
88
* \author Jochen Ulrich <[email protected]>
99
* \copyright Licensed under Creative Commons CC0 (http://creativecommons.org/publicdomain/zero/1.0/)
1010
*/
@@ -25,7 +25,7 @@
2525
namespace HttpStatus
2626
{
2727

28-
#if (QT_VERSION >= 0x050800)
28+
#if (QT_VERSION >= QT_VERSION_CHECK(5,8,0))
2929
Q_NAMESPACE
3030
#endif // Qt >= 5.8.0
3131

@@ -119,7 +119,7 @@ enum Code
119119
NotExtended = 510, //!< The policy for accessing the resource has not been met in the request. [RFC 2774]
120120
NetworkAuthenticationRequired = 511 //!< Indicates that the client needs to authenticate to gain network access.
121121
};
122-
#if (QT_VERSION >= 0x050800)
122+
#if (QT_VERSION >= QT_VERSION_CHECK(5,8,0))
123123
Q_ENUM_NS(Code)
124124
#endif // Qt >= 5.8.0
125125

@@ -238,17 +238,21 @@ inline int networkErrorToStatusCode(QNetworkReply::NetworkError error)
238238
case QNetworkReply::ContentNotFoundError: return NotFound; // 404
239239
case QNetworkReply::ContentOperationNotPermittedError: return MethodNotAllowed; // 405
240240
case QNetworkReply::ProxyAuthenticationRequiredError: return ProxyAuthenticationRequired; // 407
241+
#if QT_VERSION >= QT_VERSION_CHECK(5,3,0)
241242
case QNetworkReply::ContentConflictError: return Conflict; // 409
242243
case QNetworkReply::ContentGoneError: return Gone; // 410
243244
case QNetworkReply::InternalServerError: return InternalServerError; // 500
244245
case QNetworkReply::OperationNotImplementedError: return NotImplemented; // 501
245246
case QNetworkReply::ServiceUnavailableError: return ServiceUnavailable; // 503
247+
#endif // Qt >= 5.3.0
246248

247249
// Mapping error codes matching multiple HTTP status codes to a best matching "base" code
248250
case QNetworkReply::NoError: return OK; // 200
249251
case QNetworkReply::ProtocolInvalidOperationError: return BadRequest; // 400
250252
case QNetworkReply::UnknownContentError: return BadRequest; // 400
253+
#if QT_VERSION >= QT_VERSION_CHECK(5,3,0)
251254
case QNetworkReply::UnknownServerError: return InternalServerError; // 500
255+
#endif // Qt >= 5.3.0
252256

253257
/* Other errors do not match any HTTP status code.
254258
* Therefore, we return an invalid code.
@@ -287,21 +291,27 @@ inline QNetworkReply::NetworkError statusCodeToNetworkError(int code)
287291
case NotFound: return QNetworkReply::ContentNotFoundError; // 404
288292
case MethodNotAllowed: return QNetworkReply::ContentOperationNotPermittedError; // 405
289293
case ProxyAuthenticationRequired: return QNetworkReply::ProxyAuthenticationRequiredError; // 407
294+
#if QT_VERSION >= QT_VERSION_CHECK(5,3,0)
290295
case Conflict: return QNetworkReply::ContentConflictError; // 409
291296
case Gone: return QNetworkReply::ContentGoneError; // 410
297+
#endif // Qt >= 5.3.0
292298
case ImATeapot: return QNetworkReply::ProtocolInvalidOperationError; // 418
299+
#if QT_VERSION >= QT_VERSION_CHECK(5,3,0)
293300
case InternalServerError: return QNetworkReply::InternalServerError; // 500
294301
case NotImplemented: return QNetworkReply::OperationNotImplementedError; // 501
295302
case ServiceUnavailable: return QNetworkReply::ServiceUnavailableError; // 503
303+
#endif // Qt >= 5.3.0
296304

297305
default:
298306
break;
299307
}
300308

301309
if (isClientError(code)) // 4xx
302310
return QNetworkReply::UnknownContentError;
311+
#if QT_VERSION >= QT_VERSION_CHECK(5,3,0)
303312
if (isServerError(code)) // 5xx
304313
return QNetworkReply::UnknownServerError;
314+
#endif // Qt >= 5.3.0
305315

306316
// 600 or above
307317
return QNetworkReply::ProtocolFailure;

tests/CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,23 @@ if (Qt5_FOUND)
6565
set(QTNETWORK_LIB Qt5::Network)
6666
endif()
6767
endif()
68-
if (NOT Qt5_FOUND)
68+
if (NOT QTCORE_LIB)
69+
find_package(Qt5Core CONFIG)
70+
if (Qt5Core_FOUND)
71+
set(QTCORE_LIB Qt5::Core)
72+
find_package(Qt5Network CONFIG)
73+
if (Qt5Network_FOUND)
74+
set(QTNETWORK_LIB Qt5::Network)
75+
endif()
76+
endif()
77+
endif()
78+
if (NOT QTCORE_LIB)
6979
find_package(Qt4 COMPONENTS QtCore OPTIONAL_COMPONENTS QtNetwork)
7080
if (Qt4_FOUND)
7181
set(QTCORE_LIB Qt4::QtCore)
7282
if (QT_NETWORK_FOUND)
7383
set(QTNETWORK_LIB Qt4::QtNetwork)
74-
endif()
84+
endif()
7585
endif()
7686
endif()
7787

tests/QtVariantTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ TEST(NetworkErrorMappingTest, testNetworkErrorToStatusCode)
6363
ASSERT_EQ(HttpStatus::networkErrorToStatusCode(QNetworkReply::ContentNotFoundError), HttpStatus::NotFound);
6464
ASSERT_EQ(HttpStatus::networkErrorToStatusCode(QNetworkReply::NoError), HttpStatus::OK);
6565
ASSERT_EQ(HttpStatus::networkErrorToStatusCode(QNetworkReply::UnknownContentError), HttpStatus::BadRequest);
66+
#if QT_VERSION >= QT_VERSION_CHECK(5,3,0)
6667
ASSERT_EQ(HttpStatus::networkErrorToStatusCode(QNetworkReply::UnknownServerError), HttpStatus::InternalServerError);
68+
#endif // Qt >= 5.3.0
6769
ASSERT_EQ(HttpStatus::networkErrorToStatusCode(QNetworkReply::ProtocolFailure), -1);
6870
}
6971

@@ -72,7 +74,9 @@ TEST(NetworkErrorMappingTest, testStatusCodeToNetworkError)
7274
ASSERT_EQ(HttpStatus::statusCodeToNetworkError(HttpStatus::Unauthorized), QNetworkReply::AuthenticationRequiredError);
7375
ASSERT_EQ(HttpStatus::statusCodeToNetworkError(HttpStatus::OK), QNetworkReply::NoError);
7476
ASSERT_EQ(HttpStatus::statusCodeToNetworkError(HttpStatus::URITooLong), QNetworkReply::UnknownContentError);
77+
#if QT_VERSION >= QT_VERSION_CHECK(5,3,0)
7578
ASSERT_EQ(HttpStatus::statusCodeToNetworkError(HttpStatus::InsufficientStorage), QNetworkReply::UnknownServerError);
79+
#endif // Qt >= 5.3.0
7680
ASSERT_EQ(HttpStatus::statusCodeToNetworkError(601), QNetworkReply::ProtocolFailure);
7781
}
7882
#endif // QT_NETWORK_LIB

0 commit comments

Comments
 (0)