ULXR_API_IMPL(void) FileResource::open(bool do_read) { ULXR_TRACE(ULXR_PCHAR("FileResource::open ") << filename); if (opened) return; reset(); if (do_read) { ULXR_TRACE(ULXR_PCHAR("FileResource::open do_read")); #ifdef __unix__ if (0 == ulxr_access(getLatin1(getFilename()).c_str(), F_OK)) // file exists? #else if (0 == ulxr_access(getLatin1(getFilename()).c_str(), 0)) #endif { ulxr_FILE *ifs = ulxr_fopen (getLatin1(filename).c_str(), "rb"); char buffer [2000]; if (ifs != 0) { while (!ulxr_feof(ifs) && !error) { size_t readed = ulxr_fread(buffer, 1, sizeof(buffer), ifs); if (ulxr_ferror(ifs)) error = true; write(buffer, readed); } ulxr_fclose (ifs); } } } opened = true; }
ULXR_API_IMPL(void) HttpClient::filePUT(const CppString &filename, const CppString &type, const CppString &resource) { ULXR_TRACE(ULXR_PCHAR("filePUT")); if (!protocol->isOpen() ) protocol->open(); FILE *ifs = fopen (getLatin1(filename).c_str(), "rb"); if (ifs == 0) throw Exception(SystemError, ulxr_i18n(ULXR_PCHAR("Cannot open file: "))+filename); struct stat statbuf; if (0 != stat (getLatin1(filename).c_str(), &statbuf) ) throw Exception(SystemError, ulxr_i18n(ULXR_PCHAR("Could not get information about file: "))+filename); sendAuthentication(); protocol->sendRequestHeader(ULXR_PCHAR("PUT"), resource, type, statbuf.st_size); char buffer [ULXR_SEND_BUFFER_SIZE]; long readed; try { while (!feof(ifs)) { readed = fread(buffer, 1, sizeof(buffer), ifs); if (readed < 0) throw Exception(SystemError, ulxr_i18n(ULXR_PCHAR("Could not read from file: "))+filename); protocol->writeBody(buffer, readed); } } catch (...) { fclose(ifs); throw; } // bool eof_reached = feof(ifs); fclose(ifs); BodyProcessor bp; receiveResponse(bp); if (getHttpStatus() != 200) throw ConnectionException(TransportError, getHttpPhrase(), getHttpStatus()); if (!protocol->isPersistent() ) protocol->close(); }
ULXR_API_IMPL(void) FileResource::clear() { ULXR_TRACE(ULXR_PCHAR("FileResource::clear ") << filename); CachedResource::clear(); opened = false; #ifdef __unix__ if (0 == ulxr_access(getLatin1(getFilename()).c_str(), F_OK)) #else if (0 == ulxr_access(getLatin1(getFilename()).c_str(), 0)) #endif if (0 != ulxr_remove(getLatin1(getFilename()).c_str())) error = true; }
ULXR_API_IMPL(void) FileResource::close() { ULXR_TRACE(ULXR_PCHAR("FileResource::close ") << filename); if (!opened) return; // size_t written; const std::string dat = data(); const unsigned len = dat.length(); ulxr_FILE *ifs = ulxr_fopen (getLatin1(filename).c_str(), "wb"); if (ifs != 0) { if (len != 0) { /* written = */ ulxr_fwrite(dat.data(), 1, len, ifs); if (ulxr_ferror(ifs)) error = true; } ulxr_fclose (ifs); } else error = true; opened = false; CachedResource::clear(); }
TcpIpConnection::getHostAdress(const CppString &dom) { unsigned start = 0; if (dom.substr(start, 5) == ULXR_PCHAR("http:")) start += 5; if (dom.substr(start, 2) == ULXR_PCHAR("//")) start += 2; std::size_t slash = dom.find (ULXR_PCHAR("/"), start); if (slash != CppString::npos) pimpl->serverdomain = dom.substr(start, slash-1); else pimpl->serverdomain = dom; #ifndef ULXR_OMIT_REENTRANT_PROTECTOR Mutex::Locker lock(gethostbynameMutex); #endif return gethostbyname(getLatin1(pimpl->serverdomain).c_str() ); }
ULXR_API_IMPL(void) HttpClient::fileGET(const CppString &filename, const CppString &resource) { ULXR_TRACE(ULXR_PCHAR("fileGET")); if (!protocol->isOpen() ) protocol->open(); std::ofstream ofs (getLatin1(filename).c_str(), std::ios::out | std::ios::binary); if (!ofs.good() ) throw Exception(SystemError, ulxr_i18n(ULXR_PCHAR("Cannot create file: "))+filename); sendAuthentication(); protocol->sendRequestHeader(ULXR_PCHAR("GET"), resource, ULXR_PCHAR(""), 0); FileProcessor fp(ofs, filename); receiveResponse(fp); if (getHttpStatus() != 200) throw ConnectionException(TransportError, getHttpPhrase(), getHttpStatus()); if (!protocol->isPersistent() ) protocol->close(); }
ULXR_API_IMPL(void) TcpIpConnection::init(unsigned prt) { ULXR_TRACE(ULXR_PCHAR("init")); #if defined(__WIN32__) && !defined (ULXR_NO_WSA_STARTUP) WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD( 2, 0 ); if (WSAStartup( wVersionRequested, &wsaData) != 0) throw ConnectionException(SystemError, ulxr_i18n(ULXR_PCHAR("Could not initialize Windows sockets: ")) + ULXR_GET_STRING(getErrorString(getLastError())), 500); #endif pimpl->server_data = 0; setTcpNoDelay(false); pimpl->serverdomain = ULXR_PCHAR(""); pimpl->remote_name = ULXR_PCHAR(""); setTimeout(10); pimpl->port = prt; pimpl->hostdata_len = sizeof(pimpl->hostdata); pimpl->remotedata_len = sizeof(pimpl->remotedata); memset(&pimpl->hostdata, 0, sizeof(pimpl->hostdata)); memset(&pimpl->remotedata, 0, sizeof(pimpl->remotedata)); pimpl->hostdata.sin_port = htons(pimpl->port); pimpl->hostdata.sin_family = AF_INET; char buffer [1000]; memset(buffer, 0, sizeof(buffer)); int ret = gethostname(buffer, sizeof(buffer)-1); if (ret != 0) throw ConnectionException(SystemError, ulxr_i18n(ULXR_PCHAR("Could not get host name: ")) + ULXR_GET_STRING(getErrorString(getLastError())), 500); pimpl->host_name = ULXR_GET_STRING(buffer); #if defined(__SUN__) long status = sysinfo(SI_SRPC_DOMAIN ,buffer, sizeof(buffer)-1); if (status == -1) throw ConnectionException(SystemError, ulxr_i18n(ULXR_PCHAR("Could not get domain name: ")) + ULXR_GET_STRING(getErrorString(getLastError())), 500); if (buffer[0] != 0) { pimpl->host_name += ULXR_PCHAR("."); pimpl->host_name += ULXR_GET_STRING(buffer); } #elif defined(__unix__) || defined(__CYGWIN__) ret = getdomainname(buffer, sizeof(buffer)-1); if (ret != 0) throw ConnectionException(SystemError, ulxr_i18n(ULXR_PCHAR("Could not get domain name: ")) + ULXR_GET_STRING(getErrorString(getLastError())), 500); if (buffer[0] != 0) { pimpl->host_name += ULXR_PCHAR("."); pimpl->host_name += ULXR_GET_STRING(buffer); } #elif _WIN32 #ifndef ULXR_OMIT_REENTRANT_PROTECTOR Mutex::Locker lock(gethostbynameMutex); #endif struct hostent *hostEntPtr = gethostbyname(getLatin1(pimpl->host_name).c_str()); if (!hostEntPtr) throw ConnectionException(SystemError, ulxr_i18n(ULXR_PCHAR("Could not get host+domain name: ")) + ULXR_GET_STRING(getErrorString(getLastError())), 500); pimpl->host_name = ULXR_GET_STRING(hostEntPtr->h_name); #else # pragma message ("don't know how to determine the domain name") #endif }
assertionFailed(unsigned in_srcline, const String& in_srcfile, const String &in_message) { assertionFailed(in_srcline, getLatin1(in_srcfile).c_str(), in_message); }