app/modules/enduser_setup.c
/**
 * Load HTTP Payload
 *
 * @return - 0 iff payload loaded successfully
 *           1 iff backup html was loaded
 *           2 iff out of memory
 */
Load HTTP Payload
@return - 0 iff payload loaded successfully
1 iff backup html was loaded
2 iff out of memory
Load HTTP Payload @return - 0 iff payload loaded successfully 1 iff backup html was loaded 2 iff out of memory
static int enduser_setup_http_load_payload(void)
{
  ENDUSER_SETUP_DEBUG("enduser_setup_http_load_payload");
  int err = VFS_RES_ERR;
  int err2 = VFS_RES_ERR;
  int file_len = 0;
  int f = vfs_open(http_html_gz_filename, "r");
  if (f) 
  {
      err = vfs_lseek(f, 0, VFS_SEEK_END);
      file_len = (int) vfs_tell(f);
      err2 = vfs_lseek(f, 0, VFS_SEEK_SET);
  }
  if (!f || err == VFS_RES_ERR || err2 == VFS_RES_ERR)
  {
    if (f) 
    {
      vfs_close(f);
    }
    f = vfs_open(http_html_filename, "r");
    if (f) 
    {
        err = vfs_lseek(f, 0, VFS_SEEK_END);
        file_len = (int) vfs_tell(f);
        err2 = vfs_lseek(f, 0, VFS_SEEK_SET);
    }
  }
  char cl_hdr[30];
  size_t ce_len = 0;
  c_sprintf(cl_hdr, http_header_content_len_fmt, file_len);
  size_t cl_len = c_strlen(cl_hdr);
  if (!f || err == VFS_RES_ERR || err2 == VFS_RES_ERR)
  {
    ENDUSER_SETUP_DEBUG("Unable to load file enduser_setup.html, loading backup HTML...");
    c_sprintf(cl_hdr, http_header_content_len_fmt, sizeof(http_html_backup));
    cl_len = c_strlen(cl_hdr);
    int html_len = LITLEN(http_html_backup);
    if (http_html_backup[0] == 0x1f && http_html_backup[1] == 0x8b)
    {
        ce_len = c_strlen(http_html_gzip_contentencoding);
        html_len = http_html_backup_len; 
        ENDUSER_SETUP_DEBUG("Content is gzipped");
    }   
    int payload_len = LITLEN(http_header_200) + cl_len + ce_len + html_len; 
    state->http_payload_len = payload_len;
    state->http_payload_data = (char *) c_malloc(payload_len);
    if (state->http_payload_data == NULL)
    {
      return 2;
    }
    int offset = 0;
    c_memcpy(&(state->http_payload_data[offset]), &(http_header_200), LITLEN(http_header_200));
    offset += LITLEN(http_header_200);
    if (ce_len > 0)
    {
        offset += c_sprintf(state->http_payload_data + offset, http_html_gzip_contentencoding, ce_len);
    }
    c_memcpy(&(state->http_payload_data[offset]), &(cl_hdr), cl_len);
    offset += cl_len;
    c_memcpy(&(state->http_payload_data[offset]), &(http_html_backup), sizeof(http_html_backup));
    return 1; 
  }
  char magic[2];
  vfs_read(f, magic, 2);
  if (magic[0] == 0x1f && magic[1] == 0x8b)
  {
    ce_len = c_strlen(http_html_gzip_contentencoding);
    ENDUSER_SETUP_DEBUG("Content is gzipped");
  }
  int payload_len = LITLEN(http_header_200) + cl_len + ce_len + file_len;
  state->http_payload_len = payload_len;
  state->http_payload_data = (char *) c_malloc(payload_len);
  if (state->http_payload_data == NULL)
  {
    return 2;
  }
  vfs_lseek(f, 0, VFS_SEEK_SET);
  int offset = 0;
  c_memcpy(&(state->http_payload_data[offset]), &(http_header_200), LITLEN(http_header_200));
  offset += LITLEN(http_header_200);
  if (ce_len > 0)
  {
    offset += c_sprintf(state->http_payload_data + offset, http_html_gzip_contentencoding, ce_len);  
  }
  c_memcpy(&(state->http_payload_data[offset]), &(cl_hdr), cl_len);
  offset += cl_len;
  vfs_read(f, &(state->http_payload_data[offset]), file_len);
  vfs_close(f);  
  return 0;
}
static int enduser_setup_http_load_payload ( void ) { ENDUSER_SETUP_DEBUG ( " " ) ; int err = VFS_RES_ERR ; int err2 = VFS_RES_ERR ; int file_len = 0 ; int f = vfs_open ( http_html_gz_filename , " " ) ; if ( f ) { err = vfs_lseek ( f , 0 , VFS_SEEK_END ) ; file_len = ( int ) vfs_tell ( f ) ; err2 = vfs_lseek ( f , 0 , VFS_SEEK_SET ) ; } if ( ! f || err == VFS_RES_ERR || err2 == VFS_RES_ERR ) { if ( f ) { vfs_close ( f ) ; } f = vfs_open ( http_html_filename , " " ) ; if ( f ) { err = vfs_lseek ( f , 0 , VFS_SEEK_END ) ; file_len = ( int ) vfs_tell ( f ) ; err2 = vfs_lseek ( f , 0 , VFS_SEEK_SET ) ; } } char cl_hdr [ 30 ] ; size_t ce_len = 0 ; c_sprintf ( cl_hdr , http_header_content_len_fmt , file_len ) ; size_t cl_len = c_strlen ( cl_hdr ) ; if ( ! f || err == VFS_RES_ERR || err2 == VFS_RES_ERR ) { ENDUSER_SETUP_DEBUG ( " " ) ; c_sprintf ( cl_hdr , http_header_content_len_fmt , sizeof ( http_html_backup ) ) ; cl_len = c_strlen ( cl_hdr ) ; int html_len = LITLEN ( http_html_backup ) ; if ( http_html_backup [ 0 ] == 0x1f && http_html_backup [ 1 ] == 0x8b ) { ce_len = c_strlen ( http_html_gzip_contentencoding ) ; html_len = http_html_backup_len ; ENDUSER_SETUP_DEBUG ( " " ) ; } int payload_len = LITLEN ( http_header_200 ) + cl_len + ce_len + html_len ; state -> http_payload_len = payload_len ; state -> http_payload_data = ( char * ) c_malloc ( payload_len ) ; if ( state -> http_payload_data == NULL ) { return 2 ; } int offset = 0 ; c_memcpy ( & ( state -> http_payload_data [ offset ] ) , & ( http_header_200 ) , LITLEN ( http_header_200 ) ) ; offset += LITLEN ( http_header_200 ) ; if ( ce_len > 0 ) { offset += c_sprintf ( state -> http_payload_data + offset , http_html_gzip_contentencoding , ce_len ) ; } c_memcpy ( & ( state -> http_payload_data [ offset ] ) , & ( cl_hdr ) , cl_len ) ; offset += cl_len ; c_memcpy ( & ( state -> http_payload_data [ offset ] ) , & ( http_html_backup ) , sizeof ( http_html_backup ) ) ; return 1 ; } char magic [ 2 ] ; vfs_read ( f , magic , 2 ) ; if ( magic [ 0 ] == 0x1f && magic [ 1 ] == 0x8b ) { ce_len = c_strlen ( http_html_gzip_contentencoding ) ; ENDUSER_SETUP_DEBUG ( " " ) ; } int payload_len = LITLEN ( http_header_200 ) + cl_len + ce_len + file_len ; state -> http_payload_len = payload_len ; state -> http_payload_data = ( char * ) c_malloc ( payload_len ) ; if ( state -> http_payload_data == NULL ) { return 2 ; } vfs_lseek ( f , 0 , VFS_SEEK_SET ) ; int offset = 0 ; c_memcpy ( & ( state -> http_payload_data [ offset ] ) , & ( http_header_200 ) , LITLEN ( http_header_200 ) ) ; offset += LITLEN ( http_header_200 ) ; if ( ce_len > 0 ) { offset += c_sprintf ( state -> http_payload_data + offset , http_html_gzip_contentencoding , ce_len ) ; } c_memcpy ( & ( state -> http_payload_data [ offset ] ) , & ( cl_hdr ) , cl_len ) ; offset += cl_len ; vfs_read ( f , & ( state -> http_payload_data [ offset ] ) , file_len ) ; vfs_close ( f ) ; return 0 ; }
Load HTTP Payload
 @return - 0 iff payload loaded successfully
           1 iff backup html was loaded
           2 iff out of memory
Load HTTP Payload @return - 0 iff payload loaded successfully 1 iff backup html was loaded 2 iff out of memory
/* Try to open enduser_setup.html.gz from SPIFFS first */ /* If that didn't work, try to open enduser_setup.html from SPIFFS */ /* Defined in eus/http_html_backup.def by xxd -i */