Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up HTTP server implementation #8775

Merged
merged 12 commits into from
May 28, 2016
Merged

Conversation

unknownbrackets
Copy link
Collaborator

Felt like doing a bit of NIH.

Here's an example (working with PPSSPP, not necessarily optimal or spec compliant):

        net::Init();
        auto http = new http::Server(new threading::SameThreadExecutor());
        http->RegisterHandler("/test.cso", [](const http::Request &request) {
            s64 sz = File::GetFileSize(SERVE_FILENAME);

            std::string range;
            if (request.Method() == http::RequestHeader::HEAD) {
                request.WriteHttpResponseHeader(200, sz, "application/octet-stream", "Accept-Ranges: bytes\r\n");
            } else if (request.GetHeader("range", &range)) {
                s64 begin = 0, last = 0;
                if (sscanf(range.c_str(), "bytes=%lld-%lld", &begin, &last) != 2) {
                    request.WriteHttpResponseHeader(400, -1, "text/plain");
                    request.Out()->Push("Could not understand range request.");
                    return;
                }

                if (begin < 0 || begin > last || last >= sz) {
                    request.WriteHttpResponseHeader(416, -1, "text/plain");
                    request.Out()->Push("Range goes outside of file.");
                    return;
                }

                FILE *fp = File::OpenCFile(SERVE_FILENAME, "rb");
                if (!fp || fseek(fp, begin, SEEK_SET) != 0) {
                    request.WriteHttpResponseHeader(500, -1, "text/plain");
                    request.Out()->Push("File access failed.");
                    if (fp) {
                        fclose(fp);
                    }
                    return;
                }

                s64 len = last - begin + 1;
                char contentRange[1024];
                sprintf(contentRange, "Content-Range: bytes %lld-%lld/%lld\r\n", begin, last, sz);
                request.WriteHttpResponseHeader(206, len, "application/octet-stream", contentRange);

                char *buf = new char[16 * 1024];
                for (s64 pos = 0; pos < len; pos += 16 * 1024) {
                    s64 chunklen = std::min(len - pos, (s64)16 * 1024);
                    fread(buf, chunklen, 1, fp);
                    request.Out()->Push(buf, chunklen);
                }
                fclose(fp);
                delete [] buf;
                request.Out()->Flush();
            } else {
                request.WriteHttpResponseHeader(418, -1, "text/plain");
                request.Out()->Push("This server only supports range requests.");
            }
        });
        http->Run(8023);

-[Unknown]

@hrydgard
Copy link
Owner

Very nice.

Nothing wrong with a little NIH now and then :)

@hrydgard hrydgard merged commit 3147f1a into hrydgard:master May 28, 2016
@unknownbrackets unknownbrackets deleted the http branch May 28, 2016 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants