int
main(
    int                                 argc, 
    char **                             argv)
{
    unsigned char                       buffer[LINE_LEN];
    globus_size_t                       nbytes;
    globus_result_t                     res;
    globus_xio_attr_t                   attr;
    char *                              opts = NULL;

    if(argc < 2)
    {
        fprintf(stderr, "usage: %s url\n", argv[0]);
        return -1;
    }
    if(argc > 2)
    {
        opts = argv[2];
    }
    globus_module_activate(GLOBUS_XIO_MODULE);

    globus_xio_attr_init(&attr);
    globus_xio_handle_t             myhandle;

    res = globus_xio_handle_create_from_url(
        &myhandle,
        argv[1],
        attr,
        opts);
    test_res(res, __LINE__);

    res = globus_xio_open(myhandle, argv[1], attr);
    test_res(res, __LINE__);
    while(res == GLOBUS_SUCCESS)
    {
        res = globus_xio_read(myhandle, buffer, LINE_LEN, 1, &nbytes, NULL);
        buffer[nbytes] = '\0';
        printf("%s", buffer);
    }
    if(!globus_xio_error_is_eof(res))
    {
        printf("Error before EOF\n");
        test_res(res, __LINE__);
    }
    globus_xio_close(myhandle, NULL);
    globus_xio_attr_destroy(attr);

    globus_module_deactivate(GLOBUS_XIO_MODULE);

    return 0;
}
Exemplo n.º 2
0
static void
read_cb(
    globus_xio_handle_t                         handle,
    globus_result_t                             result,
    globus_byte_t *                             buffer,
    globus_size_t                               len,
    globus_size_t                               nbytes,
    globus_xio_data_descriptor_t                data_desc,
    void *                                      user_arg)
{
    test_info_t *                               info;
    globus_result_t                             res;

    if(!globus_xio_error_is_eof(result) &&
            !globus_xio_error_is_canceled(result))
    {
        test_res(GLOBUS_XIO_TEST_FAIL_FINISH_READ, result, __LINE__, __FILE__);
    }

    info = (test_info_t *) user_arg;

    globus_mutex_lock(&info->mutex);
    {
        if(len < nbytes)
        {
            failed_exit("read wait for has failed");
        }
        else if(nbytes > len)
        {
            failed_exit("too many bytes were read.");
        }

        info->nread += nbytes;

        if(info->nread >= info->total_read_bytes && !info->read_done)
        {
            info->closed++;
            info->read_done = GLOBUS_TRUE;
            if(info->closed == 2 || info->write_count == 0)
            {
                res = globus_xio_register_close(
                          handle,
                          NULL,
                          close_cb,
                          user_arg);
                test_res(GLOBUS_XIO_TEST_FAIL_NONE, res, __LINE__, __FILE__);
            }
        }
        else if(!info->read_done)
        {
            res = globus_xio_register_read(
                      handle,
                      info->buffer,
                      info->buffer_length,
                      info->buffer_length,
                      NULL,
                      read_cb,
                      user_arg);
            test_res(GLOBUS_XIO_TEST_FAIL_PASS_READ, res, __LINE__, __FILE__);
        }
    }
    globus_mutex_unlock(&info->mutex);
}
void
globus_i_xio_http_server_read_request_callback(
    globus_xio_operation_t              op,
    globus_result_t                     result,
    globus_size_t                       nbytes,
    void *                              user_arg)
{
    globus_i_xio_http_handle_t *        http_handle = user_arg;
    globus_bool_t                       done;
    globus_result_t                     eof_result = GLOBUS_SUCCESS;
    globus_i_xio_http_attr_t *          descriptor;
    globus_bool_t                       registered_again = GLOBUS_FALSE;
    GlobusXIOName(globus_i_xio_http_server_read_request_callback);

    globus_mutex_lock(&http_handle->mutex);

    if (result != GLOBUS_SUCCESS)
    {
        if (globus_xio_error_is_eof(result))
        {
            eof_result = result;
        }
        else
        {
            goto error_exit;
        }
    }

    /* Haven't parsed request and headers yet */
    http_handle->read_buffer_valid += nbytes;

    result = globus_l_xio_http_server_parse_request(http_handle, &done);
    if (result == GLOBUS_SUCCESS && !done)
    {
        goto reregister_read;
    }
    else if (result != GLOBUS_SUCCESS)
    {
        goto error_exit;
    }

    /* Determine whether we should expect an entity along with the
     * request
     */
    if ((http_handle->request_info.http_version == GLOBUS_XIO_HTTP_VERSION_1_1)
            && (http_handle->request_info.headers.transfer_encoding
            == GLOBUS_XIO_HTTP_TRANSFER_ENCODING_CHUNKED))
    {
        http_handle->parse_state = GLOBUS_XIO_HTTP_CHUNK_LINE;
    }
    else if (GLOBUS_I_XIO_HTTP_HEADER_IS_CONTENT_LENGTH_SET(
                &http_handle->request_info.headers))
    {
        http_handle->parse_state = GLOBUS_XIO_HTTP_IDENTITY_BODY;
    }

    if (GLOBUS_I_XIO_HTTP_HEADER_IS_CONNECTION_CLOSE(
                &http_handle->request_info.headers))
    {
        http_handle->response_info.headers.flags |=
                GLOBUS_I_XIO_HTTP_HEADER_CONNECTION_CLOSE;
    }

    http_handle->send_state = GLOBUS_XIO_HTTP_STATUS_LINE;

    descriptor = globus_xio_operation_get_data_descriptor(op, GLOBUS_TRUE);
    if (descriptor == NULL)
    {
        result = GlobusXIOErrorMemory("descriptor");
        
        goto error_exit;
    }
    globus_i_xio_http_request_destroy(&descriptor->request);
    result = globus_i_xio_http_request_copy(
            &descriptor->request,
            &http_handle->request_info);
    if (result != GLOBUS_SUCCESS)
    {
        goto error_exit;
    }

    result = globus_i_xio_http_parse_residue(http_handle, &registered_again);

    if ((http_handle->read_operation.wait_for <= 0 && !registered_again) ||
        result != GLOBUS_SUCCESS)
    {
        if (http_handle->response_info.headers.transfer_encoding !=
                GLOBUS_XIO_HTTP_TRANSFER_ENCODING_CHUNKED &&
            GLOBUS_I_XIO_HTTP_HEADER_IS_CONTENT_LENGTH_SET(
                    &http_handle->response_info.headers) &&
            http_handle->response_info.headers.content_length == 0)
        {
            /* Synthesize EOF if we've read all of the entity content */
            result = GlobusXIOErrorEOF();
        }
        /*
         * Either we've read enough, hit end of chunk, no entity was present,
         * or pass to transport failed. Call finished_read
         */
        nbytes = http_handle->read_operation.nbytes;
        globus_libc_free(http_handle->read_operation.iov);
        http_handle->read_operation.iov = NULL;
        http_handle->read_operation.iovcnt = 0;
        http_handle->read_operation.operation = NULL;
        http_handle->read_operation.driver_handle = NULL;
        http_handle->read_operation.nbytes = 0;

        globus_mutex_unlock(&http_handle->mutex);
        
        globus_xio_driver_finished_read(op, result, nbytes);

        return;
    }
    else if (registered_again)
    {
        globus_mutex_unlock(&http_handle->mutex);
        return;
    }

    /* FALLSTHROUGH */
reregister_read:
    globus_assert(op == http_handle->read_operation.operation);
    if (eof_result != GLOBUS_SUCCESS)
    {
        /* Header block wasn't complete before eof */
        result = eof_result;
        goto error_exit;
    }
    result = globus_i_xio_http_clean_read_buffer(http_handle);

    if (result != GLOBUS_SUCCESS)
    {
        goto error_exit;
    }

    result = globus_xio_driver_pass_read(
            op,
            &http_handle->read_iovec,
            1,
            1,
            globus_i_xio_http_server_read_request_callback,
            http_handle);

    if (result != GLOBUS_SUCCESS)
    {
        goto error_exit;
    }

    globus_mutex_unlock(&http_handle->mutex);
    return;

error_exit:
    globus_libc_free(http_handle->read_operation.iov);
    http_handle->read_operation.iov = NULL;
    http_handle->read_operation.iovcnt = 0;
    http_handle->read_operation.operation = NULL;
    http_handle->read_operation.driver_handle = NULL;
    http_handle->read_operation.nbytes = 0;
    globus_mutex_unlock(&http_handle->mutex);

    globus_xio_driver_finished_read(op, result, 0);
}
Exemplo n.º 4
0
/**
 * Operator: read
 */
