SuperHouse / esp-open-rtos

Open source FreeRTOS-based ESP8266 software framework
BSD 3-Clause "New" or "Revised" License
1.52k stars 491 forks source link

HTTP GET clear function - QUESTION #730

Closed kiralikbeyin closed 4 years ago

kiralikbeyin commented 4 years ago

HTTP GET response:

I only want to get 1.90.90 to a variable. (I added '|' for separating)

HTTP/1.1 200 OK
Cache-Control: public, max-age=1,public, no-transform, must-revalidate
Expires: Wed, 04 Sep 2019 09:39:28 GMT
Content-Type: text/plain
Content-Length: 8
Accept-Ranges: bytes
Date: Wed, 04 Sep 2019 09:39:27 GMT
Server: LiteSpeed
Vary: User-Agent
Last-modified: Tue, 1 Oct 2014 10:10:10 GMT
Connection: close

|1.90.90

I am using a lot of variables to get body : 1.90.90

Please can anyone help me to clear this function?

char* httpGetCheck(void)
{
  while (1 ) {
    const struct addrinfo hints = {
      .ai_family = AF_UNSPEC,
      .ai_socktype = SOCK_STREAM,
    };
    struct addrinfo *res;
    printf("Running DNS lookup httpGetCheck for %s...\r\n", "httpGetCheck.com");
    int err = getaddrinfo("httpGetCheck.com", "80", &hints, &res);
    if (err != 0 || res == NULL) {
      printf("DNS lookup failed err=%d res=%p\r\n", err, res);
      if (res)
        freeaddrinfo(res);
      continue;
    }
    struct sockaddr *sa = res->ai_addr;
    if (sa->sa_family == AF_INET) {
      printf("DNS lookup succeeded. IP=%s\r\n", inet_ntoa(((struct sockaddr_in *)sa)->sin_addr));
    }
    int s = socket(res->ai_family, res->ai_socktype, 0);
    if (s < 0) {
      printf("... Failed to allocate socket.\r\n");
      freeaddrinfo(res);
      continue;
    }
    printf("... allocated socket\r\n");
    if (connect(s, res->ai_addr, res->ai_addrlen) != 0) {
      close(s);
      freeaddrinfo(res);
      printf("... socket connect failed.\r\n"); 
      continue;
    }
    printf("... connected\r\n");
    freeaddrinfo(res);

    char *req =  "GET /version.txt HTTP/1.1\r\nHost: httpGetCheck.com\r\nUser-Agent: httpGetCheck\r\nConnection: close\r\n\r\n";

    if (write(s, req, strlen(req)) < 0) {
      printf("... socket send failed\r\n");
      close(s);
      continue;
    }
    printf("... socket send success\r\n"); 

    int r;
    char bufreq[128]; 
    char buffer[120];
    bzero(buffer, sizeof(buffer));

    do {
      bzero(bufreq, sizeof(bufreq));
      r = read(s, bufreq, sizeof(bufreq) - 1);
      for (int i = 0; i < r; i++) {
        putchar(bufreq[i]);
        if (bufreq[i] == '|')
        {
          snprintf(buffer, 119, "%s\n", bufreq);
        }

      }
    } while (r > 0)  ;

    close(s);

     printf("\n !!!VERSION!!! \n");
     printf("VERSION CURRENT %s\n", "1.23.456");

    char delimDevice[] = "|";
    char *ayikla = strtok(buffer, delimDevice);

    bzero(hepsi, sizeof(hepsi)); // hepsi -> GLOBAL variable :static char hepsi [20] ;

    int devi = 0;
    while (ayikla != NULL)
    {
      devi++;
      char *sonayikla = ayikla;
      ayikla = strtok(NULL, delimDevice);
      if (devi == 2)
      {
        printf("%i ---> %s\n", devi, sonayikla);
        strncpy(hepsi, sonayikla, sizeof hepsi - 1);
        gelenversion = sonayikla; // gelenversion -> GLOBAL variable : static char *gelenversion ;
      }
    }

    printf("hepsi %s\n", hepsi); 
    printf("\n gelenversion %s\n", gelenversion);

    taskYIELD();

     char *output = NULL;
    output = strstr (gelenversion, VERSION);

    memset(bufreq, 0, sizeof(bufreq));
    memset(buffer, 0, sizeof(buffer));
    memset(delimDevice, 0, sizeof(delimDevice));
        r=0;
        devi=0;

    if (output) {
      printf("\n !!!SAME VERSION \n");
      free(output);
      return VERSION;
    }
    else
    {
      printf("\n !!! NEW VERSION \n");
      free(output);
      return hepsi;
    }
    break;
  }
}