Davix::HttpRequest Class Reference

Http low level request interface. More...

`#include <httprequest.hpp>`

Inheritance diagram for Davix::HttpRequest:

## Public Member Functions

| HttpRequest (Context &context, const Uri &url, DavixError **err)
| HttpRequest constructor with a defined URL.
| HttpRequest (Context &context, const std::string &url, DavixError **err)
| HttpRequest constructor with a defined URL from a string.
| HttpRequest (NEONRequest *req)
| HttpRequest internal usage.
void | addHeaderField (const std::string &field, const std::string &value)
void | setRequestMethod (const std::string &method)
| set the request method ( "GET", "PUT", ... )
void | setParameters (const RequestParams &parameters)
| set the request parameter
int | executeRequest (DavixError **err)
| execute this request completely
void | setRequestBody (const std::string &body)
void | setRequestBody (const void *buffer, dav_size_t len_buff)
void | setRequestBody (int fd, dav_off_t offset, dav_size_t len)
void | setRequestBody (HttpBodyProvider provider, dav_size_t len, void *udata)
void | setRequestBody (ContentProvider &provider)
int | beginRequest (DavixError **err)
| start a multi-part HTTP Request
dav_ssize_t | readBlock (char *buffer, dav_size_t max_size, DavixError **err)
dav_ssize_t | readBlock (std::vector< char > &buffer, dav_size_t max_size, DavixError **err)
dav_ssize_t | readSegment (char *buffer, dav_size_t max_size, DavixError **err)
dav_ssize_t | readToFd (int fd, DavixError **err)
dav_ssize_t | readToFd (int fd, dav_size_t read_size, DavixError **err)
dav_ssize_t | readLine (char *buffer, dav_size_t max_size, DavixError **err)
void | discardBody (DavixError **err)
int | endRequest (DavixError **err)
const char * | getAnswerContent ()
std::vector< char > & | getAnswerContentVec ()
dav_ssize_t | getAnswerSize () const
time_t | getLastModified () const
void | clearAnswerContent ()
int | getRequestCode ()
bool | getAnswerHeader (const std::string &header_name, std::string &value) const
size_t | getAnswerHeaders (HeaderVec &vec_headers) const
| DEPRECATED (HttpCacheToken *extractCacheToken() const)
| DEPRECATED (void useCacheToken(const HttpCacheToken *token))
void | setFlag (const RequestFlag::RequestFlag flag, bool value)
| set a HttpRequest flag
bool | getFlag (const RequestFlag::RequestFlag flag)
| get a HttpRequest flag value

## Detailed Description

Http low level request interface.

HTTPRequest is the main davix class for low level HTTP queries.

HTTPRequest objects are provided by Davix::Context

## Constructor & Destructor Documentation

## HttpRequest() [1/3]

Davix::HttpRequest::HttpRequest | ( | Context & | context,
|  | const Uri & | url,
|  | DavixError ** | err )

HttpRequest constructor with a defined URL.

Parameters

  context | davix context
  url | URL of the resource
  err | Davix error report system

Context c;
DavixError* err = NULL;
HttpRequest myrequest(c, Uri("http://example.org/some_useful_stuff"), &err);

Referenced by HttpRequest().

## HttpRequest() [2/3]

Davix::HttpRequest::HttpRequest | ( | Context & | context,
|  | const std::string & | url,
|  | DavixError ** | err )

HttpRequest constructor with a defined URL from a string.

Parameters

  context | davix context
  url | URL of the resource
  err | Davix error report system

Context c;
DavixError* err = NULL;
HttpRequest myrequest(c, "http://example.org/some_useful_stuff", &err);

## HttpRequest() [3/3]

Davix::HttpRequest::HttpRequest | ( | NEONRequest * | req | ) |

HttpRequest internal usage.

Parameters

  req |

References HttpRequest().

## Member Function Documentation

## addHeaderField()

void Davix::HttpRequest::addHeaderField | ( | const std::string & | field,
|  | const std::string & | value )

add a optional HTTP header request replace an existing one if already exist if the content of value of the header field is empty : remove an existing one

Parameters

  field | header field name
  value | header field value

myrequest.addHeaderField("Accept", "application/metalink4+xml");

## beginRequest()

int Davix::HttpRequest::beginRequest | ( | DavixError ** | err | ) |

start a multi-part HTTP Request

the multi-part HTTP Request of davix should be used for request with a large answer

Parameters

  err | : DavixError error report system

Returns
  return 0 if success, or a negative value if an error occures

myrequest.beginRequest(&err);

## clearAnswerContent()

void Davix::HttpRequest::clearAnswerContent | ( |  | ) |

clear the current result

myrequest.clearAnswerContent();

## DEPRECATED() [1/2]

