コード例 #1
0
ファイル: BR_Update.cpp プロジェクト: Breeder/sws
unsigned WINAPI BR_SearchObject::StartSearch (void* searchObject)
{
	BR_SearchObject* _this = (BR_SearchObject*)searchObject;
	JNL::open_socketlib();

	BR_Version versionL;
	LineParser lp(false);
	lp.parse(SWS_VERSION_STR);
	versionL.maj   = lp.gettoken_int(0);
	versionL.min   = lp.gettoken_int(1);
	versionL.rev   = lp.gettoken_int(2);
	versionL.build = lp.gettoken_int(3);

	// When searching by user request, lookup both official and beta
	bool searchO = true, searchB = true;
	if (_this->IsStartup())
		GetStartupSearchOptions(&searchO, &searchB, NULL);

	// Get official version and compare
	BR_Version versionO;
	int statusO = NO_CONNECTION;
	if (searchO && !_this->GetKillFlag())
	{
		JNL_HTTPGet web;
		web.addheader("User-Agent:SWS (Mozilla)");
		web.addheader("Accept:*/*");
		web.connect(SWS_URL_VERSION_H);
		time_t startTime = time(NULL);
		while (time(NULL) - startTime <= SEARCH_TIMEOUT && !_this->GetKillFlag())
		{
			_this->SetProgress((double)(time(NULL) - startTime) / (SEARCH_TIMEOUT*2));

			// Try to get version.h
			int run = web.run();
			if (run < 0 || web.get_status() < 0 || web.getreplycode() >= 400)
				break;
			if (run == 1 && web.getreplycode() == 200) //  get data only after the connection has closed
			{
				int size = web.bytes_available();
				if (char* buf = new (nothrow) char[size])
				{
					web.get_bytes(buf, size);
					char* token = strtok(buf, "\n");
					while (token != NULL)
					{
						if (sscanf(token, "#define SWS_VERSION %10d,%10d,%10d,%10d", &versionO.maj, &versionO.min, &versionO.rev, &versionO.build) > 0)
							break;
						token = strtok(NULL, "\n");
					}

					if (_this->CompareVersion(versionO, versionL) == 1)
						statusO = OFFICIAL_AVAILABLE;
					else
						statusO = UP_TO_DATE;

					delete[] buf;
					break;
				}
			}
		}
	}

	// Get beta version and compare
	BR_Version versionB;
	int statusB = NO_CONNECTION;
	if (searchB && !_this->GetKillFlag())
	{
		JNL_HTTPGet web;
		web.addheader("User-Agent:SWS (Mozilla)");
		web.addheader("Accept:*/*");
		web.connect(SWS_URL_BETA_VERSION_H);
		time_t startTime = time(NULL);
		while (time(NULL) - startTime <= SEARCH_TIMEOUT && !_this->GetKillFlag())
		{
			_this->SetProgress((double)(time(NULL) - startTime) / (SEARCH_TIMEOUT*2) + 0.5);

			// Try to get version.h
			int run = web.run();
			if (run < 0 || web.get_status() < 0 || web.getreplycode() >= 400)
				break;
			else if (run == 1 && web.getreplycode() == 200) //  get data only after the connection has closed
			{
				int size = web.bytes_available();
				if (char* buf = new (nothrow) char[size])
				{
					web.get_bytes(buf, size);
					char* token = strtok(buf, "\n");
					while (token != NULL)
					{
						if (sscanf(token, "#define SWS_VERSION %10d,%10d,%10d,%10d", &versionB.maj, &versionB.min, &versionB.rev, &versionB.build) > 0)
							break;
						token = strtok(NULL, "\n");
					}

					if (_this->CompareVersion(versionB, versionL) == 1)
						statusB = BETA_AVAILABLE;
					else
						statusB = UP_TO_DATE;

					delete[] buf;
					break;
				}
			}
		}
	}

	_this->SetProgress(1);

	// Give some breathing space to hwndDlg (i.e. SNM startup action may load screenset that repositions Reaper...)
	if (_this->IsStartup() && !_this->GetKillFlag())
		Sleep(1500);

	// When both versions are available and are identical, ignore beta
	if (statusO == OFFICIAL_AVAILABLE && statusB == BETA_AVAILABLE)
		if (!_this->CompareVersion(versionO, versionB))
			statusB = UP_TO_DATE;

	int status;
	if      (statusO == NO_CONNECTION && statusB == NO_CONNECTION)       status = NO_CONNECTION;
	else if (statusO == OFFICIAL_AVAILABLE && statusB == BETA_AVAILABLE) status = BOTH_AVAILABLE;
	else if (statusO == OFFICIAL_AVAILABLE)                              status = OFFICIAL_AVAILABLE;
	else if (statusB == BETA_AVAILABLE)                                  status = BETA_AVAILABLE;
	else                                                                 status = UP_TO_DATE;

	_this->SetStatus(versionO, versionB, status);
	JNL::close_socketlib();
	return 0;
}
コード例 #2
0
int main(int argc, char **argv)
{

  if (argc != 3)
  {
    printf("usage: httpget <url> <outfile>\n");
    exit(0);
  }

  JNL_HTTPGet get;
  JNL::open_socketlib();

  get.addheader("User-Agent:PooHead (Mozilla)");
  get.addheader("Accept:*/*");
  get.connect(argv[1]);

  FILE *fp=fopen(argv[2],"wb");
  int headerstate=0;
  int has_printed_headers=0;
  int has_printed_reply=0;
  while (1)
  {
    int st=get.run();
    if (st<0)
    {
      printf("HTTPGet error: %s\n",get.geterrorstr());
      break;
    }
    if (get.get_status()>0)
    {
      if (!has_printed_reply)
      {
        has_printed_reply=1;
        printf("reply: %s (code:%d)\n",get.getreply(),get.getreplycode());
      }
      if (get.get_status()==2)
      {
        int len;
        if (!has_printed_headers)
        {
          has_printed_headers=1;
          printf("headers:\n");
          char *p=get.getallheaders();
          while (p&&*p)
          {
            printf("%s\n",p);
            p+=strlen(p)+1;
          }
        }
        while ((len=get.bytes_available()) > 0)
        {
          char buf[4096];
          if (len > 4096) len=4096;
          len=get.get_bytes(buf,len);
          if (len>0)fwrite(buf,len,1,fp);
        }
      }
    }
    if (st==1) // 1 means connection closed
    {
      printf("HTTPGet done!\n");
      break;
    }
  }
  if (fp) fclose(fp);
  JNL::close_socketlib();
  return 0;
}