⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftp.c

📁 harvest是一个下载html网页得机器人
💻 C
📖 第 1 页 / 共 5 页
字号:
  ftp->passwd = conn->passwd;  ftp->response_time = 3600; /* set default response time-out */  if (data->set.tunnel_thru_httpproxy) {    /* We want "seamless" FTP operations through HTTP proxy tunnel */    result = Curl_ConnectHTTPProxyTunnel(conn, conn->firstsocket,                                         conn->hostname, conn->remote_port);    if(CURLE_OK != result)      return result;  }  if(conn->protocol & PROT_FTPS) {    /* FTPS is simply ftp with SSL for the control channel */    /* now, perform the SSL initialization for this socket */    result = Curl_SSLConnect(conn);    if(result)      return result;  }  /* The first thing we do is wait for the "220*" line: */  result = Curl_GetFTPResponse(&nread, conn, &ftpcode);  if(result)    return result;  if(ftpcode != 220) {    failf(data, "This doesn't seem like a nice ftp-server response");    return CURLE_FTP_WEIRD_SERVER_REPLY;  }#ifdef KRB4  /* if not anonymous login, try a secure login */  if(data->set.krb4) {    /* request data protection level (default is 'clear') */    Curl_sec_request_prot(conn, "private");    /* We set private first as default, in case the line below fails to       set a valid level */    Curl_sec_request_prot(conn, data->set.krb4_level);    if(Curl_sec_login(conn) != 0)      infof(data, "Logging in with password in cleartext!\n");    else      infof(data, "Authentication successful\n");  }#endif    /* send USER */  FTPSENDF(conn, "USER %s", ftp->user?ftp->user:"");  /* wait for feedback */  result = Curl_GetFTPResponse(&nread, conn, &ftpcode);  if(result)    return result;  if(ftpcode == 530) {    /* 530 User ... access denied       (the server denies to log the specified user) */    failf(data, "Access denied: %s", &buf[4]);    return CURLE_FTP_ACCESS_DENIED;  }  else if(ftpcode == 331) {    /* 331 Password required for ...       (the server requires to send the user's password too) */    FTPSENDF(conn, "PASS %s", ftp->passwd?ftp->passwd:"");    result = Curl_GetFTPResponse(&nread, conn, &ftpcode);    if(result)      return result;    if(ftpcode == 530) {      /* 530 Login incorrect.         (the username and/or the password are incorrect) */      failf(data, "the username and/or the password are incorrect");      return CURLE_FTP_USER_PASSWORD_INCORRECT;    }    else if(ftpcode == 230) {      /* 230 User ... logged in.         (user successfully logged in) */              infof(data, "We have successfully logged in\n");    }    else {      failf(data, "Odd return code after PASS");      return CURLE_FTP_WEIRD_PASS_REPLY;    }  }  else if(buf[0] == '2') {    /* 230 User ... logged in.       (the user logged in without password) */    infof(data, "We have successfully logged in\n");#ifdef KRB4	/* we are logged in (with Kerberos)	 * now set the requested protection level	 */    if(conn->sec_complete)      Curl_sec_set_protection_level(conn);    /* we may need to issue a KAUTH here to have access to the files     * do it if user supplied a password     */    if(conn->passwd && *conn->passwd) {      result = Curl_krb_kauth(conn);      if(result)        return result;    }#endif  }  else {    failf(data, "Odd return code after USER");    return CURLE_FTP_WEIRD_USER_REPLY;  }  /* send PWD to discover our entry point */  FTPSENDF(conn, "PWD", NULL);  /* wait for feedback */  result = Curl_GetFTPResponse(&nread, conn, &ftpcode);  if(result)    return result;  if(ftpcode == 257) {    char *dir = (char *)malloc(nread+1);    char *store=dir;    char *ptr=&buf[4]; /* start on the first letter */    if(!dir)      return CURLE_OUT_OF_MEMORY;        /* Reply format is like       257<space>"<directory-name>"<space><commentary> and the RFC959 says       The directory name can contain any character; embedded double-quotes       should be escaped by double-quotes (the "quote-doubling" convention).    */    if('\"' == *ptr) {      /* it started good */      ptr++;      while(ptr && *ptr) {        if('\"' == *ptr) {          if('\"' == ptr[1]) {            /* "quote-doubling" */            *store = ptr[1];            ptr++;          }          else {            /* end of path */            *store = '\0'; /* zero terminate */            break; /* get out of this loop */          }        }        else          *store = *ptr;        store++;        ptr++;      }      ftp->entrypath =dir; /* remember this */      infof(data, "Entry path is '%s'\n", ftp->entrypath);    }    else {      /* couldn't get the path */      free(dir);      infof(data, "Failed to figure out path\n");    }  }  else {    /* We couldn't read the PWD response! */  }  return CURLE_OK;}/*********************************************************************** * * Curl_ftp_done() * * The DONE function. This does what needs to be done after a single DO has * performed. * * Input argument is already checked for validity. */CURLcode Curl_ftp_done(struct connectdata *conn){  struct SessionHandle *data = conn->data;  struct FTP *ftp = conn->proto.ftp;  ssize_t nread;  int ftpcode;  CURLcode result=CURLE_OK;  /* free the dir tree parts */  freedirs(ftp);  if(ftp->file) {    free(ftp->file);    ftp->file = NULL;  }  if(data->set.upload) {    if((-1 != data->set.infilesize) &&       (data->set.infilesize != *ftp->bytecountp) &&       !data->set.crlf) {      failf(data, "Uploaded unaligned file size (%d out of %d bytes)",            *ftp->bytecountp, data->set.infilesize);      return CURLE_PARTIAL_FILE;    }  }  else {    if((-1 != conn->size) && (conn->size != *ftp->bytecountp) &&       (conn->maxdownload != *ftp->bytecountp)) {      failf(data, "Received only partial file: %d bytes", *ftp->bytecountp);      return CURLE_PARTIAL_FILE;    }    else if(!ftp->dont_check &&            !*ftp->bytecountp &&            (conn->size>0)) {      /* We consider this an error, but there's no true FTP error received         why we need to continue to "read out" the server response too.         We don't want to leave a "waiting" server reply if we'll get told         to make a second request on this same connection! */      failf(data, "No data was received!");      result = CURLE_FTP_COULDNT_RETR_FILE;    }  }#ifdef KRB4  Curl_sec_fflush_fd(conn, conn->secondarysocket);#endif  /* shut down the socket to inform the server we're done */  sclose(conn->secondarysocket);  conn->secondarysocket = -1;  if(!ftp->no_transfer) {    /* Let's see what the server says about the transfer we just performed,       but lower the timeout as sometimes this connection has died while        the data has been transfered. This happens when doing through NATs       etc that abandon old silent connections.    */    ftp->response_time = 60; /* give it only a minute for now */    result = Curl_GetFTPResponse(&nread, conn, &ftpcode);    ftp->response_time = 3600; /* set this back to one hour waits */      if(!nread && (CURLE_OPERATION_TIMEDOUT == result)) {      failf(data, "control connection looks dead");      return result;    }    if(result)      return result;    if(!ftp->dont_check) {      /* 226 Transfer complete, 250 Requested file action okay, completed. */      if((ftpcode != 226) && (ftpcode != 250)) {        failf(data, "server did not report OK, got %d", ftpcode);        return CURLE_FTP_WRITE_ERROR;      }    }  }  /* clear these for next connection */  ftp->no_transfer = FALSE;  ftp->dont_check = FALSE;   /* Send any post-transfer QUOTE strings? */  if(!result && data->set.postquote)    result = ftp_sendquote(conn, data->set.postquote);  return result;}/*********************************************************************** * * ftp_sendquote() * * Where a 'quote' means a list of custom commands to send to the server. * The quote list is passed as an argument. */static CURLcode ftp_sendquote(struct connectdata *conn, struct curl_slist *quote){  struct curl_slist *item;  ssize_t nread;  int ftpcode;  CURLcode result;  item = quote;  while (item) {    if (item->data) {      FTPSENDF(conn, "%s", item->data);      result = Curl_GetFTPResponse(&nread, conn, &ftpcode);      if (result)        return result;      if (ftpcode >= 400) {        failf(conn->data, "QUOT string not accepted: %s", item->data);        return CURLE_FTP_QUOTE_ERROR;      }    }    item = item->next;  }  return CURLE_OK;}/*********************************************************************** * * ftp_getfiletime() * * Get the timestamp of the given file. */staticCURLcode ftp_getfiletime(struct connectdata *conn, char *file){  CURLcode result=CURLE_OK;  int ftpcode; /* for ftp status */  ssize_t nread;  char *buf = conn->data->state.buffer;  /* we have requested to get the modified-time of the file, this is yet     again a grey area as the MDTM is not kosher RFC959 */  FTPSENDF(conn, "MDTM %s", file);  result = Curl_GetFTPResponse(&nread, conn, &ftpcode);  if(result)    return result;  switch(ftpcode) {  case 213:    {      /* we got a time. Format should be: "YYYYMMDDHHMMSS[.sss]" where the         last .sss part is optional and means fractions of a second */      int year, month, day, hour, minute, second;      if(6 == sscanf(buf+4, "%04d%02d%02d%02d%02d%02d",                     &year, &month, &day, &hour, &minute, &second)) {        /* we have a time, reformat it */        time_t secs=time(NULL);        sprintf(buf, "%04d%02d%02d %02d:%02d:%02d GMT",                year, month, day, hour, minute, second);        /* now, convert this into a time() value: */        conn->data->info.filetime = curl_getdate(buf, &secs);      }    }    break;  default:    infof(conn->data, "unsupported MDTM reply format\n");    break;  case 550: /* "No such file or directory" */    failf(conn->data, "Given file does not exist");    result = CURLE_FTP_COULDNT_RETR_FILE;    break;  }  return  result;}/*********************************************************************** * * ftp_transfertype() * * Set transfer type. We only deal with ASCII or BINARY so this function * sets one of them. */static CURLcode ftp_transfertype(struct connectdata *conn,                                  bool ascii){  struct SessionHandle *data = conn->data;  int ftpcode;  ssize_t nread;  CURLcode result;  FTPSENDF(conn, "TYPE %s", ascii?"A":"I");  result = Curl_GetFTPResponse(&nread, conn, &ftpcode);  if(result)    return result;    if(ftpcode != 200) {    failf(data, "Couldn't set %s mode",          ascii?"ASCII":"binary");    return ascii? CURLE_FTP_COULDNT_SET_ASCII:CURLE_FTP_COULDNT_SET_BINARY;  }  return CURLE_OK;}/*********************************************************************** * * ftp_getsize() * * Returns the file size (in bytes) of the given remote file. */staticCURLcode ftp_getsize(struct connectdata *conn, char *file,                      ssize_t *size){  struct SessionHandle *data = conn->data;  int ftpcode;  ssize_t nread;  char *buf=data->state.buffer;  CURLcode result;  FTPSENDF(conn, "SIZE %s", file);  result = Curl_GetFTPResponse(&nread, conn, &ftpcode);  if(result)    return result;  if(ftpcode == 213) {    /* get the size from the ascii string: */    *size = atoi(buf+4);  }  else    return CURLE_FTP_COULDNT_GET_SIZE;  return CURLE_OK;}/*************************************************************************** * * ftp_pasv_verbose() * * This function only outputs some informationals about this second connection * when we've issued a PASV command before and thus we have connected to a * possibly new IP address. * */static voidftp_pasv_verbose(struct connectdata *conn,                 Curl_ipconnect *addr,                 char *newhost, /* ascii version */                 int port)

⌨️ 快捷键说明

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