Davix::HttpRequest::DEPRECATED | ( | HttpCacheToken *extractCacheToken() const |  | ) |

Deprecated
  not in use anymore

## DEPRECATED() [2/2]

Davix::HttpRequest::DEPRECATED | ( | void | useCacheTokenconst HttpCacheToken *token | ) |

Deprecated
  not in use anymore

## discardBody()

void Davix::HttpRequest::discardBody | ( | DavixError ** | err | ) |

discard the response body

Parameters

  err | DavixError error report system

myrequest.discardBody(&err);

## endRequest()

int Davix::HttpRequest::endRequest | ( | DavixError ** | err | ) |

finish a request stated with beginRequest

myrequest.endRequest(&err);

## executeRequest()

int Davix::HttpRequest::executeRequest | ( | DavixError ** | err | ) |

execute this request completely

the answer is accessible with Davix::HttpRequest::getAnswerContent

Parameters

  err | davix error report

Returns
  0 on success

myrequest.executeRequest(&err);

## getAnswerContent()

const char * Davix::HttpRequest::getAnswerContent | ( |  | ) |

return the body of the answer

// into static buffer
char buffer2[255];
buffer2 = myrequest.getAnswerContent();

## getAnswerContentVec()

std::vector< char > & Davix::HttpRequest::getAnswerContentVec | ( |  | ) |

return the body of the answer in a vector

// into dynamically sized buffer
std::vector<char> buffer1;
buffer1 = myrequest.getAnswerContentVec();

## getAnswerHeader()

bool Davix::HttpRequest::getAnswerHeader | ( | const std::string & | header_name,
|  | std::string & | value ) const

get the value associated to a header key in the request answer

Parameters

  header_name | : key of the header field
  value | : reference of the string to set

Returns
  true if this header exist or false if it does not

std::string value;
myrequest.getAnswerHeader(“Content-Type”, &value);
std::cout << “Content-Type is ” << value << std::endl;

## getAnswerHeaders()

size_t Davix::HttpRequest::getAnswerHeaders | ( | HeaderVec & | vec_headers | ) | const

get all the headers associated with this answer

Parameters

  vec_headers | : vector of headers

Returns
  true if this header exist or false if it does not

HeaderVec headers;
myrequest.getAnswerHeaders(headers);
for(HeaderVec::iterator it = headers.begin(), it < headers.end(); ++it){
std::cout << it->first << “: ” << it->second << std::endl;
}

## getAnswerSize()

dav_ssize_t Davix::HttpRequest::getAnswerSize | ( |  | ) | const

get content length

Returns
  content size, return -1 if chunked

dav_ssize_t size;
size = myrequest.getAnswerSize();

## getLastModified()

time_t Davix::HttpRequest::getLastModified | ( |  | ) | const

get last modified time

time_t last_modified;
last_modified = myrequest.getLastModified();

## getRequestCode()

int Davix::HttpRequest::getRequestCode | ( |  | ) |

Returns
  current request code error undefined if executeRequest or beginRequest has not be called before

int code;
code = myrequest.getRequestCode();

## readBlock() [1/2]

dav_ssize_t Davix::HttpRequest::readBlock | ( | char * | buffer,
|  | dav_size_t | max_size,
|  | DavixError ** | err )

read a block of a maximum size bytes in the answer can return < max_size bytes depending of the data available

Parameters

  buffer | : buffer to fill
  max_size | : maximum number of byte to read
  err | : DavixError error report system

Returns
  number of bytes readed

// read max n bytes to static buffer
char buffer[255];
myrequest.readBlock(&buffer, n, &err);
// read to dynamically sized buffer, with max size n
std::vector<char> buffer2;
myrequest.readBlock(&buffer2, n, &err);

## readBlock() [2/2]

dav_ssize_t Davix::HttpRequest::readBlock | ( | std::vector< char > & | buffer,
|  | dav_size_t | max_size,
|  | DavixError ** | err )

read a block of a maximum size bytes in the answer into buffer can return < max_size bytes depending of the data available

Parameters

  buffer | : vector to fill
  max_size | : maximum number of byte to read
  err | : DavixError error report system

Returns
  number of bytes readed

// read max n bytes to static buffer
char buffer[255];
myrequest.readBlock(&buffer, n, &err);
// read to dynamically sized buffer, with max size n
std::vector<char> buffer2;
myrequest.readBlock(&buffer2, n, &err);

## readLine()

dav_ssize_t Davix::HttpRequest::readLine | ( | char * | buffer,
|  | dav_size_t | max_size,
|  | DavixError ** | err )

read a line of text of a maximum size bytes in the answer

Parameters

  buffer | : buffer to fill
  max_size | : maximum number of bytes to read
  err | : DavixError error report system

