hrev45250 adds 3 changesets to branch 'master' old head: 956670089702cab586ca0f916a31778bf620880a new head: f00edeb7e364656cbe11784edd33a6af43d32038 overview: http://cgit.haiku-os.org/haiku/log/?qt=range&q=f00edeb+%5E9566700 ---------------------------------------------------------------------------- d1b6afe: Re-enable the services kit from GSOC 2010 * Use a public domain MD5 implementation when the OpenSSL one is not available * No functional changes 4e607e1: Enable HTTPS connections. Using more modern network classes: * BNetworkAddress instead of BNetAddress * BSocket and BSecureSocket instead of BNetEndpoint f00edeb: Automatically pick port 443 for HTTPS [ Niels Sascha Reedijk <niels.reedijk@xxxxxxxxx> ] ---------------------------------------------------------------------------- 11 files changed, 457 insertions(+), 63 deletions(-) headers/os/net/UrlProtocol.h | 1 + headers/os/net/UrlProtocolDispatchingListener.h | 1 + headers/os/net/UrlProtocolHttp.h | 14 +- .../network/libnetapi/HttpAuthentication.cpp | 24 +- src/kits/network/libnetapi/Jamfile | 26 +- src/kits/network/libnetapi/UrlProtocol.cpp | 5 + .../libnetapi/UrlProtocolDispatchingListener.cpp | 5 + src/kits/network/libnetapi/UrlProtocolHttp.cpp | 94 +++--- src/kits/network/libnetapi/UrlRequest.cpp | 10 +- src/kits/network/libnetapi/md5.c | 295 +++++++++++++++++++ src/kits/network/libnetapi/md5.h | 45 +++ ############################################################################ Commit: d1b6afe678cffdd0d56a9a1dea8071932a568dd7 URL: http://cgit.haiku-os.org/haiku/commit/?id=d1b6afe Author: Niels Sascha Reedijk <niels.reedijk@xxxxxxxxx> Date: Fri May 11 20:19:13 2012 UTC Committer: Hamish Morrison <hamishm53@xxxxxxxxx> Commit-Date: Thu Feb 7 18:05:38 2013 UTC Re-enable the services kit from GSOC 2010 * Use a public domain MD5 implementation when the OpenSSL one is not available * No functional changes ---------------------------------------------------------------------------- diff --git a/headers/os/net/UrlProtocol.h b/headers/os/net/UrlProtocol.h index fdc5472..2d95d8f 100644 --- a/headers/os/net/UrlProtocol.h +++ b/headers/os/net/UrlProtocol.h @@ -21,6 +21,7 @@ public: BUrlResult* result, const char* threadName, const char* protocolName); + virtual ~BUrlProtocol(); // URL protocol required members // TODO: (stippi) I know it's sometimes appealing to have these diff --git a/headers/os/net/UrlProtocolDispatchingListener.h b/headers/os/net/UrlProtocolDispatchingListener.h index b3cdf58..a89c3b9 100644 --- a/headers/os/net/UrlProtocolDispatchingListener.h +++ b/headers/os/net/UrlProtocolDispatchingListener.h @@ -36,6 +36,7 @@ public: BHandler* handler); BUrlProtocolDispatchingListener( const BMessenger& messenger); + virtual ~BUrlProtocolDispatchingListener(); virtual void ConnectionOpened(BUrlProtocol* caller); virtual void HostnameResolved(BUrlProtocol* caller, diff --git a/src/kits/network/libnetapi/HttpAuthentication.cpp b/src/kits/network/libnetapi/HttpAuthentication.cpp index 889beea..606e859 100644 --- a/src/kits/network/libnetapi/HttpAuthentication.cpp +++ b/src/kits/network/libnetapi/HttpAuthentication.cpp @@ -10,11 +10,21 @@ #include <HttpAuthentication.h> #include <cstdlib> -#include <openssl/md5.h> #include <cstdio> #define PRINT(x) printf x +#ifdef OPENSSL_ENABLED +#define HAVE_OPENSSL // Instruct md5.h to include the openssl version +#endif + +extern "C" { +#include "md5.h" +}; + +#ifndef MD5_DIGEST_LENGTH +#define MD5_DIGEST_LENGTH 16 +#endif static const char* kBase64Symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; @@ -361,11 +371,13 @@ BHttpAuthentication::_DigestResponse(const BString& uri, const BString& method) BString BHttpAuthentication::_H(const BString& value) const -{ - unsigned char* hashResult - = MD5(reinterpret_cast<const unsigned char*>(value.String()), - value.Length(), NULL); - +{ + MD5_CTX context; + static unsigned char hashResult[MD5_DIGEST_LENGTH]; + MD5_Init(&context); + MD5_Update(&context, (void *)(value.String()), value.Length()); + MD5_Final(hashResult, &context); + BString result; // TODO: This is slower than it needs to be. If we already know the // final hash string length, we can use diff --git a/src/kits/network/libnetapi/Jamfile b/src/kits/network/libnetapi/Jamfile index ab598f9..427de08 100644 --- a/src/kits/network/libnetapi/Jamfile +++ b/src/kits/network/libnetapi/Jamfile @@ -48,23 +48,25 @@ SharedLibrary libbnetapi.so : # Building of the commented out files has not been completely tested after # integrating the code from the GSoC 2010 "Services Kit" project and doing # some renaming of types, constants and methods. -# HttpAuthentication.cpp -# HttpHeaders.cpp -# HttpForm.cpp + HttpAuthentication.cpp + HttpHeaders.cpp + HttpForm.cpp HttpTime.cpp + md5.c + Url.cpp UrlContext.cpp -# UrlProtocol.cpp -# UrlProtocolAsynchronousListener.cpp -# UrlProtocolDispatchingListener.cpp -# UrlProtocolHttp.cpp # TODO: -> add-on, See above. -# UrlProtocolListener.cpp -# UrlRequest.cpp -# UrlResult.cpp -# UrlSynchronousRequest.cpp + UrlProtocol.cpp + UrlProtocolAsynchronousListener.cpp + UrlProtocolDispatchingListener.cpp + UrlProtocolHttp.cpp # TODO: -> add-on, See above. + UrlProtocolListener.cpp + UrlRequest.cpp + UrlResult.cpp + UrlSynchronousRequest.cpp : - be $(TARGET_NETWORK_LIBS) $(TARGET_LIBSUPC++) $(HAIKU_OPENSSL_LIBS) + be $(TARGET_NETWORK_LIBS) $(TARGET_LIBSTDC++) $(TARGET_LIBSUPC++) $(HAIKU_OPENSSL_LIBS) libshared.a ; diff --git a/src/kits/network/libnetapi/UrlProtocol.cpp b/src/kits/network/libnetapi/UrlProtocol.cpp index 3a3efde..358f641 100644 --- a/src/kits/network/libnetapi/UrlProtocol.cpp +++ b/src/kits/network/libnetapi/UrlProtocol.cpp @@ -44,6 +44,11 @@ BUrlProtocol::BUrlProtocol(const BUrl& url, BUrlProtocolListener* listener, } +BUrlProtocol::~BUrlProtocol() +{ +} + + // #pragma mark URL protocol thread management diff --git a/src/kits/network/libnetapi/UrlProtocolDispatchingListener.cpp b/src/kits/network/libnetapi/UrlProtocolDispatchingListener.cpp index 5bba318..4601f1b 100644 --- a/src/kits/network/libnetapi/UrlProtocolDispatchingListener.cpp +++ b/src/kits/network/libnetapi/UrlProtocolDispatchingListener.cpp @@ -31,6 +31,11 @@ BUrlProtocolDispatchingListener::BUrlProtocolDispatchingListener } +BUrlProtocolDispatchingListener::~BUrlProtocolDispatchingListener() +{ +} + + void BUrlProtocolDispatchingListener::ConnectionOpened(BUrlProtocol* caller) { diff --git a/src/kits/network/libnetapi/md5.c b/src/kits/network/libnetapi/md5.c new file mode 100644 index 0000000..7d43a60 --- /dev/null +++ b/src/kits/network/libnetapi/md5.c @@ -0,0 +1,295 @@ +/* + * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. + * MD5 Message-Digest Algorithm (RFC 1321). + * + * Homepage: + * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 + * + * Author: + * Alexander Peslyak, better known as Solar Designer <solar at openwall.com> + * + * This software was written by Alexander Peslyak in 2001. No copyright is + * claimed, and the software is hereby placed in the public domain. + * In case this attempt to disclaim copyright and place the software in the + * public domain is deemed null and void, then the software is + * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the + * general public under the following terms: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + * There's ABSOLUTELY NO WARRANTY, express or implied. + * + * (This is a heavily cut-down "BSD license".) + * + * This differs from Colin Plumb's older public domain implementation in that + * no exactly 32-bit integer data type is required (any 32-bit or wider + * unsigned integer data type will do), there's no compile-time endianness + * configuration, and the function prototypes match OpenSSL's. No code from + * Colin Plumb's implementation has been reused; this comment merely compares + * the properties of the two independent implementations. + * + * The primary goals of this implementation are portability and ease of use. + * It is meant to be fast, but not as fast as possible. Some known + * optimizations are not included to reduce source code size and avoid + * compile-time configuration. + */ + +#ifndef HAVE_OPENSSL + +#include <string.h> + +#include "md5.h" + +/* + * The basic MD5 functions. + * + * F and G are optimized compared to their RFC 1321 definitions for + * architectures that lack an AND-NOT instruction, just like in Colin Plumb's + * implementation. + */ +#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) +#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y)))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) +#define I(x, y, z) ((y) ^ ((x) | ~(z))) + +/* + * The MD5 transformation for all four rounds. + */ +#define STEP(f, a, b, c, d, x, t, s) \ + (a) += f((b), (c), (d)) + (x) + (t); \ + (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \ + (a) += (b); + +/* + * SET reads 4 input bytes in little-endian byte order and stores them + * in a properly aligned word in host byte order. + * + * The check for little-endian architectures that tolerate unaligned + * memory accesses is just an optimization. Nothing will break if it + * doesn't work. + */ +#if defined(__i386__) || defined(__x86_64__) || defined(__vax__) +#define SET(n) \ + (*(MD5_u32plus *)&ptr[(n) * 4]) +#define GET(n) \ + SET(n) +#else +#define SET(n) \ + (ctx->block[(n)] = \ + (MD5_u32plus)ptr[(n) * 4] | \ + ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \ + ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \ + ((MD5_u32plus)ptr[(n) * 4 + 3] << 24)) +#define GET(n) \ + (ctx->block[(n)]) +#endif + +/* + * This processes one or more 64-byte data blocks, but does NOT update + * the bit counters. There are no alignment requirements. + */ +static void *body(MD5_CTX *ctx, void *data, unsigned long size) +{ + unsigned char *ptr; + MD5_u32plus a, b, c, d; + MD5_u32plus saved_a, saved_b, saved_c, saved_d; + + ptr = data; + + a = ctx->a; + b = ctx->b; + c = ctx->c; + d = ctx->d; + + do { + saved_a = a; + saved_b = b; + saved_c = c; + saved_d = d; + +/* Round 1 */ + STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7) + STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12) + STEP(F, c, d, a, b, SET(2), 0x242070db, 17) + STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22) + STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7) + STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12) + STEP(F, c, d, a, b, SET(6), 0xa8304613, 17) + STEP(F, b, c, d, a, SET(7), 0xfd469501, 22) + STEP(F, a, b, c, d, SET(8), 0x698098d8, 7) + STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12) + STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17) + STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22) + STEP(F, a, b, c, d, SET(12), 0x6b901122, 7) + STEP(F, d, a, b, c, SET(13), 0xfd987193, 12) + STEP(F, c, d, a, b, SET(14), 0xa679438e, 17) + STEP(F, b, c, d, a, SET(15), 0x49b40821, 22) + +/* Round 2 */ + STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5) + STEP(G, d, a, b, c, GET(6), 0xc040b340, 9) + STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14) + STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20) + STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5) + STEP(G, d, a, b, c, GET(10), 0x02441453, 9) + STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14) + STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20) + STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5) + STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9) + STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14) + STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20) + STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5) + STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9) + STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14) + STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20) + +/* Round 3 */ + STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4) + STEP(H, d, a, b, c, GET(8), 0x8771f681, 11) + STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16) + STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23) + STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4) + STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11) + STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16) + STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23) + STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4) + STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11) + STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16) + STEP(H, b, c, d, a, GET(6), 0x04881d05, 23) + STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4) + STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11) + STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16) + STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23) + +/* Round 4 */ + STEP(I, a, b, c, d, GET(0), 0xf4292244, 6) + STEP(I, d, a, b, c, GET(7), 0x432aff97, 10) + STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15) + STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21) + STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6) + STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10) + STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15) + STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21) + STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6) + STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10) + STEP(I, c, d, a, b, GET(6), 0xa3014314, 15) + STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21) + STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6) + STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10) + STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15) + STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21) + + a += saved_a; + b += saved_b; + c += saved_c; + d += saved_d; + + ptr += 64; + } while (size -= 64); + + ctx->a = a; + ctx->b = b; + ctx->c = c; + ctx->d = d; + + return ptr; +} + +void MD5_Init(MD5_CTX *ctx) +{ + ctx->a = 0x67452301; + ctx->b = 0xefcdab89; + ctx->c = 0x98badcfe; + ctx->d = 0x10325476; + + ctx->lo = 0; + ctx->hi = 0; +} + +void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size) +{ + MD5_u32plus saved_lo; + unsigned long used, free; + + saved_lo = ctx->lo; + if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) + ctx->hi++; + ctx->hi += size >> 29; + + used = saved_lo & 0x3f; + + if (used) { + free = 64 - used; + + if (size < free) { + memcpy(&ctx->buffer[used], data, size); + return; + } + + memcpy(&ctx->buffer[used], data, free); + data = (unsigned char *)data + free; + size -= free; + body(ctx, ctx->buffer, 64); + } + + if (size >= 64) { + data = body(ctx, data, size & ~(unsigned long)0x3f); + size &= 0x3f; + } + + memcpy(ctx->buffer, data, size); +} + +void MD5_Final(unsigned char *result, MD5_CTX *ctx) +{ + unsigned long used, free; + + used = ctx->lo & 0x3f; + + ctx->buffer[used++] = 0x80; + + free = 64 - used; + + if (free < 8) { + memset(&ctx->buffer[used], 0, free); + body(ctx, ctx->buffer, 64); + used = 0; + free = 64; + } + + memset(&ctx->buffer[used], 0, free - 8); + + ctx->lo <<= 3; + ctx->buffer[56] = ctx->lo; + ctx->buffer[57] = ctx->lo >> 8; + ctx->buffer[58] = ctx->lo >> 16; + ctx->buffer[59] = ctx->lo >> 24; + ctx->buffer[60] = ctx->hi; + ctx->buffer[61] = ctx->hi >> 8; + ctx->buffer[62] = ctx->hi >> 16; + ctx->buffer[63] = ctx->hi >> 24; + + body(ctx, ctx->buffer, 64); + + result[0] = ctx->a; + result[1] = ctx->a >> 8; + result[2] = ctx->a >> 16; + result[3] = ctx->a >> 24; + result[4] = ctx->b; + result[5] = ctx->b >> 8; + result[6] = ctx->b >> 16; + result[7] = ctx->b >> 24; + result[8] = ctx->c; + result[9] = ctx->c >> 8; + result[10] = ctx->c >> 16; + result[11] = ctx->c >> 24; + result[12] = ctx->d; + result[13] = ctx->d >> 8; + result[14] = ctx->d >> 16; + result[15] = ctx->d >> 24; + + memset(ctx, 0, sizeof(*ctx)); +} + +#endif diff --git a/src/kits/network/libnetapi/md5.h b/src/kits/network/libnetapi/md5.h new file mode 100644 index 0000000..f1a6857 --- /dev/null +++ b/src/kits/network/libnetapi/md5.h @@ -0,0 +1,45 @@ +/* + * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. + * MD5 Message-Digest Algorithm (RFC 1321). + * + * Homepage: + * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 + * + * Author: + * Alexander Peslyak, better known as Solar Designer <solar at openwall.com> + * + * This software was written by Alexander Peslyak in 2001. No copyright is + * claimed, and the software is hereby placed in the public domain. + * In case this attempt to disclaim copyright and place the software in the + * public domain is deemed null and void, then the software is + * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the + * general public under the following terms: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + * There's ABSOLUTELY NO WARRANTY, express or implied. + * + * See md5.c for more information. + */ + +#ifdef HAVE_OPENSSL +#include <openssl/md5.h> +#elif !defined(_MD5_H) +#define _MD5_H + +/* Any 32-bit or wider unsigned integer data type will do */ +typedef unsigned int MD5_u32plus; + +typedef struct { + MD5_u32plus lo, hi; + MD5_u32plus a, b, c, d; + unsigned char buffer[64]; + MD5_u32plus block[16]; +} MD5_CTX; + +extern void MD5_Init(MD5_CTX *ctx); +extern void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size); +extern void MD5_Final(unsigned char *result, MD5_CTX *ctx); + +#endif ############################################################################ Commit: 4e607e1aae9912f08cc6dbcb6d4b1e90b7c62c1f URL: http://cgit.haiku-os.org/haiku/commit/?id=4e607e1 Author: Niels Sascha Reedijk <niels.reedijk@xxxxxxxxx> Date: Sat May 12 07:53:47 2012 UTC Committer: Hamish Morrison <hamishm53@xxxxxxxxx> Commit-Date: Thu Feb 7 18:06:02 2013 UTC Enable HTTPS connections. Using more modern network classes: * BNetworkAddress instead of BNetAddress * BSocket and BSecureSocket instead of BNetEndpoint ---------------------------------------------------------------------------- diff --git a/headers/os/net/UrlProtocolHttp.h b/headers/os/net/UrlProtocolHttp.h index 7542510..c702433 100644 --- a/headers/os/net/UrlProtocolHttp.h +++ b/headers/os/net/UrlProtocolHttp.h @@ -12,17 +12,21 @@ #include <HttpAuthentication.h> #include <HttpForm.h> #include <HttpHeaders.h> -#include <NetEndpoint.h> #include <NetBuffer.h> +#include <NetworkAddress.h> #include <UrlProtocol.h> +class BAbstractSocket; + + class BUrlProtocolHttp : public BUrlProtocol { public: - BUrlProtocolHttp(BUrl& url, + BUrlProtocolHttp(BUrl& url, bool ssl = false, BUrlProtocolListener* listener = NULL, BUrlContext* context = NULL, BUrlResult* result = NULL); + virtual ~BUrlProtocolHttp(); virtual status_t SetOption(uint32 option, void* value); @@ -56,8 +60,8 @@ private: private: - BNetEndpoint fSocket; - BNetAddress fRemoteAddr; + BAbstractSocket* fSocket; + BNetworkAddress fRemoteAddr; int8 fRequestMethod; int8 fHttpVersion; diff --git a/src/kits/network/libnetapi/UrlProtocolHttp.cpp b/src/kits/network/libnetapi/UrlProtocolHttp.cpp index 4971d3b..fdf7646 100644 --- a/src/kits/network/libnetapi/UrlProtocolHttp.cpp +++ b/src/kits/network/libnetapi/UrlProtocolHttp.cpp @@ -1,9 +1,10 @@ /* - * Copyright 2010 Haiku Inc. All rights reserved. + * Copyright 2010-2011 Haiku Inc. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: * Christophe Huriaux, c.huriaux@xxxxxxxxx + * Niels Sascha Reedijk, niels.reedijk@xxxxxxxxx */ @@ -14,6 +15,8 @@ #include <arpa/inet.h> #include <Debug.h> #include <File.h> +#include <Socket.h> +#include <SecureSocket.h> #include <UrlProtocolHttp.h> @@ -28,14 +31,26 @@ static const char* kHttpProtocolThreadStrStatus[ }; -BUrlProtocolHttp::BUrlProtocolHttp(BUrl& url, BUrlProtocolListener* listener, - BUrlContext* context, BUrlResult* result) +BUrlProtocolHttp::BUrlProtocolHttp(BUrl& url, bool ssl, + BUrlProtocolListener* listener, BUrlContext* context, + BUrlResult* result) : BUrlProtocol(url, listener, context, result, "BUrlProtocol.HTTP", "HTTP"), fRequestMethod(B_HTTP_GET), fHttpVersion(B_HTTP_11) { _ResetOptions(); + if (ssl) + fSocket = new BSecureSocket(); + else + fSocket = new BSocket(); + +} + + +BUrlProtocolHttp::~BUrlProtocolHttp() +{ + delete fSocket; } @@ -203,11 +218,7 @@ status_t BUrlProtocolHttp::_ProtocolLoop() { printf("UHP[%p]::{Loop} %s\n", this, fUrl.UrlString().String()); - // Socket initialization - fSocket = BNetEndpoint(SOCK_STREAM); - if (fSocket.InitCheck() != B_OK) - return B_PROT_SOCKET_ERROR; - + // Initialize the request redirection loop int8 maxRedirs = fOptMaxRedirs; bool newRequest; @@ -325,23 +336,20 @@ BUrlProtocolHttp::_ResolveHostName() fUrl.UrlString().String()); if (fUrl.HasPort()) - fRemoteAddr = BNetAddress(fUrl.Host(), fUrl.Port()); + fRemoteAddr = BNetworkAddress(fUrl.Host(), fUrl.Port()); else - fRemoteAddr = BNetAddress(fUrl.Host(), 80); + fRemoteAddr = BNetworkAddress(fUrl.Host(), 80); if (fRemoteAddr.InitCheck() != B_OK) return false; - - char addr[15]; - struct in_addr ip; - fRemoteAddr.GetAddr(ip); - inet_ntop(AF_INET, &ip, addr, 15); //! ProtocolHook:HostnameResolved if (fListener != NULL) - fListener->HostnameResolved(this, const_cast<const char*>(addr)); + fListener->HostnameResolved(this, + const_cast<const char*>(fRemoteAddr.ToString().String())); - _EmitDebug(B_URL_PROTOCOL_DEBUG_TEXT, "Hostname resolved to: %s", addr); + _EmitDebug(B_URL_PROTOCOL_DEBUG_TEXT, "Hostname resolved to: %s", + fRemoteAddr.ToString().String()); return true; } @@ -352,11 +360,10 @@ BUrlProtocolHttp::_MakeRequest() { _EmitDebug(B_URL_PROTOCOL_DEBUG_TEXT, "Connection to %s.", fUrl.Authority().String()); - status_t connectError = fSocket.Connect(fRemoteAddr); + status_t connectError = fSocket->Connect(fRemoteAddr); if (connectError != B_OK) { - _EmitDebug(B_URL_PROTOCOL_DEBUG_ERROR, "Connection error: %s.", - fSocket.ErrorStr()); + _EmitDebug(B_URL_PROTOCOL_DEBUG_ERROR, "Socket connection error."); return B_PROT_CONNECTION_FAILED; } @@ -368,7 +375,7 @@ BUrlProtocolHttp::_MakeRequest() _EmitDebug(B_URL_PROTOCOL_DEBUG_TEXT, "Sending request (size=%d)", fOutputBuffer.Length()); - fSocket.Send(fOutputBuffer.String(), fOutputBuffer.Length()); + fSocket->Write(fOutputBuffer.String(), fOutputBuffer.Length()); fOutputBuffer.Truncate(0); _EmitDebug(B_URL_PROTOCOL_DEBUG_TEXT, "Request sent."); @@ -377,14 +384,14 @@ BUrlProtocolHttp::_MakeRequest() fOutputBuffer = fOptPostFields->RawData(); _EmitDebug(B_URL_PROTOCOL_DEBUG_TRANSFER_OUT, fOutputBuffer.String()); - fSocket.Send(fOutputBuffer.String(), fOutputBuffer.Length()); + fSocket->Write(fOutputBuffer.String(), fOutputBuffer.Length()); } else { for (BHttpForm::Iterator it = fOptPostFields->GetIterator(); const BHttpFormData* currentField = it.Next(); ) { _EmitDebug(B_URL_PROTOCOL_DEBUG_TRANSFER_OUT, it.MultipartHeader().String()); - fSocket.Send(it.MultipartHeader().String(), + fSocket->Write(it.MultipartHeader().String(), it.MultipartHeader().Length()); switch (currentField->Type()) { @@ -393,7 +400,7 @@ BUrlProtocolHttp::_MakeRequest() break; case B_HTTPFORM_STRING: - fSocket.Send(currentField->String().String(), + fSocket->Write(currentField->String().String(), currentField->String().Length()); break; @@ -406,22 +413,22 @@ BUrlProtocolHttp::_MakeRequest() readSize = upFile.Read(readBuffer, 1024); while (readSize > 0) { - fSocket.Send(readBuffer, readSize); + fSocket->Write(readBuffer, readSize); readSize = upFile.Read(readBuffer, 1024); } } break; case B_HTTPFORM_BUFFER: - fSocket.Send(currentField->Buffer(), + fSocket->Write(currentField->Buffer(), currentField->BufferSize()); break; } - fSocket.Send("\r\n", 2); + fSocket->Write("\r\n", 2); } - fSocket.Send(fOptPostFields->GetMultipartFooter().String(), + fSocket->Write(fOptPostFields->GetMultipartFooter().String(), fOptPostFields->GetMultipartFooter().Length()); } } else if ((fRequestMethod == B_HTTP_POST || fRequestMethod == B_HTTP_PUT) @@ -436,19 +443,17 @@ BUrlProtocolHttp::_MakeRequest() char hexSize[16]; size_t hexLength = sprintf(hexSize, "%ld", read); - fSocket.Send(hexSize, hexLength); - fSocket.Send("\r\n", 2); - fSocket.Send(outputTempBuffer, read); - fSocket.Send("\r\n", 2); + fSocket->Write(hexSize, hexLength); + fSocket->Write("\r\n", 2); + fSocket->Write(outputTempBuffer, read); + fSocket->Write("\r\n", 2); } } - fSocket.Send("0\r\n\r\n", 5); + fSocket->Write("0\r\n\r\n", 5); } fOutputBuffer.Truncate(0, true); - fSocket.SetNonBlocking(false); - fStatusReceived = false; fHeadersReceived = false; @@ -466,7 +471,9 @@ BUrlProtocolHttp::_MakeRequest() while (!fQuit && !(receiveEnd && parseEnd)) { if (!receiveEnd) { - bytesRead = fSocket.Receive(fInputBuffer, receiveBufferSize); + fSocket->WaitForReadable(); + BNetBuffer chunk(receiveBufferSize); + bytesRead = fSocket->Read(chunk.Data(), receiveBufferSize); if (bytesRead < 0) { readError = true; @@ -474,6 +481,8 @@ BUrlProtocolHttp::_MakeRequest() continue; } else if (bytesRead == 0) receiveEnd = true; + + fInputBuffer.AppendData(chunk.Data(), bytesRead); } else bytesRead = 0; @@ -563,7 +572,7 @@ BUrlProtocolHttp::_MakeRequest() parseEnd = (fInputBuffer.Size() == 0); } - fSocket.Close(); + fSocket->Disconnect(); if (readError) return B_PROT_READ_FAILED; @@ -681,7 +690,7 @@ BUrlProtocolHttp::_CopyChunkInBuffer(char** buffer, ssize_t* bytesReceived) chunkHeader.Length() - semiColonIndex); chunkSize = strtol(chunkHeader.String(), NULL, 16); - PRINT(("BHP[%p] Chunk %s=%d\n", this, chunkHeader.String(), chunkSize)); + PRINT(("BHP[%p] Chunk %s=%ld\n", this, chunkHeader.String(), chunkSize)); if (chunkSize == 0) { fContentReceived = true; } diff --git a/src/kits/network/libnetapi/UrlRequest.cpp b/src/kits/network/libnetapi/UrlRequest.cpp index aec723f..e04ce13 100644 --- a/src/kits/network/libnetapi/UrlRequest.cpp +++ b/src/kits/network/libnetapi/UrlRequest.cpp @@ -136,7 +136,15 @@ BUrlRequest::Identify() fUrlProtocol = NULL; if (fUrl.Protocol() == "http") { - fUrlProtocol = new(std::nothrow) BUrlProtocolHttp(fUrl, fListener, fContext, &fResult); + fUrlProtocol = new(std::nothrow) BUrlProtocolHttp(fUrl, false, + fListener, fContext, + &fResult); + fReady = true; + return B_OK; + } else if (fUrl.Protocol() == "https") { + fUrlProtocol = new(std::nothrow) BUrlProtocolHttp(fUrl, true, + fListener, fContext, + &fResult); fReady = true; return B_OK; } ############################################################################ Revision: hrev45250 Commit: f00edeb7e364656cbe11784edd33a6af43d32038 URL: http://cgit.haiku-os.org/haiku/commit/?id=f00edeb Author: Niels Sascha Reedijk <niels.reedijk@xxxxxxxxx> Date: Sat May 12 10:37:07 2012 UTC Committer: Hamish Morrison <hamishm53@xxxxxxxxx> Commit-Date: Thu Feb 7 18:06:45 2013 UTC Automatically pick port 443 for HTTPS ---------------------------------------------------------------------------- diff --git a/headers/os/net/UrlProtocolHttp.h b/headers/os/net/UrlProtocolHttp.h index c702433..1902827 100644 --- a/headers/os/net/UrlProtocolHttp.h +++ b/headers/os/net/UrlProtocolHttp.h @@ -23,6 +23,7 @@ class BAbstractSocket; class BUrlProtocolHttp : public BUrlProtocol { public: BUrlProtocolHttp(BUrl& url, bool ssl = false, + const char *protocolName = "HTTP", BUrlProtocolListener* listener = NULL, BUrlContext* context = NULL, BUrlResult* result = NULL); @@ -62,6 +63,7 @@ private: private: BAbstractSocket* fSocket; BNetworkAddress fRemoteAddr; + bool fSSL; int8 fRequestMethod; int8 fHttpVersion; diff --git a/src/kits/network/libnetapi/UrlProtocolHttp.cpp b/src/kits/network/libnetapi/UrlProtocolHttp.cpp index fdf7646..4093649 100644 --- a/src/kits/network/libnetapi/UrlProtocolHttp.cpp +++ b/src/kits/network/libnetapi/UrlProtocolHttp.cpp @@ -32,10 +32,11 @@ static const char* kHttpProtocolThreadStrStatus[ BUrlProtocolHttp::BUrlProtocolHttp(BUrl& url, bool ssl, - BUrlProtocolListener* listener, BUrlContext* context, - BUrlResult* result) + const char *protocolName, BUrlProtocolListener* listener, + BUrlContext* context, BUrlResult* result) : - BUrlProtocol(url, listener, context, result, "BUrlProtocol.HTTP", "HTTP"), + BUrlProtocol(url, listener, context, result, "BUrlProtocol.HTTP", protocolName), + fSSL(ssl), fRequestMethod(B_HTTP_GET), fHttpVersion(B_HTTP_11) { @@ -337,9 +338,13 @@ BUrlProtocolHttp::_ResolveHostName() if (fUrl.HasPort()) fRemoteAddr = BNetworkAddress(fUrl.Host(), fUrl.Port()); - else - fRemoteAddr = BNetworkAddress(fUrl.Host(), 80); - + else { + if (fSSL) + fRemoteAddr = BNetworkAddress(fUrl.Host(), 443); + else + fRemoteAddr = BNetworkAddress(fUrl.Host(), 80); + } + if (fRemoteAddr.InitCheck() != B_OK) return false; diff --git a/src/kits/network/libnetapi/UrlRequest.cpp b/src/kits/network/libnetapi/UrlRequest.cpp index e04ce13..975cb48 100644 --- a/src/kits/network/libnetapi/UrlRequest.cpp +++ b/src/kits/network/libnetapi/UrlRequest.cpp @@ -136,13 +136,13 @@ BUrlRequest::Identify() fUrlProtocol = NULL; if (fUrl.Protocol() == "http") { - fUrlProtocol = new(std::nothrow) BUrlProtocolHttp(fUrl, false, + fUrlProtocol = new(std::nothrow) BUrlProtocolHttp(fUrl, false, "HTTP", fListener, fContext, &fResult); fReady = true; return B_OK; } else if (fUrl.Protocol() == "https") { - fUrlProtocol = new(std::nothrow) BUrlProtocolHttp(fUrl, true, + fUrlProtocol = new(std::nothrow) BUrlProtocolHttp(fUrl, true, "HTTPS", fListener, fContext, &fResult); fReady = true;