Repository: ossrs/srs
File: trunk/src/app/srs_app_http_static.cpp (C++)

Code before:

    string ctx = r->query_get(SRS_CONTEXT_IN_HLS);

    // If HLS stream is disabled, use SrsHttpFileServer to serve HLS, which is normal file server.
    if (!_srs_config->get_hls_ctx_enabled(req->vhost)) {
        *served = false;
        return srs_success;
    }

    // Correct the app and stream by path, which is created from template.
    // @remark Be careful that the stream has extension now, might cause identify fail.
    req->stream = srs_path_basename(r->path());

    // Served by us.
    *served = true;

    // Already exists context, response with rebuilt m3u8 content.
    if (!ctx.empty() && ctx_is_exist(ctx)) {
        // If HLS stream is disabled, use SrsHttpFileServer to serve HLS, which is normal file server.
        if (!_srs_config->get_hls_ts_ctx_enabled(req->vhost)) {
            *served = false;
            return srs_success;
        }

        if (is_interrupt(ctx))
            return srs_error_new(ERROR_HTTP_STREAM_EOF, "HTTP stream is EOF");

        err = serve_exists_session(w, r, factory, fullpath);
    } else {
        // Create a m3u8 in memory, contains the session id(ctx).
        err = serve_new_session(w, r, req, ctx);
    }

    // Always make the ctx alive now.
    alive(ctx, req);

    return err;
}

void SrsHlsStream::on_serve_ts_ctx(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{
    string ctx = r->query_get(SRS_CONTEXT_IN_HLS);
    if (ctx.empty() || !ctx_is_exist(ctx)) {
        return;
    }

    SrsHttpMessage* hr = dynamic_cast<SrsHttpMessage*>(r);
    srs_assert(hr);

    SrsHttpConn* hc = dynamic_cast<SrsHttpConn*>(hr->connection());
    srs_assert(hc);

Review comment (suggestion):
I would recommend two styles.

One is the general style, which is also commonly used in SRS.

```cpp
if (is_interrupt(ctx)) {
    return srs_error_new(ERROR_HTTP_STREAM_EOF, "HTTP stream is EOF");
}
```

Another simplified writing style is usually characterized by a large number of individual "if" statements or assignments.

```cpp
if (is_interrupt(ctx)) return srs_error_new(ERROR_HTTP_STREAM_EOF, "HTTP stream is EOF");
```

The latter writing style is controversial, but it can be simplified in cases where there are many such situations.

However, this kind is not recommended.

```cpp
if (is_interrupt(ctx))
    return srs_error_new(ERROR_HTTP_STREAM_EOF, "HTTP stream is EOF");
```

The reason is that it is easy to make mistakes when writing, especially when adding another line, it can easily cause problems, such as:

```cpp
if (is_interrupt(ctx))
    srs_trace("xxx");
    return srs_error_new(ERROR_HTTP_STREAM_EOF, "HTTP stream is EOF");
```

`TRANS_BY_GPT3`

Code after:

    string ctx = r->query_get(SRS_CONTEXT_IN_HLS);

    // If HLS stream is disabled, use SrsHttpFileServer to serve HLS, which is normal file server.
    if (!_srs_config->get_hls_ctx_enabled(req->vhost)) {
        *served = false;
        return srs_success;
    }

    // Correct the app and stream by path, which is created from template.
    // @remark Be careful that the stream has extension now, might cause identify fail.
    req->stream = srs_path_basename(r->path());

    // Served by us.
    *served = true;

    // Already exists context, response with rebuilt m3u8 content.
    if (!ctx.empty() && ctx_is_exist(ctx)) {
        // If HLS stream is disabled, use SrsHttpFileServer to serve HLS, which is normal file server.
        if (!_srs_config->get_hls_ts_ctx_enabled(req->vhost)) {
            *served = false;
            return srs_success;
        }

        if (is_interrupt(ctx)) {
            srs_warn("Reject: HLS stream is EOF, ctx=%s", ctx.c_str());
            return srs_go_http_error(w, SRS_CONSTS_HTTP_NotFound, srs_fmt("HLS stream %s is EOF", ctx.c_str()));
        }

        err = serve_exists_session(w, r, factory, fullpath);
    } else {
        // Create a m3u8 in memory, contains the session id(ctx).
        err = serve_new_session(w, r, req, ctx);
    }

    // Always make the ctx alive now.
    alive(ctx, req);

    return err;
}

void SrsHlsStream::on_serve_ts_ctx(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{
    string ctx = r->query_get(SRS_CONTEXT_IN_HLS);
    if (ctx.empty() || !ctx_is_exist(ctx)) {
        return;
    }

    SrsHttpMessage* hr = dynamic_cast<SrsHttpMessage*>(r);
    srs_assert(hr);