acme-client/src/mimik_acme_client.c
int mk_esp_http_acme_req_rsp(struct mk_acme_http_config *mkcfg, struct mk_acme_response_data *mkrsp)
{
   int rc = 0;
   int content_length = 0;
   int status_code = 0;
   esp_http_client_config_t espCfg = {0}; 
   esp_http_client_handle_t espclient = NULL;
   esp_err_t err = 0;
   char *hname = NULL;
   char *hval = NULL;

   rc = mk_update_esp_http_config(&espCfg, mkcfg);
   if (rc < 0) {
      MK_LOG(LOG_ERR,"%s() mk_update_esp_http_config failed.\n",__func__);
      return -1;
   }

   if (mkrsp && mkrsp->rsp_buff && (mkrsp->rsp_max_buff_sz > 0)) {
       mkrsp->rsp_rx_bytes = 0;
       mkrsp->content_length = 0;
       espCfg.user_data = (void *)mkrsp; // need to access in call to store data
   }

   espclient = esp_http_client_init(&espCfg);
   if (!espclient) {
      rc = -1;
      MK_LOG(LOG_ERR,"%s() esp_http_client_init failed \n",__func__);
      return -1;
   }

   if ((mkcfg->http_method == MK_HTTP_METHOD_POST) && mkcfg->post_msg && (mkcfg->post_msg_len > 0)) {
      err = esp_http_client_set_post_field(espclient, mkcfg->post_msg, mkcfg->post_msg_len);
      if (err == ESP_OK) {
         err = esp_http_client_set_method(espclient, HTTP_METHOD_POST);
      }
      if (err != ESP_OK) {
         MK_LOG(LOG_ERR, "%s: set_post error %d %s", __func__, err, esp_err_to_name(err));
         esp_http_client_cleanup(espclient);
         return -1;
      } 
   }

   if (mkcfg->http_method == MK_HTTP_METHOD_HEAD) {
      err = esp_http_client_set_method(espclient, HTTP_METHOD_HEAD);
      if (err != ESP_OK) {
         MK_LOG(LOG_ERR, "%s: set_method_head error %d %s", __func__, err, esp_err_to_name(err));
         esp_http_client_cleanup(espclient);
         return -1;
      }
   }

   if (mkcfg->set_Accept) {
      hname = "Accept";
      hval = mkcfg->set_Accept;
      err = esp_http_client_set_header(espclient, hname, hval);
      if (err != ESP_OK) {
         MK_LOG(LOG_ERR, "%s: set_header(%s:%s) error %d %s", __func__, hname,hval,err, esp_err_to_name(err));
      }
   }

   if (mkcfg->set_UserAgent) {
      hname = "User-Agent";
      hval = mkcfg->set_UserAgent;
      err = esp_http_client_set_header(espclient, hname, hval);
      if (err != ESP_OK) {
         MK_LOG(LOG_ERR, "%s: set_header(%s:%s) error %d %s", __func__, hname,hval,err, esp_err_to_name(err));
      }
   }

   if (mkcfg->set_Content_Type) {
      hname = "Content-Type";
      hval = mkcfg->set_Content_Type;
      err = esp_http_client_set_header(espclient, hname, hval);
      if (err != ESP_OK) {
         MK_LOG(LOG_ERR, "%s: set_header(%s:%s) error %d %s", __func__, hname,hval,err, esp_err_to_name(err));
      }
   }

   if (mkcfg->set_Host) {
      hname = "Host";
      hval = mkcfg->set_Host;
      err = esp_http_client_set_header(espclient, hname, hval);
      if (err != ESP_OK) {
         MK_LOG(LOG_ERR, "%s: set_header(%s:%s) error %d %s", __func__, hname,hval,err, esp_err_to_name(err));
      }
   }

   hname = "Connection";
   hval = "close";
   err = esp_http_client_set_header(espclient, hname, hval);
   if (err != ESP_OK) {
      MK_LOG(LOG_ERR, "%s: set_header(%s:%s) error %d %s", __func__, hname,hval,err, esp_err_to_name(err));
   }

   err = esp_http_client_perform(espclient);
   if (err == ESP_OK) {
	content_length = esp_http_client_get_content_length(espclient);
	status_code = esp_http_client_get_status_code(espclient);

        // first check status code : either 200 or 201 only for acme
        if ((status_code != 200) && (status_code != 201)) {
	   MK_LOG(LOG_INFO,"%s() HTTP acme error, Status = %d is not valid \n", __func__,status_code);
           rc = -1;
        }

        if (content_length > 0) {
	   MK_LOG(LOG_INFO,"%s() HTTP Status = %d, content_length = %d \n", __func__,status_code,content_length);
           rc = content_length;
        }
        else {
	   MK_LOG(LOG_INFO,"%s() HTTP Status = %d  \n", __func__,status_code);
           rc = 0;
        }

   } else {
      rc = -1;
      MK_LOG(LOG_ERR,"%s() esp_http_client_perform http request failed: err=%d  %s \n",
			 __func__,err,esp_err_to_name(err));
   }

   esp_http_client_close(espclient);
   esp_http_client_cleanup(espclient);

   return rc;
}
// returns content_length on success else returns -1 on error 
returns content_length on success else returns -1 on error
returns content_length on success else returns - 1 on error
int mk_esp_http_acme_req_rsp(struct mk_acme_http_config *mkcfg, struct mk_acme_response_data *mkrsp)
{
   int rc = 0;
   int content_length = 0;
   int status_code = 0;
   esp_http_client_config_t espCfg = {0}; 
   esp_http_client_handle_t espclient = NULL;
   esp_err_t err = 0;
   char *hname = NULL;
   char *hval = NULL;
   rc = mk_update_esp_http_config(&espCfg, mkcfg);
   if (rc < 0) {
      MK_LOG(LOG_ERR,"%s() mk_update_esp_http_config failed.\n",__func__);
      return -1;
   }
   if (mkrsp && mkrsp->rsp_buff && (mkrsp->rsp_max_buff_sz > 0)) {
       mkrsp->rsp_rx_bytes = 0;
       mkrsp->content_length = 0;
       espCfg.user_data = (void *)mkrsp; 
   }
   espclient = esp_http_client_init(&espCfg);
   if (!espclient) {
      rc = -1;
      MK_LOG(LOG_ERR,"%s() esp_http_client_init failed \n",__func__);
      return -1;
   }
   if ((mkcfg->http_method == MK_HTTP_METHOD_POST) && mkcfg->post_msg && (mkcfg->post_msg_len > 0)) {
      err = esp_http_client_set_post_field(espclient, mkcfg->post_msg, mkcfg->post_msg_len);
      if (err == ESP_OK) {
         err = esp_http_client_set_method(espclient, HTTP_METHOD_POST);
      }
      if (err != ESP_OK) {
         MK_LOG(LOG_ERR, "%s: set_post error %d %s", __func__, err, esp_err_to_name(err));
         esp_http_client_cleanup(espclient);
         return -1;
      } 
   }
   if (mkcfg->http_method == MK_HTTP_METHOD_HEAD) {
      err = esp_http_client_set_method(espclient, HTTP_METHOD_HEAD);
      if (err != ESP_OK) {
         MK_LOG(LOG_ERR, "%s: set_method_head error %d %s", __func__, err, esp_err_to_name(err));
         esp_http_client_cleanup(espclient);
         return -1;
      }
   }
   if (mkcfg->set_Accept) {
      hname = "Accept";
      hval = mkcfg->set_Accept;
      err = esp_http_client_set_header(espclient, hname, hval);
      if (err != ESP_OK) {
         MK_LOG(LOG_ERR, "%s: set_header(%s:%s) error %d %s", __func__, hname,hval,err, esp_err_to_name(err));
      }
   }
   if (mkcfg->set_UserAgent) {
      hname = "User-Agent";
      hval = mkcfg->set_UserAgent;
      err = esp_http_client_set_header(espclient, hname, hval);
      if (err != ESP_OK) {
         MK_LOG(LOG_ERR, "%s: set_header(%s:%s) error %d %s", __func__, hname,hval,err, esp_err_to_name(err));
      }
   }
   if (mkcfg->set_Content_Type) {
      hname = "Content-Type";
      hval = mkcfg->set_Content_Type;
      err = esp_http_client_set_header(espclient, hname, hval);
      if (err != ESP_OK) {
         MK_LOG(LOG_ERR, "%s: set_header(%s:%s) error %d %s", __func__, hname,hval,err, esp_err_to_name(err));
      }
   }
   if (mkcfg->set_Host) {
      hname = "Host";
      hval = mkcfg->set_Host;
      err = esp_http_client_set_header(espclient, hname, hval);
      if (err != ESP_OK) {
         MK_LOG(LOG_ERR, "%s: set_header(%s:%s) error %d %s", __func__, hname,hval,err, esp_err_to_name(err));
      }
   }
   hname = "Connection";
   hval = "close";
   err = esp_http_client_set_header(espclient, hname, hval);
   if (err != ESP_OK) {
      MK_LOG(LOG_ERR, "%s: set_header(%s:%s) error %d %s", __func__, hname,hval,err, esp_err_to_name(err));
   }
   err = esp_http_client_perform(espclient);
   if (err == ESP_OK) {
	content_length = esp_http_client_get_content_length(espclient);
	status_code = esp_http_client_get_status_code(espclient);
        if ((status_code != 200) && (status_code != 201)) {
	   MK_LOG(LOG_INFO,"%s() HTTP acme error, Status = %d is not valid \n", __func__,status_code);
           rc = -1;
        }
        if (content_length > 0) {
	   MK_LOG(LOG_INFO,"%s() HTTP Status = %d, content_length = %d \n", __func__,status_code,content_length);
           rc = content_length;
        }
        else {
	   MK_LOG(LOG_INFO,"%s() HTTP Status = %d  \n", __func__,status_code);
           rc = 0;
        }
   } else {
      rc = -1;
      MK_LOG(LOG_ERR,"%s() esp_http_client_perform http request failed: err=%d  %s \n",
			 __func__,err,esp_err_to_name(err));
   }
   esp_http_client_close(espclient);
   esp_http_client_cleanup(espclient);
   return rc;
}
int mk_esp_http_acme_req_rsp ( struct mk_acme_http_config * mkcfg , struct mk_acme_response_data * mkrsp ) { int rc = 0 ; int content_length = 0 ; int status_code = 0 ; esp_http_client_config_t espCfg = { 0 } ; esp_http_client_handle_t espclient = NULL ; esp_err_t err = 0 ; char * hname = NULL ; char * hval = NULL ; rc = mk_update_esp_http_config ( & espCfg , mkcfg ) ; if ( rc < 0 ) { MK_LOG ( LOG_ERR , " \n " , __func__ ) ; return -1 ; } if ( mkrsp && mkrsp -> rsp_buff && ( mkrsp -> rsp_max_buff_sz > 0 ) ) { mkrsp -> rsp_rx_bytes = 0 ; mkrsp -> content_length = 0 ; espCfg . user_data = ( void * ) mkrsp ; } espclient = esp_http_client_init ( & espCfg ) ; if ( ! espclient ) { rc = -1 ; MK_LOG ( LOG_ERR , " \n " , __func__ ) ; return -1 ; } if ( ( mkcfg -> http_method == MK_HTTP_METHOD_POST ) && mkcfg -> post_msg && ( mkcfg -> post_msg_len > 0 ) ) { err = esp_http_client_set_post_field ( espclient , mkcfg -> post_msg , mkcfg -> post_msg_len ) ; if ( err == ESP_OK ) { err = esp_http_client_set_method ( espclient , HTTP_METHOD_POST ) ; } if ( err != ESP_OK ) { MK_LOG ( LOG_ERR , " " , __func__ , err , esp_err_to_name ( err ) ) ; esp_http_client_cleanup ( espclient ) ; return -1 ; } } if ( mkcfg -> http_method == MK_HTTP_METHOD_HEAD ) { err = esp_http_client_set_method ( espclient , HTTP_METHOD_HEAD ) ; if ( err != ESP_OK ) { MK_LOG ( LOG_ERR , " " , __func__ , err , esp_err_to_name ( err ) ) ; esp_http_client_cleanup ( espclient ) ; return -1 ; } } if ( mkcfg -> set_Accept ) { hname = " " ; hval = mkcfg -> set_Accept ; err = esp_http_client_set_header ( espclient , hname , hval ) ; if ( err != ESP_OK ) { MK_LOG ( LOG_ERR , " " , __func__ , hname , hval , err , esp_err_to_name ( err ) ) ; } } if ( mkcfg -> set_UserAgent ) { hname = " " ; hval = mkcfg -> set_UserAgent ; err = esp_http_client_set_header ( espclient , hname , hval ) ; if ( err != ESP_OK ) { MK_LOG ( LOG_ERR , " " , __func__ , hname , hval , err , esp_err_to_name ( err ) ) ; } } if ( mkcfg -> set_Content_Type ) { hname = " " ; hval = mkcfg -> set_Content_Type ; err = esp_http_client_set_header ( espclient , hname , hval ) ; if ( err != ESP_OK ) { MK_LOG ( LOG_ERR , " " , __func__ , hname , hval , err , esp_err_to_name ( err ) ) ; } } if ( mkcfg -> set_Host ) { hname = " " ; hval = mkcfg -> set_Host ; err = esp_http_client_set_header ( espclient , hname , hval ) ; if ( err != ESP_OK ) { MK_LOG ( LOG_ERR , " " , __func__ , hname , hval , err , esp_err_to_name ( err ) ) ; } } hname = " " ; hval = " " ; err = esp_http_client_set_header ( espclient , hname , hval ) ; if ( err != ESP_OK ) { MK_LOG ( LOG_ERR , " " , __func__ , hname , hval , err , esp_err_to_name ( err ) ) ; } err = esp_http_client_perform ( espclient ) ; if ( err == ESP_OK ) { content_length = esp_http_client_get_content_length ( espclient ) ; status_code = esp_http_client_get_status_code ( espclient ) ; if ( ( status_code != 200 ) && ( status_code != 201 ) ) { MK_LOG ( LOG_INFO , " \n " , __func__ , status_code ) ; rc = -1 ; } if ( content_length > 0 ) { MK_LOG ( LOG_INFO , " \n " , __func__ , status_code , content_length ) ; rc = content_length ; } else { MK_LOG ( LOG_INFO , " \n " , __func__ , status_code ) ; rc = 0 ; } } else { rc = -1 ; MK_LOG ( LOG_ERR , " \n " , __func__ , err , esp_err_to_name ( err ) ) ; } esp_http_client_close ( espclient ) ; esp_http_client_cleanup ( espclient ) ; return rc ; }
returns content_length on success else returns -1 on error
returns content_length on success else returns - 1 on error
// need to access in call to store data // first check status code : either 200 or 201 only for acme