16 #ifndef _HttpMessage_h_ 17 #define _HttpMessage_h_ 22 #include <os/OsDefs.h> 26 #include <os/OsSocket.h> 27 #include <os/OsTimeLog.h> 28 #include <os/OsMsgQ.h> 29 #include <os/OsAtomics.h> 30 #include <utl/UtlDList.h> 33 #define HTTP_NAME_VALUE_DELIMITER ':' 34 #define HEADER_LINE_PART_DELIMITER ' ' 35 #define END_OF_LINE_DELIMITOR "\r\n" 37 #define PASSWORD_SECRET "xpressa" 39 #define HTTP_DEFAULT_SOCKET_BUFFER_SIZE 10240 41 #define HTTP_LONG_INT_CHARS 20 43 #define HTTP_PROTOCOL_VERSION "HTTP/1.0" 44 #define HTTP_PROTOCOL_VERSION_1_1 "HTTP/1.1" 47 #define HTTP_GET_METHOD "GET" 48 #define HTTP_PUT_METHOD "PUT" 49 #define HTTP_POST_METHOD "POST" 52 #define HTTP_OK_CODE 200 53 #define HTTP_OK_TEXT "OK" 54 #define HTTP_MOVED_PERMANENTLY_CODE 301 55 #define HTTP_MOVED_PERMANENTLY_TEXT "Moved Permanently" 56 #define HTTP_MOVED_TEMPORARILY_CODE 302 57 #define HTTP_MOVED_TEMPORARILY_TEXT "Moved Temporatily" 58 #define HTTP_UNAUTHORIZED_CODE 401 59 #define HTTP_UNAUTHORIZED_TEXT "Unauthorized" 60 #define HTTP_FORBIDDEN_CODE 403 61 #define HTTP_FORBIDDEN_TEXT "Forbidden" 62 #define HTTP_FILE_NOT_FOUND_CODE 404 63 #define HTTP_FILE_NOT_FOUND_TEXT "File Not Found" 64 #define HTTP_PROXY_UNAUTHORIZED_CODE 407 65 #define HTTP_PROXY_UNAUTHORIZED_TEXT "Proxy Authentication Required" 66 #define HTTP_UNSUPPORTED_METHOD_CODE 501 67 #define HTTP_UNSUPPORTED_METHOD_TEXT "Not Implemented" 68 #define HTTP_OUT_OF_RESOURCES_CODE 503 69 #define HTTP_OUT_OF_RESOURCES_TEXT "Out of Resources" 72 #define HTTP_ACCEPT_LANGUAGE_FIELD "ACCEPT-LANGUAGE" 73 #define HTTP_ACCEPT_ENCODING_FIELD "ACCEPT-ENCODING" 74 #define HTTP_AUTHORIZATION_FIELD "AUTHORIZATION" 75 #define HTTP_DATE_FIELD "DATE" 76 #define HTTP_CONTENT_DISPOSITION_FIELD "CONTENT-DISPOSITION" 77 #define HTTP_CONTENT_TRANSFER_ENCODING_FIELD "CONTENT-TRANSFER-ENCODING" 78 #define HTTP_CONTENT_LENGTH_FIELD "CONTENT-LENGTH" 79 #define HTTP_CONTENT_TYPE_FIELD "CONTENT-TYPE" 80 #define HTTP_CONTENT_ID_FIELD "CONTENT-ID" 81 #define HTTP_LOCATION_FIELD "LOCATION" 82 #define HTTP_PROXY_AUTHENTICATE_FIELD "PROXY-AUTHENTICATE" 83 #define HTTP_PROXY_AUTHORIZATION_FIELD "PROXY-AUTHORIZATION" 84 #define HTTP_REFRESH_FIELD "REFRESH" 85 #define HTTP_USER_AGENT_FIELD "USER-AGENT" 86 #define HTTP_WWW_AUTHENTICATE_FIELD "WWW-AUTHENTICATE" 87 #define HTTP_HOST_FIELD "HOST" 88 #define HTTP_ACCEPT_FIELD "ACCEPT" 89 #define HTTP_CONNECTION_FIELD "CONNECTION" 95 #define HTTP_BASIC_AUTHENTICATION "Basic" 96 #define HTTP_DIGEST_AUTHENTICATION "Digest" 97 #define HTTP_AUTHENTICATION_ALGORITHM_TOKEN "algorithm" 98 # define HTTP_MD5_ALGORITHM "MD5" 99 # define HTTP_MD5_SESSION_ALGORITHM "MD5-sess" 100 #define HTTP_AUTHENTICATION_CNONCE_TOKEN "cnonce" 101 #define HTTP_AUTHENTICATION_DOMAIN_TOKEN "domain" 102 #define HTTP_AUTHENTICATION_REALM_TOKEN "realm" 103 #define HTTP_AUTHENTICATION_NONCE_TOKEN "nonce" 104 #define HTTP_AUTHENTICATION_NONCE_COUNT_TOKEN "nc" 105 #define HTTP_AUTHENTICATION_OPAQUE_TOKEN "opaque" 106 #define HTTP_AUTHENTICATION_QOP_TOKEN "qop" 107 # define HTTP_QOP_AUTH "auth" 108 # define HTTP_QOP_AUTH_INTEGRITY "auth-int" 109 #define HTTP_AUTHENTICATION_USERNAME_TOKEN "username" 110 #define HTTP_AUTHENTICATION_URI_TOKEN "uri" 111 #define HTTP_AUTHENTICATION_RESPONSE_TOKEN "response" 114 #define HTTP_CONTENT_TRANSFER_ENCODING_BINARY "binary" 115 #define HTTP_CONTENT_TRANSFER_ENCODING_BASE64 "base64" 118 #define HTTP_CONTENT_TYPE_PARAM_ACCESS_TYPE "access-type" 119 #define HTTP_CONTENT_TYPE_PARAM_EXPIRATION "expiration" 120 #define HTTP_CONTENT_TYPE_PARAM_HASH "hash" 121 #define HTTP_CONTENT_TYPE_PARAM_SIZE "size" 122 #define HTTP_CONTENT_TYPE_PARAM_URL "url" 136 class OsConnectionSocket;
230 int get(
Url& httpUrl,
231 int maxWaitMilliSeconds,
232 bool bPersistent=
true);
240 int get(
Url& httpUrl,
242 int maxWaitMilliSeconds,
243 bool bPersistent=
false);
262 OsStatus
get(
Url& httpUrl,
263 int maxWaitMilliSeconds,
265 void* optionalData = NULL,
266 OsConnectionSocket** socket = NULL) ;
289 int read(OsSocket* inSocket,
291 UtlString* externalBuffer = NULL,
292 int maxContentLength = 6000000);
296 int readHeader(OsSocket* inSocket, UtlString& buffer);
306 UtlBoolean
write(OsSocket* outSocket)
const;
313 static void unescape(UtlString& escapedText);
317 static void escapeChars(UtlString& unEscapedText, UtlString& tobeEscapedChars);
320 static void escape(UtlString& unEscapedText);
323 static void escapeOneChar(UtlString& unEscapedText,
char tobeEscapedChar);
338 void parseMessage(
const char* messageBytes,
int byteCount);
341 void parseBody(
const char* messageBodyBytes,
int byteCount);
346 UtlDList& headerNameValues);
351 static int findHeaderEnd(
const char* messageBytes,
int messageLength);
368 const char* subfield1,
369 const char* subfield2);
403 void setSendProtocol(OsSocket::IpProtocolSocketType protocol = OsSocket::TCP);
453 const char*
getHeaderValue(
int index,
const char* name = NULL)
const;
463 void setHeaderValue(
const char* name,
const char* newValue,
int index = 0);
496 const char* accessType = NULL,
497 const char* expirationDate = NULL,
498 const char* url = NULL,
500 const char* hash = NULL);
502 UtlBoolean
getContentType(UtlString* contentType, UtlHashMap* parameters = NULL)
const;
512 void setRefresh(
int seconds,
const char* refreshUrl = NULL);
546 void getBytes(UtlString* bytes,
int* length)
const;
552 int authorizationEntity)
const;
558 UtlString* algorithm,
560 int authorizationEntity)
const;
563 const char* nonce,
const char* opaque,
564 const char* domain = NULL,
572 const char* authenticationField)
const;
575 const char * AuthorizeValue,
576 UtlBoolean otherAuthentications = TRUE);
579 const char* password,
583 const char* authenticationScheme,
584 const char* authenticationRealm,
585 const char* authenticationNonce = NULL,
586 const char* authenticationOpaque = NULL,
587 const char* authenticationDomain = NULL);
594 int authorizationEntity)
const;
597 int authorizationEntity);
602 UtlString* password)
const;
605 const char* password)
const;
612 const char* response,
613 const char* algorithm,
618 int authorizationEntity);
621 UtlString* realm = NULL,
622 UtlString* nonce = NULL,
623 UtlString* opaque = NULL,
624 UtlString* response = NULL,
625 UtlString* uri = NULL,
627 int index = 0)
const;
631 const char* password,
632 UtlString& userPasswordDigest);
635 const char* algorithm,
642 const char* bodyDigest,
643 UtlString* responseToken);
646 const char* password,
649 const char* thisMessageMethod = NULL,
650 const char* thisMessageUri = NULL,
655 const char* thisMessageMethod = NULL,
656 const char* thisMessageUri = NULL)
const;
664 const char* statusText);
683 const char* protocol);
706 int& numberBytesChecked,
721 long transportTimeStamp;
722 int lastResendDuration;
723 OsSocket::IpProtocolSocketType transportProtocol;
725 UtlBoolean mFirstSent;
726 UtlString mSendAddress;
728 OsMsgQ* mpResponseListenerQueue;
729 void* mResponseListenerData;
735 NameValuePair* getHeaderField(
int index,
const char* name = NULL)
const;
742 #endif // _HttpMessage_h_ int getResendDuration() const
Definition: HttpMessage.cpp:1861
const char * getHeaderValue(int index, const char *name=NULL) const
Get the value of the header field (i.e. second header line).
Definition: HttpMessage.cpp:2021
UtlBoolean getDigestAuthorizationData(UtlString *user, UtlString *realm=NULL, UtlString *nonce=NULL, UtlString *opaque=NULL, UtlString *response=NULL, UtlString *uri=NULL, int authorizationEntity=HttpMessage::PROXY, int index=0) const
Definition: HttpMessage.cpp:2828
void incrementTimesSent()
Definition: HttpMessage.cpp:1871
void getRequestProtocol(UtlString *protocol) const
Get this request's application layer protocol type.
Definition: HttpMessage.cpp:2581
virtual void setRequestUnauthorized(const HttpMessage *request, const char *authenticationScheme, const char *authenticationRealm, const char *authenticationNonce=NULL, const char *authenticationOpaque=NULL, const char *authenticationDomain=NULL)
Definition: HttpMessage.cpp:3363
UtlBoolean mHeaderCacheClean
Definition: HttpMessage.h:715
HttpEndpointEnum
Definition: HttpMessage.h:196
void setTimesSent(int times=0)
Definition: HttpMessage.cpp:1876
static void cannonizeToken(UtlString &token)
Converts the given token to initial cap capitialization in the style used in RFC 822 header names...
Definition: HttpMessage.cpp:1737
static void escapeOneChar(UtlString &unEscapedText, char tobeEscapedChar)
Selectively URL escape the text if the char is tobeEscapedChar.
Definition: HttpMessage.cpp:1689
UtlBoolean getAcceptField(UtlString &acceptValue) const
Definition: HttpMessage.cpp:2333
static int findHeaderEnd(const char *messageBytes, int messageLength)
returns: the number of bytes in the message buffer which constitue the message header ...
Definition: HttpMessage.cpp:405
void getRequestMethod(UtlString *method) const
Get this request's method.
Definition: HttpMessage.cpp:2570
static UtlBoolean isWholeMessage(const char *messageBuffer, int bufferLength, int &numberBytesChecked, int &contentLength)
Definition: HttpMessage.cpp:3552
UtlBoolean getAuthenticationScheme(UtlString *scheme, int authorizationEntity) const
Definition: HttpMessage.cpp:2598
UtlBoolean getAuthorizationUser(UtlString *userId) const
Definition: HttpMessage.cpp:2761
Definition: HttpMessage.h:198
int getCountHeaderFields(const char *name=NULL) const
Find the number of occurrences of header fields with the given name.
Definition: HttpMessage.cpp:1940
int readBody(OsSocket *inSocket, int length, GetDataCallbackProc callbackProc, void *optionalData)
Read the body of the specified data and deliever via the callback proc.
Definition: HttpMessage.cpp:1064
void setLocationField(const char *locationField)
Definition: HttpMessage.cpp:2359
void resetTransport()
Definition: HttpMessage.cpp:1909
void getAcceptLanguageField(UtlString *acceptLanaguageFieldValue) const
Definition: HttpMessage.cpp:2344
void setDateField()
Sets the date field to the current time.
Definition: HttpMessage.cpp:2259
void setTransportTime(long timeStamp)
Definition: HttpMessage.cpp:1838
UtlBoolean getContentType(UtlString *contentType, UtlHashMap *parameters=NULL) const
Definition: HttpMessage.cpp:2136
void setAuthenticationData(const char *scheme, const char *realm, const char *nonce, const char *opaque, const char *domain=NULL, enum HttpEndpointEnum authEntity=SERVER)
Definition: HttpMessage.cpp:2623
void dumpTimeLog() const
Definition: HttpMessage.cpp:1793
void setAcceptLanguageField(const char *acceptLanaguageFieldValue)
Definition: HttpMessage.cpp:2354
void setResponseFirstHeaderLine(const char *protocol, int statusCode, const char *statusText)
Definition: HttpMessage.cpp:2527
UtlBoolean getAuthenticationData(UtlString *scheme, UtlString *realm, UtlString *nonce, UtlString *opaque, UtlString *algorithm, UtlString *qop, int authorizationEntity) const
Definition: HttpMessage.cpp:2696
UtlDList mNameValues
Definition: HttpMessage.h:713
int read(OsSocket *inSocket, int bufferSize=HTTP_DEFAULT_SOCKET_BUFFER_SIZE, UtlString *externalBuffer=NULL, int maxContentLength=6000000)
Read HTTP message from socket.
Definition: HttpMessage.cpp:1107
HTTP message container.
Definition: HttpMessage.h:191
static void buildBasicAuthorizationCookie(const char *user, const char *password, UtlString *cookie)
Definition: HttpMessage.cpp:3344
#define HTTP_DEFAULT_SOCKET_BUFFER_SIZE
Definition: HttpMessage.h:39
void changeRequestUri(const char *newUri)
Change this request's URI.
Definition: HttpMessage.cpp:2588
void setResponseListenerData(void *requestListenerData)
Sets the data item to pass to the request listener.
Definition: HttpMessage.cpp:1935
UtlBoolean removeHeader(const char *name, int index)
Definition: HttpMessage.cpp:2050
long getTransportTime() const
Definition: HttpMessage.cpp:1851
static OsAtomicInt smHttpMessageCount
Definition: HttpMessage.h:356
HttpMessage & operator=(const HttpMessage &rhs)
Assignment operator.
Definition: HttpMessage.cpp:201
void setBody(HttpBody *newBody)
Attach the body section of the message.
Definition: HttpMessage.cpp:2127
void setContentLength(int contentLength)
Definition: HttpMessage.cpp:2277
UtlBoolean getAuthorizationScheme(UtlString *scheme) const
Definition: HttpMessage.cpp:3400
UtlBoolean getBasicAuthorizationData(UtlString *encodedCookie) const
Definition: HttpMessage.cpp:3411
static int parseHeaders(const char *headerBytes, int messageLength, UtlDList &headerNameValues)
returns: the number of bytes parsed
Definition: HttpMessage.cpp:455
UtlBoolean isFirstSend() const
Definition: HttpMessage.cpp:3560
UtlBoolean verifyMd5Authorization(const char *userId, const char *password, const char *nonce, const char *realm, const char *thisMessageMethod=NULL, const char *thisMessageUri=NULL, enum HttpEndpointEnum authEntity=SERVER) const
Definition: HttpMessage.cpp:3205
int readHeader(OsSocket *inSocket, UtlString &buffer)
Will read bytes off the socket until the header of the message is believed received.
Definition: HttpMessage.cpp:1004
void parseBody(const char *messageBodyBytes, int byteCount)
Parses the bytes into a single or multipart body.
Definition: HttpMessage.cpp:369
const HttpBody * getBody() const
Get the body section of the message.
Definition: HttpMessage.cpp:2122
void setRequestFirstHeaderLine(const char *method, const char *uri, const char *protocol)
Definition: HttpMessage.cpp:2556
void addHeaderField(const char *name, const char *value)
Adds a new header line.
Definition: HttpMessage.cpp:2102
int getContentLength() const
Definition: HttpMessage.cpp:2268
const char * getFirstHeaderLine() const
Definition: HttpMessage.cpp:1805
static void unescape(UtlString &escapedText)
URL unescape the text to human readable form.
Definition: HttpMessage.cpp:1542
void getUserAgentField(UtlString *userAgentFieldValue) const
Definition: HttpMessage.cpp:2284
void insertHeaderField(const char *name, const char *value, int index=0)
Inserts a new header line, shifting existing headers at and after the secified location down by one...
Definition: HttpMessage.cpp:2111
Definition: NameValuePair.h:35
UtlBoolean verifyBasicAuthorization(const char *user, const char *password) const
Definition: HttpMessage.cpp:3497
void setDigestAuthorizationData(const char *user, const char *realm, const char *nonce, const char *uri, const char *response, const char *algorithm, const char *cnonce, const char *opaque, const char *qop, int nonceCount, int authorizationEntity)
Definition: HttpMessage.cpp:2957
static void escape(UtlString &unEscapedText)
URL sscape the text.
Definition: HttpMessage.cpp:1651
void setFirstHeaderLine(const char *newHeaderLine)
Set the header line.
Definition: HttpMessage.cpp:1810
void setHeaderValue(const char *name, const char *newValue, int index=0)
Sets the value of an existing header field.
Definition: HttpMessage.cpp:2034
int getResponseStatusCode() const
Get this response's status code.
Definition: HttpMessage.cpp:2541
static void buildMd5UserPasswordDigest(const char *user, const char *realm, const char *password, UtlString &userPasswordDigest)
Definition: HttpMessage.cpp:3080
void getResponseStatusText(UtlString *text) const
Get this response's status code text.
Definition: HttpMessage.cpp:2549
HttpMessage(const char *messageBytes=NULL, int byteCount=-1)
Construct from a string.
Definition: HttpMessage.cpp:75
UtlBoolean getAuthenticationField(int index, enum HttpEndpointEnum authEntity, const char *authenticationField) const
Definition: HttpMessage.cpp:2918
void setResponseListenerQueue(OsMsgQ *requestListenerQueue)
Sets the queue on which responses from the same transaction are deposited.
Definition: HttpMessage.cpp:1925
const int HttpMessageRetries
Definition: HttpMessage.h:128
static void buildMd5Digest(const char *userPasswordDigest, const char *algorithm, const char *nonce, const char *cnonce, int nonceCount, const char *qop, const char *method, const char *uri, const char *bodyDigest, UtlString *responseToken)
Definition: HttpMessage.cpp:3119
void setSendProtocol(OsSocket::IpProtocolSocketType protocol=OsSocket::TCP)
Definition: HttpMessage.cpp:1881
void setSendAddress(const char *address, int port)
Definition: HttpMessage.cpp:1896
OsMsgQ * getResponseListenerQueue() const
Gets the queue on which responses from the same transaction are deposited.
Definition: HttpMessage.cpp:1920
void setFirstSent()
Definition: HttpMessage.cpp:1891
UtlBoolean getAuthorizationField(UtlString *authenticationField, int authorizationEntity) const
Definition: HttpMessage.cpp:2805
int parseFirstLine(const char *messageBytes, int byteCount)
Parse the first line of the request or response.
Definition: HttpMessage.cpp:283
void parseMessage(const char *messageBytes, int byteCount)
Parse the message from a byte buffer.
Definition: HttpMessage.cpp:321
void touchTransportTime()
Definition: HttpMessage.cpp:1843
void setContentType(const char *contentTypeString, const char *accessType=NULL, const char *expirationDate=NULL, const char *url=NULL, int size=-1, const char *hash=NULL)
Definition: HttpMessage.cpp:2187
void getBytes(UtlString *bytes, int *length) const
Get the bytes for the compete message.
Definition: HttpMessage.cpp:2364
int getTimesSent() const
Definition: HttpMessage.cpp:1866
void getFirstHeaderLinePart(int partIndex, UtlString *part, char separator=HEADER_LINE_PART_DELIMITER) const
Get part of header line.
Definition: HttpMessage.cpp:2480
void * getResponseListenerData() const
Gets the data item to pass to the request listener.
Definition: HttpMessage.cpp:1930
void getResponseProtocol(UtlString *protocol) const
Get this response's message protocol.
Definition: HttpMessage.cpp:2536
#define HEADER_LINE_PART_DELIMITER
Definition: HttpMessage.h:34
Definition: HttpMessage.h:199
void getRequestUri(UtlString *uri) const
Get this request's URI.
Definition: HttpMessage.cpp:2576
void setUserAgentField(const char *userAgentFieldValue)
Definition: HttpMessage.cpp:2294
URL parser and constructor.
Definition: Url.h:90
class to contain an HTTP body
Definition: HttpBody.h:55
UtlBoolean getDateField(long *epochDate) const
Definition: HttpMessage.cpp:2313
void getSendAddress(UtlString *address, int *port) const
Definition: HttpMessage.cpp:1903
UtlString mFirstHeaderLine
Definition: HttpMessage.h:714
void setRefresh(int seconds, const char *refreshUrl=NULL)
Definition: HttpMessage.cpp:2299
void addAuthenticationField(const char *authenticationField, enum HttpEndpointEnum authType)
Definition: HttpMessage.cpp:2901
UtlBoolean(* GetDataCallbackProc)(char *pData, int iLength, void *pOptionalData, HttpMessage *pMsg)
Callback method used as part of HttpMessage::get. If the callback method returns FALSE, then the retrieve is aborted.
Definition: HttpMessage.h:142
void logTimeEvent(const char *eventName)
Definition: HttpMessage.cpp:1786
virtual ~HttpMessage()
Destructor.
Definition: HttpMessage.cpp:168
void setResendDuration(int resendMSec)
Used for transport to track reliably the last duration waited before resending.
Definition: HttpMessage.cpp:1856
static int getHttpMessageCount()
Definition: HttpMessage.cpp:1800
UtlBoolean write(OsSocket *outSocket) const
Write message to socket.
Definition: HttpMessage.cpp:1531
void setBasicAuthorization(const char *user, const char *password, int authorizationEntity)
Definition: HttpMessage.cpp:3376
OsSocket::IpProtocolSocketType getSendProtocol() const
Definition: HttpMessage.cpp:1886
static void escapeChars(UtlString &unEscapedText, UtlString &tobeEscapedChars)
Selectively URL escape the text if they are contained in tobeEscapedChars.
Definition: HttpMessage.cpp:1725