http.cc

来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· CC 代码 · 共 2,361 行 · 第 1/5 页

CC
2,361 行
字号
  const char* buf = static_cast<const char*>(_buf);  while ( nbytes > 0 )  {    int n = TCPSlaveBase::write(buf, nbytes);    if ( n <= 0 )    {      // remote side closed connection ?      if ( n == 0 )        break;      // a valid exception(s) occurred, let's retry...      if (n < 0 && ((errno == EINTR) || (errno == EAGAIN)))        continue;      // some other error occurred ?      return -1;    }    nbytes -= n;    buf += n;    bytes_sent += n;  }  return bytes_sent;}void HTTPProtocol::setRewindMarker(){  m_rewindCount = 0;}void HTTPProtocol::rewind(){  m_linePtrUnget = m_rewindBuf,  m_lineCountUnget = m_rewindCount;  m_rewindCount = 0;}char *HTTPProtocol::gets (char *s, int size){  int len=0;  char *buf=s;  char mybuf[2]={0,0};  while (len < size)  {    read(mybuf, 1);    if (m_bEOF)      break;    if (m_rewindCount < sizeof(m_rewindBuf))       m_rewindBuf[m_rewindCount++] = *mybuf;    if (*mybuf == '\r') // Ignore!      continue;    if ((*mybuf == '\n') || !*mybuf)      break;    *buf++ = *mybuf;    len++;  }  *buf=0;  return s;}ssize_t HTTPProtocol::read (void *b, size_t nbytes){  ssize_t ret = 0;  if (m_lineCountUnget > 0)  {    ret = ( nbytes < m_lineCountUnget ? nbytes : m_lineCountUnget );    m_lineCountUnget -= ret;    memcpy(b, m_linePtrUnget, ret);    m_linePtrUnget += ret;    return ret;  }  if (m_lineCount > 0)  {    ret = ( nbytes < m_lineCount ? nbytes : m_lineCount );    m_lineCount -= ret;    memcpy(b, m_linePtr, ret);    m_linePtr += ret;    return ret;  }  if (nbytes == 1)  {    ret = read(m_lineBuf, 1024); // Read into buffer    m_linePtr = m_lineBuf;    if (ret <= 0)    {      m_lineCount = 0;      return ret;    }    m_lineCount = ret;    return read(b, 1); // Read from buffer  }  do  {    ret = TCPSlaveBase::read( b, nbytes);    if (ret == 0)      m_bEOF = true;  } while ((ret == -1) && (errno == EAGAIN || errno == EINTR));  return ret;}void HTTPProtocol::httpCheckConnection(){  kdDebug(7113) << "(" << m_pid << ") HTTPProtocol::httpCheckConnection: " <<                                   " Socket status: " << m_iSock <<                                      " Keep Alive: " << m_bKeepAlive <<                                           " First: " << m_bFirstRequest << endl;  if ( !m_bFirstRequest && (m_iSock != -1) )  {     bool closeDown = false;     if ( !isConnectionValid())     {        kdDebug(7113) << "(" << m_pid << ") Connection lost!" << endl;        closeDown = true;     }     else if ( m_request.method != HTTP_GET )     {        closeDown = true;     }     else if ( !m_state.doProxy && !m_request.doProxy )     {        if (m_state.hostname != m_request.hostname ||            m_state.port != m_request.port ||            m_state.user != m_request.user ||            m_state.passwd != m_request.passwd)          closeDown = true;     }     else     {        // Keep the connection to the proxy.        if ( !(m_request.doProxy && m_state.doProxy) )          closeDown = true;     }     if (closeDown)        httpCloseConnection();  }  // Let's update our current state  m_state.hostname = m_request.hostname;  m_state.encoded_hostname = m_request.encoded_hostname;  m_state.port = m_request.port;  m_state.user = m_request.user;  m_state.passwd = m_request.passwd;  m_state.doProxy = m_request.doProxy;}bool HTTPProtocol::httpOpenConnection(){  int errCode;  QString errMsg;  kdDebug(7113) << "(" << m_pid << ") HTTPProtocol::httpOpenConnection" << endl;  setBlockConnection( true );  // kio_http uses its own proxying:  KSocks::self()->disableSocks();  if ( m_state.doProxy )  {    QString proxy_host = m_proxyURL.host();    int proxy_port = m_proxyURL.port();    kdDebug(7113) << "(" << m_pid << ") Connecting to proxy server: "                  << proxy_host << ", port: " << proxy_port << endl;    infoMessage( i18n("Connecting to %1...").arg(m_state.hostname) );    setConnectTimeout( m_proxyConnTimeout );    if ( !connectToHost(proxy_host, proxy_port, false) )    {      if (userAborted()) {        error(ERR_NO_CONTENT, "");        return false;      }      switch ( connectResult() )      {        case IO_LookupError:          errMsg = proxy_host;          errCode = ERR_UNKNOWN_PROXY_HOST;          break;        case IO_TimeOutError:          errMsg = i18n("Proxy %1 at port %2").arg(proxy_host).arg(proxy_port);          errCode = ERR_SERVER_TIMEOUT;          break;        default:          errMsg = i18n("Proxy %1 at port %2").arg(proxy_host).arg(proxy_port);          errCode = ERR_COULD_NOT_CONNECT;      }      error( errCode, errMsg );      return false;    }  }  else  {    // Apparently we don't want a proxy.  let's just connect directly    setConnectTimeout(m_remoteConnTimeout);    if ( !connectToHost(m_state.hostname, m_state.port, false ) )    {      if (userAborted()) {        error(ERR_NO_CONTENT, "");        return false;      }      switch ( connectResult() )      {        case IO_LookupError:          errMsg = m_state.hostname;          errCode = ERR_UNKNOWN_HOST;          break;        case IO_TimeOutError:          errMsg = i18n("Connection was to %1 at port %2").arg(m_state.hostname).arg(m_state.port);          errCode = ERR_SERVER_TIMEOUT;          break;        default:          errCode = ERR_COULD_NOT_CONNECT;          if (m_state.port != m_iDefaultPort)            errMsg = i18n("%1 (port %2)").arg(m_state.hostname).arg(m_state.port);          else            errMsg = m_state.hostname;      }      error( errCode, errMsg );      return false;    }  }  // Set our special socket option!!  int on = 1;  (void) setsockopt( m_iSock, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on) );  m_bFirstRequest = true;  connected();  return true;}/** * This function is responsible for opening up the connection to the remote * HTTP server and sending the header.  If this requires special * authentication or other such fun stuff, then it will handle it.  This * function will NOT receive anything from the server, however.  This is in * contrast to previous incarnations of 'httpOpen'. * * The reason for the change is due to one small fact: some requests require * data to be sent in addition to the header (POST requests) and there is no * way for this function to get that data.  This function is called in the * slotPut() or slotGet() functions which, in turn, are called (indirectly) as * a result of a KIOJob::put() or KIOJob::get().  It is those latter functions * which are responsible for starting up this ioslave in the first place. * This means that 'httpOpen' is called (essentially) as soon as the ioslave * is created -- BEFORE any data gets to this slave. * * The basic process now is this: * * 1) Open up the socket and port * 2) Format our request/header * 3) Send the header to the remote server */bool HTTPProtocol::httpOpen(){  kdDebug(7113) << "(" << m_pid << ") HTTPProtocol::httpOpen" << endl;  // Cannot have an https request without the m_bIsSSL being set!  This can  // only happen if TCPSlaveBase::InitializeSSL() function failed in which it  // means the current installation does not support SSL...  if ( (m_protocol == "https" || m_protocol == "webdavs") && !m_bIsSSL )  {    error( ERR_UNSUPPORTED_PROTOCOL, m_protocol );    return false;  }  m_request.fcache = 0;  m_request.bCachedRead = false;  m_request.bCachedWrite = false;  m_request.bMustRevalidate = false;  m_request.expireDate = 0;  m_request.creationDate = 0;  if (m_request.bUseCache)  {     m_request.fcache = checkCacheEntry( );     bool bCacheOnly = (m_request.cache == KIO::CC_CacheOnly);     bool bOffline = isOffline(m_request.doProxy ? m_proxyURL : m_request.url);     if (bOffline && (m_request.cache != KIO::CC_Reload))        m_request.cache = KIO::CC_CacheOnly;     if (m_request.cache == CC_Reload && m_request.fcache)     {        if (m_request.fcache)          fclose(m_request.fcache);        m_request.fcache = 0;     }     if ((m_request.cache == KIO::CC_CacheOnly) || (m_request.cache == KIO::CC_Cache))        m_request.bMustRevalidate = false;     m_request.bCachedWrite = true;     if (m_request.fcache && !m_request.bMustRevalidate)     {        // Cache entry is OK.        m_request.bCachedRead = true; // Cache hit.        return true;     }     else if (!m_request.fcache)     {        m_request.bMustRevalidate = false; // Cache miss     }     else     {        // Conditional cache hit. (Validate)     }     if (bCacheOnly)     {        error( ERR_DOES_NOT_EXIST, m_request.url.url() );        return false;     }     if (bOffline)     {        error( ERR_COULD_NOT_CONNECT, m_request.url.url() );        return false;     }  }  QString header;  QString davHeader;  bool moreData = false;  bool davData = false;  // Clear out per-connection settings...  resetConnectionSettings ();  // Check the validity of the current connection, if one exists.  httpCheckConnection();  if ( !m_bIsTunneled && m_bNeedTunnel )  {    setEnableSSLTunnel( true );    // We send a HTTP 1.0 header since some proxies refuse HTTP 1.1 and we don't    // need any HTTP 1.1 capabilities for CONNECT - Waba    header = QString("CONNECT %1:%2 HTTP/1.0"                     "\r\n").arg( m_request.encoded_hostname).arg(m_request.port);    // Identify who you are to the proxy server!    if (!m_request.userAgent.isEmpty())        header += "User-Agent: " + m_request.userAgent + "\r\n";    /* Add hostname information */    header += "Host: " + m_state.encoded_hostname;    if (m_state.port != m_iDefaultPort)      header += QString(":%1").arg(m_state.port);    header += "\r\n";    header += proxyAuthenticationHeader();  }  else  {    // Determine if this is a POST or GET method    switch (m_request.method)    {    case HTTP_GET:        header = "GET ";        break;    case HTTP_PUT:        header = "PUT ";        moreData = true;        m_request.bCachedWrite = false; // Do not put any result in the cache        break;    case HTTP_POST:        header = "POST ";        moreData = true;        m_request.bCachedWrite = false; // Do not put any result in the cache        break;    case HTTP_HEAD:        header = "HEAD ";        break;    case HTTP_DELETE:        header = "DELETE ";        m_request.bCachedWrite = false; // Do not put any result in the cache        break;    case HTTP_OPTIONS:        header = "OPTIONS ";        m_request.bCachedWrite = false; // Do not put any result in the cache        break;#if !defined(QT_NO_DOM)    case DAV_PROPFIND:        header = "PROPFIND ";        davData = true;        davHeader = "Depth: ";        if ( hasMetaData( "davDepth" ) )        {          kdDebug(7113) << "Reading DAV depth from metadata: " << metaData( "davDepth" ) << endl;          davHeader += metaData( "davDepth" );        }        else        {          if ( m_request.davData.depth == 2 )            davHeader += "infinity";          else            davHeader += QString("%1").arg( m_request.davData.depth );        }        davHeader += "\r\n";        m_request.bCachedWrite = false; // Do not put any result in the cache        break;    case DAV_PROPPATCH:        header = "PROPPATCH ";        davData = true;        m_request.bCachedWrite = false; // Do not put any result in the cache        break;    case DAV_MKCOL:        header = "MKCOL ";        m_request.bCachedWrite = false; // Do not put any result in the cache        break;    case DAV_COPY:    case DAV_MOVE:        header = ( m_request.method == DAV_COPY ) ? "COPY " : "MOVE ";        davHeader = "Destination: " + m_request.davData.desturl;        // infinity depth means copy recursively        // (optional for copy -> but is the desired action)        davHeader += "\r\nDepth: infinity\r\nOverwrite: ";        davHeader += m_request.davData.overwrite ? "T" : "F";        davHeader += "\r\n";        m_request.bCachedWrite = false; // Do not put any result in the cache        break;    case DAV_LOCK:        header = "LOCK ";        davHeader = "Timeout: ";        {          uint timeout = 0;          if ( hasMetaData( "davTimeout" ) )            timeout = metaData( "davTimeout" ).toUInt();          if ( timeout == 0 )            davHeader += "Infinite";          else            davHeader += QString("Seconds-%1").arg(timeout);        }        davHeader += "\r\n";        m_request.bCachedWrite = false; // Do not put any result in the cache        davData = true;        break;    case DAV_UNLOCK:        header = "UNLOCK ";        davHeader = "Lock-token: " + metaData("davLockToken") + "\r\n";        m_request.bCachedWrite = false; // Do not put any result in the cache        break;    case DAV_SEARCH:        header = "SEARCH ";        davD

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?