Esempio n. 1
0
static
void write_response(http_connection *http)
{
  int bytes_written = write(http->watcher.fd, http->response, http->response_length);

  if (bytes_written >= 0)
  {
    http->response += bytes_written;
    http->response_length -= bytes_written;
    
    if (http->response_length == 0)
    {
      if (!http_should_keep_alive(&http->parser))
      {
        shutdown(http->watcher.fd, SHUT_RDWR);
        http_destroy(http);
      }
      else
      {
        http->message_complete = 0;
      }
    }
  }
  else if (errno != EWOULDBLOCK)
  {
    http_destroy(http);
  }
}
Esempio n. 2
0
static
void http_callback(EV_P_ ev_io *w, int revents)
{
  UNUSED(loop);
  
  http_connection *http = WATCHER_TO_CONNECTION(w);
  
/*  if (revents & EV_WRITE)
  {
    puts("writable");
  }
  if (revents & EV_READ)
  {
    puts("readable");
  }*/
  
  if (!http->message_complete && (revents & EV_READ))
  {
    int length = read(http->watcher.fd, read_buffer, sizeof(read_buffer));
    
    if (length > 0)
    {
      int bytes_parsed = http_parser_execute(&http->parser, &http_settings, read_buffer, length);
      
      if (bytes_parsed < length)
      {
        printf("%i: parse error!\n", http->id);
        http_destroy(http);
        return;
      }
    }
    else if ((length < 0 && errno != EWOULDBLOCK) || length == 0)
    {
      http_destroy(http);
      return;
    }
  }
  
  if (http->response_length && (revents & EV_WRITE))
  {
    write_response(http);
  }
}
int mfconn_api_device_get_status(mfconn * conn, uint64_t * revision)
{
    const char     *api_call;
    int             retval;
    mfhttp         *http;
    int             i;

    if (conn == NULL)
        return -1;

    for (i = 0; i < mfconn_get_max_num_retries(conn); i++) {
        api_call = mfconn_create_signed_get(conn, 0, "device/get_status.php",
                                            "?response_format=json");
        if (api_call == NULL) {
            fprintf(stderr, "mfconn_create_signed_get failed\n");
            return -1;
        }

        http = http_create();

        if (mfconn_get_http_flags(conn) & HTTP_FLAG_LAZY_SSL) {

            http_set_connect_flags(http, HTTP_FLAG_LAZY_SSL);
        }

        http_set_data_handler(http, _decode_device_get_status,
                              (void *)revision);

        retval = http_get_buf(http, api_call);

        http_destroy(http);

        mfconn_update_secret_key(conn);

        free((void *)api_call);

        if (retval != 127 && retval != 28)
            break;

        // if there was either a curl timeout or a token error, get a new
        // token and try again
        //
        // on a curl timeout we get a new token because it is likely that we
        // lost signature synchronization (we don't know whether the server
        // accepted or rejected the last call)
        fprintf(stderr, "got error %d - negotiate a new token\n", retval);
        retval = mfconn_refresh_token(conn);
        if (retval != 0) {
            fprintf(stderr, "failed to get a new token\n");
            break;
        }
    }

    return retval;
}
Esempio n. 4
0
static void
http_close (DB_FILE *stream) {
    trace ("http_close %p\n", stream);
    assert (stream);
    HTTP_FILE *fp = (HTTP_FILE *)stream;

    http_abort (stream);
    if (fp->tid) {
        deadbeef->thread_join (fp->tid);
    }
    http_cancel_abort ((DB_FILE *)fp);
    http_destroy (fp);
    http_unreg_open_file ((DB_FILE *)fp);
    trace ("http_close done\n");
}
Esempio n. 5
0
husthttp_t::~ husthttp_t ( )
{
    if ( m_body )
    {
        delete m_body;
        m_body = NULL;
    }

    if ( m_head )
    {
        delete m_head;
        m_head = NULL;
    }

    if ( m_h )
    {
        http_destroy ( m_h );
    }
}
int mfconn_api_file_get_links(mfconn * conn, mffile * file,
                              const char *quickkey,
                              enum mfconn_file_link_type link_mask)
{
    const char     *api_call;
    int             retval;
    int             len;
    mfhttp         *http;
    int             i;