ngemResult_t
ngrcOperatorRead(
    ngrcOperator_t *op,
    void *buf,
    size_t size,
    size_t *nRead,
    bool *canceled)
{
    ngemResult_t ret = NGEM_FAILED;
    globus_result_t gResult;
    ngLog_t *log = NULL;
    bool locked = false;
    ngrclOperatorCallbackArg_t arg;
    NGEM_FNAME(ngrcOperatorRead);

    log = ngemLogGetDefault();

    NGEM_ASSERT(op != NULL);
    NGEM_ASSERT(size >= 0);
    NGEM_ASSERT(nRead != NULL);
    NGEM_ASSERT(canceled != NULL);

    *canceled = false;

    gResult = globus_mutex_lock(&op->ngo_mutex);
    if (gResult != GLOBUS_SUCCESS) {
        ngcpLogGlobusError(log, NGRC_LOGCAT_GT, fName,
            "globus_mutex_lock", gResult);
        goto finalize;
    } 
    locked = true;

    ngrclOperatorCallbackArgInitialize(&arg, op);

    if (op->ngo_canceled) {
        *canceled = true;
        ret = NGEM_SUCCESS;
        goto finalize;
    }

    gResult = globus_xio_register_read(
        op->ngo_handle, buf, size, 1, NULL,
        ngrclGlobusCallback, &arg);
    if (gResult != GLOBUS_SUCCESS) {
        ngcpLogGlobusError(log, NGRC_LOGCAT_GT, fName,
            "globus_xio_register_read", gResult);
        goto finalize;
    }

    while (arg.ngoca_done == false) {
        gResult = globus_cond_wait(&op->ngo_cond, &op->ngo_mutex);
        if (gResult != GLOBUS_SUCCESS) {
            ngcpLogGlobusError(log, NGRC_LOGCAT_GT, fName,
                "globus_cond_wait", gResult);
        }
    }

    if (arg.ngoca_result != GLOBUS_SUCCESS) {
        if (globus_xio_error_is_canceled(arg.ngoca_result) == GLOBUS_TRUE) {
            *canceled = true;
        } else if (globus_xio_error_is_eof(arg.ngoca_result) == GLOBUS_TRUE) {
            *nRead = 0;
        } else {
            ngcpLogGlobusError(log, NGRC_LOGCAT_GT, fName,
                "Callback function for reading", arg.ngoca_result);
            goto finalize;
        }
    } else {
        *nRead = arg.ngoca_bytes;
        NGEM_ASSERT(*nRead > 0);
    }

    ret = NGEM_SUCCESS;
finalize:
    ngrclOperatorCallbackArgFinalize(&arg);

    if (locked) {
        gResult = globus_mutex_unlock(&op->ngo_mutex);
        if (gResult != GLOBUS_SUCCESS) {
            ngcpLogGlobusError(log, NGRC_LOGCAT_GT, fName,
                "globus_mutex_unlock", gResult);
            ret = NGEM_FAILED;
        }
        locked = false;
    }

    return ret;
}