// Parse message bool request::parse(const binary_data & dt_in, binary_data * dt_remain) { size_t commandend_pos, urlend_pos; binary_data _command_string; // Parse message if(!message::parse(dt_in, dt_remain)) return false; // Get Command if ((commandend_pos = m_title.find(const_space))== binary_data::npos) OONET_THROW_EXCEPTION(ExceptionWrongFormat, "This is not an http request message"); _command_string = m_title.sub_data(0, commandend_pos); if (_command_string.size() == 0) OONET_THROW_EXCEPTION(ExceptionWrongFormat, "This is not an http request message"); else if (_command_string == const_get) // GET m_req_method = REQUEST_GET; else if (_command_string == const_post) // POST m_req_method = REQUEST_POST; else if (_command_string == const_head) // HEAD m_req_method = REQUEST_HEAD; else if (_command_string == const_put) // PUT m_req_method = REQUEST_PUT; else if (_command_string == const_delete) // DELETE m_req_method = REQUEST_DELETE; else if (_command_string == const_options) // OPTIONS m_req_method = REQUEST_OPTIONS; else if (_command_string == const_trace) // TRACE m_req_method = REQUEST_TRACE; else if (_command_string == const_connect) // CONNECT m_req_method = REQUEST_CONNECT; else // CUSTOM (extended) { m_req_method = REQUEST_CUSTOM; m_custom_method = _command_string; } // Get URL commandend_pos ++; // Start one position lower if ((urlend_pos = m_title.find(const_space, commandend_pos)) == binary_data::npos) OONET_THROW_EXCEPTION(ExceptionWrongFormat, "This is not an http request message"); m_uri = url(title().sub_data(commandend_pos, urlend_pos - commandend_pos).to_string()); if (m_uri.full() == "") OONET_THROW_EXCEPTION(ExceptionWrongFormat, "This is not an http request message"); // Get version urlend_pos ++; m_http_version = m_title.get_from(urlend_pos); if ((m_http_version != const_http_ver1_1) && (m_http_version != const_http_ver1_0)) OONET_THROW_EXCEPTION(ExceptionWrongFormat, "This is not an http request message"); return true; }
void mutex::lock(ulong tm_timeoutms) { DWORD dwResult; // Wait for locking dwResult = WaitForSingleObject(mutex_h, tm_timeoutms); // Check error if (dwResult == WAIT_TIMEOUT) OONET_THROW_EXCEPTION(ExceptionTimeOut, "TimeOut waiting to lock mutex!"); else if (dwResult != WAIT_OBJECT_0) OONET_THROW_EXCEPTION(ExceptionSystemError, "Unable to lock mutex!"); }
// Wait for a semaphore for predefined time void semaphore::wait(ulong tm_timeoutms) { DWORD dwResult; // Wait for locking dwResult = WaitForSingleObject(sem_h, tm_timeoutms); // Check error if (dwResult == WAIT_TIMEOUT) OONET_THROW_EXCEPTION(ExceptionTimeOut, "Semaphore was abandoned"); else if (dwResult != WAIT_OBJECT_0) OONET_THROW_EXCEPTION(ExceptionSystemError, "Unable to wait for semaphore!"); }
// Constructor mutex::mutex() { mutex_h = CreateMutex(NULL, // No special Access Rights FALSE, // Don't lock it at the initialization NULL // Unamed mutex ); // Check if mutex created succesfully if (mutex_h == NULL) OONET_THROW_EXCEPTION(ExceptionSystemError, "Unknown error trying to create mutex!"); }
// Get the address from list const host_inet & host_resolver::get_host(int a) const { // Check if there is that address if (a >= qu_addresses) { OONET_THROW_EXCEPTION(ExceptionNotFound, "Requested address out of address-list limits!"); return host_inet::ANY; } return a_address_list[a]; }
// Default constructor host_resolver::host_resolver(const string & hostname) { struct hostent *p_temp; struct in_addr addr; // Initialize info of host s_official_name = hostname; qu_addresses = 0; // Try to resolve by dns. p_temp = gethostbyname(hostname.c_str()); if (p_temp != NULL) { // Found by dns // Count addresses qu_addresses = 0; while (p_temp->h_addr_list[++qu_addresses] != NULL); // Copy one by one addresses for(int i = 0;i < qu_addresses;i++) a_address_list[i] = host_inet(ntohl(*(ulong *)p_temp->h_addr_list[i])); // Copy official name if (p_temp->h_name) s_official_name = p_temp->h_name; return; } //Else check if the string contains the ip in human-readable way. if ((addr.s_addr = inet_addr(hostname.c_str())) != 0xffffffff) { // It was an ip in string format. qu_addresses = 1; a_address_list[0] = host_inet(ntohl((ulong)addr.s_addr)); return; } // Error not found OONET_THROW_EXCEPTION(ExceptionServerNotFound, string("Cannot resolve host's address: ") + hostname); }
void mutex::unlock() { // Release mutex if (0 == ReleaseMutex(mutex_h)) OONET_THROW_EXCEPTION(ExceptionSystemError, "Unable to unlock mutex!"); }