    if (conn == NULL)
        return -1;

    if (file == NULL)
        return -1;
    if (quickkey == NULL)
        return -1;

    len = strlen(quickkey);

    // key must either be 11 or 15 chars
    if (len != 11 && len != 15)
        return -1;

    for (i = 0; i < mfconn_get_max_num_retries(conn); i++) {
        api_call = mfconn_create_signed_get(conn, 0, "file/get_links.php",
                                            "?quick_key=%s"
                                            "&link_type=%s"
                                            "&response_format=json",
                                            quickkey,
                                            mfconn_file_link_types[link_mask]);
        if (api_call == NULL) {
            fprintf(stderr, "mfconn_create_signed_get failed\n");
            return -1;
        }

        http = http_create();

        if (mfconn_get_http_flags(conn) & HTTP_FLAG_LAZY_SSL) {

            http_set_connect_flags(http, HTTP_FLAG_LAZY_SSL);
        }

        http_set_data_handler(http, _decode_file_get_links, file);

        retval = http_get_buf(http, api_call);

        http_destroy(http);

        mfconn_update_secret_key(conn);

        free((void *)api_call);

        if (retval != 127 && retval != 28)
            break;

        // if there was either a curl timeout or a token error, get a new
        // token and try again
        //
        // on a curl timeout we get a new token because it is likely that we
        // lost signature synchronization (we don't know whether the server
        // accepted or rejected the last call)
        fprintf(stderr, "got error %d - negotiate a new token\n", retval);
        retval = mfconn_refresh_token(conn);
        if (retval != 0) {
            fprintf(stderr, "failed to get a new token\n");
            break;
        }
    }

    return retval;
}
int
mfconn_api_folder_get_info(mfconn * conn, mffolder * folder,
                           const char *folderkey)
{
    const char     *api_call;
    int             retval;
    mfhttp         *http;
    int             i;

    if (conn == NULL)
        return -1;

    if (folder == NULL)
        return -1;

    // key must either be 13 chars or NULL
    if (folderkey != NULL && strlen(folderkey) != 13) {
        return -1;
    }

    for (i = 0; i < mfconn_get_max_num_retries(conn); i++) {
        if (folderkey == NULL) {
            api_call = mfconn_create_signed_get(conn, 0, "folder/get_info.php",
                                                "?response_format=json");
        } else {
            api_call = mfconn_create_signed_get(conn, 0, "folder/get_info.php",
                                                "?folder_key=%s"
                                                "&response_format=json",
                                                folderkey);
        }
        if (api_call == NULL) {
            fprintf(stderr, "mfconn_create_signed_get failed\n");
            return -1;
        }

        http = http_create();

        if (mfconn_get_http_flags(conn) & HTTP_FLAG_LAZY_SSL) {

            http_set_connect_flags(http, HTTP_FLAG_LAZY_SSL);
        }

        http_set_data_handler(http, _decode_folder_get_info, folder);

        retval = http_get_buf(http, api_call);

        http_destroy(http);

        mfconn_update_secret_key(conn);

        free((void *)api_call);

        if (retval != 127 && retval != 28)
            break;

        // if there was either a curl timeout or a token error, get a new
        // token and try again
        //
        // on a curl timeout we get a new token because it is likely that we
        // lost signature synchronization (we don't know whether the server
        // accepted or rejected the last call)
        fprintf(stderr, "got error %d - negotiate a new token\n", retval);
        retval = mfconn_refresh_token(conn);
        if (retval != 0) {
            fprintf(stderr, "failed to get a new token\n");
            break;
        }
    }

    return retval;
}