Exemplo n.º 1
0
static int serve_file (CC_SFILE *f, char *probfname, int silent)
{
    char request;
    char probbuf[1024];
    int id;
    int rval;

    rval = CCutil_sread_char (f, &request);
    if (rval) {
        fprintf (stderr, "CCutil_sread_char failed\n");
        return rval;
    }
    rval = CCutil_sread_string (f, probbuf, sizeof (probbuf));
    if (rval) {
        fprintf (stderr, "CCutil_sread_string failed\n");
        return rval;
    }
    rval = CCutil_sread_int (f, &id);
    if (rval) {
        fprintf (stderr, "CCutil_sread_int failed\n");
        return rval;
    }

    if (strcmp (probfname, probbuf)) {
        fprintf (stderr, "ERROR - serving %s, request %s\n", probfname,
                 probbuf);
        return rval;
    }

    switch (request) {
      case CCtsp_Pread:
        rval = serve_read (f, probfname, id, silent);
        if (rval) {
            fprintf (stderr, "serve_read failed\n");
            return rval;
        }
        break;
      case CCtsp_Pwrite:
        rval = serve_write (f, probfname, id, silent);
        if (rval) {
            fprintf (stderr, "serve_write failed\n");
            return rval;
        }
        break;
      case CCtsp_Pdelete:
        rval = serve_delete (probname, id);
        if (rval) {
            fprintf (stderr, "serve_delete failed\n");
            return rval;
        }
        break;
      default:
        fprintf (stderr, "Invalid request %c\n", request);
        return 1;
    }

    return 0;
}
Exemplo n.º 2
0
ec_cbrc_t serve(ec_server_t *srv, void *u0, struct timeval *tv, bool resched)
{
    ec_mt_t mta[16];
    size_t mta_sz = sizeof mta / sizeof(ec_mt_t);
    ec_rep_t *rep;
    ec_res_t *res;
    char uri[U_URI_STRMAX];
    bool is_proxy;

    u_unused_args(u0);

    /* Get the requested URI and method. */
    dbg_if (ec_request_get_uri(srv, uri, &is_proxy) == NULL);
    ec_method_t method = ec_server_get_method(srv);

    CHAT("%s %s", ec_method_str(method), uri);

    /* Tell'em to use test/proxy to support Proxy-Uri's. */
    if (is_proxy)
    {
        dbg_if (ec_response_set_code(srv, EC_PROXYING_NOT_SUPPORTED));
        return EC_CBRC_READY;
    }

    /* See if configured for separate responses. */
    if (resched == false && g_ctx.sep.tv_sec)
    {
        *tv = g_ctx.sep;
        u_con("reschedule %s() for %s in %llu seconds",
              __func__, uri, (long long) g_ctx.sep.tv_sec);
        return EC_CBRC_WAIT;
    }

    /* See if it is a query for the /.well-known/core URI. */
    if (!strcasecmp(ec_request_get_uri_path(srv), "/.well-known/core"))
    {
        (void) serve_wkc(srv, method);
        return EC_CBRC_READY;
    }

    /* Get Accept'able media types. */
    dbg_err_if(ec_request_get_acceptable_media_types(srv, mta, &mta_sz));

    /* Try to retrieve a representation that fits client request. */
    rep = ec_filesys_get_suitable_rep(g_ctx.fs, ec_server_get_url(srv), mta, 
            mta_sz, NULL);

    /* If found, craft the response. */
    if (rep)
    {
        dbg_err_if((res = ec_rep_get_res(rep)) == NULL);

        /* Make sure resource supports the requested method.
         * Bypass check for Proxy-Uri requests because of Publish admin
         * operations (will be checked in PUT/DELETE handlers.) */
        if (!ec_request_via_proxy(srv)
                && ec_resource_check_method(res, method))
        {
            (void) ec_response_set_code(srv, EC_METHOD_NOT_ALLOWED);
            return EC_CBRC_READY;
        }

        switch (method)
        {
            case EC_COAP_GET:
                (void) serve_get(srv, rep);
                return EC_CBRC_READY;

            case EC_COAP_DELETE:
                (void) serve_delete(srv, uri);
                return EC_CBRC_READY;

            case EC_COAP_PUT:
                (void) serve_put(srv, rep);
                return EC_CBRC_READY;

            case EC_COAP_POST:
                (void) serve_post(srv);
                return EC_CBRC_READY;

            default:
                ec_response_set_code(srv, EC_NOT_IMPLEMENTED);
                return EC_CBRC_READY;
        }
    }
    else
        (void) ec_response_set_code(srv, EC_NOT_FOUND);

    return EC_CBRC_READY;
err:
    return EC_CBRC_ERROR;
}