Returns
  number of bytes readed, if return == max_size -> the line too big

char buffer[255];
myrequest.readLine(&buffer, n, &err);

## readSegment()

dav_ssize_t Davix::HttpRequest::readSegment | ( | char * | buffer,
|  | dav_size_t | max_size,
|  | DavixError ** | err )

read a segment of size bytes, return always max_size excepted if the end of the content is reached

Parameters

  buffer | : vector to fill
  max_size | : maximum number of byte to read
  err | : DavixError error report system

Returns
  number of bytes readed

// readSegment calls readBlock repeatedly until n size is read, or end of answer
char buffer[50*1024];
myrequest.readSegment(&buffer, n, &err);

## readToFd() [1/2]

dav_ssize_t Davix::HttpRequest::readToFd | ( | int | fd,
|  | dav_size_t | read_size,
|  | DavixError ** | err )

write the first 'read_size' first bytes to the given file descriptor

Parameters

  fd | : buffer to fill
  read_size | : number of bytes to read
  err | : DavixError error report system

Returns
  number of bytes read

char buffer[255]
int fd = open(“tmp/myfile”, O_WRONLY | O_CREAT);
// with no size limit
myrequest.readToFd(fd, &err);
// with 100 bytes limit
myrequest.readToFd(fd, 100, &err);

## readToFd() [2/2]

dav_ssize_t Davix::HttpRequest::readToFd | ( | int | fd,
|  | DavixError ** | err )

write the full answer content to the given file descriptor

Parameters

  fd | : buffer to fill
  err | : DavixError error report system

Returns
  number of bytes read

char buffer[255]
int fd = open(“tmp/myfile”, O_WRONLY | O_CREAT);
// with no size limit
myrequest.readToFd(fd, &err);
// with 100 bytes limit
myrequest.readToFd(fd, 100, &err);

## setParameters()

void Davix::HttpRequest::setParameters | ( | const RequestParams & | parameters | ) |

set the request parameter

Parameters

  parameters | Davix Request parameters

define the request parameters, can be used to define parameters such as authentication scheme, timeout or user agent.

## setRequestBody() [1/5]

void Davix::HttpRequest::setRequestBody | ( | const std::string & | body | ) |

set the content of the request from a string an empty string set no request content

Warning
  this string is not duplicated internally for performance reasons

// from a string
std::string content_string
myrequest.setRequestBody(content_string);
// from a buffer
char buffer [255];
// fills buffer with something useful
myrequest.setRequestBody(&buffer, sizeof(buffer));
// from a file descriptor, at offset 100 for 200 bytes
int fd = open(“/tmp/myfile”, O_RDONLY);
myrequest.setRequestBody(fd, 100, 200);
close(fd);

## setRequestBody() [2/5]

void Davix::HttpRequest::setRequestBody | ( | const void * | buffer,
|  | dav_size_t | len_buff )

set the content of the request from a buffer NULL pointer means a empty content

// from a string
std::string content_string
myrequest.setRequestBody(content_string);
// from a buffer
char buffer [255];
// fills buffer with something useful
myrequest.setRequestBody(&buffer, sizeof(buffer));
// from a file descriptor, at offset 100 for 200 bytes
int fd = open(“/tmp/myfile”, O_RDONLY);
myrequest.setRequestBody(fd, 100, 200);
close(fd);

## setRequestBody() [3/5]

void Davix::HttpRequest::setRequestBody | ( | ContentProvider & | provider | ) |

set a content provider object to provide the body of the requests

## setRequestBody() [4/5]

void Davix::HttpRequest::setRequestBody | ( | HttpBodyProvider | provider,
|  | dav_size_t | len,
|  | void * | udata )

set a callback to provide the body of the requests

## setRequestBody() [5/5]

void Davix::HttpRequest::setRequestBody | ( | int | fd,
|  | dav_off_t | offset,
|  | dav_size_t | len )

set the content of the request from a file descriptor start at offset and read a maximum of len bytes

// from a string
std::string content_string
myrequest.setRequestBody(content_string);
// from a buffer
char buffer [255];
// fills buffer with something useful
myrequest.setRequestBody(&buffer, sizeof(buffer));
// from a file descriptor, at offset 100 for 200 bytes
int fd = open(“/tmp/myfile”, O_RDONLY);
myrequest.setRequestBody(fd, 100, 200);
close(fd);

## setRequestMethod()

void Davix::HttpRequest::setRequestMethod | ( | const std::string & | method | ) |

set the request method ( "GET", "PUT", ... )

Parameters

  method | request method

myrequest.setRequestMethod(“GET”);

The documentation for this class was generated from the following file:

- /build/davix-vkLe3H/davix-0.8.10/include/davix/request/httprequest.hpp