Skip to content

armgit / WSM

Public Imported from GitHub
0

Run macOS applications on your Windows desktop, with a background runtime and integrated app windows.

C++
1.4 KB
Raw Blame
#pragma once
// WebDavServer — the Windows drives, shared with the macOS runtime (like WSL's /mnt/c).
//
// A small WebDAV (class 1+2) server on 127.0.0.1. Inside the runtime QEMU's user network reaches
// the host's loopback at 10.0.2.2, and macOS mounts the share with its own mount_webdav at
// ~/Windows: ~/Windows/C is C:\, ~/Windows/D is D:\ ... Every request must carry a random token
// as the first path segment; nothing else on the PC can guess it. macOS's AppleDouble ("._*")
// and .DS_Store files are accepted and dropped, so they never litter the Windows drives.
#include <atomic>
#include <cstdint>
#include <string>
#include <thread>

namespace wsm::runtime {

class WebDavServer {
public:
    WebDavServer(uint16_t port, std::string token);
    ~WebDavServer();

    bool start(std::string& err);
    void stop();
    void set_enabled(bool on) { enabled_.store(on); }   // off: every request gets 403
    bool enabled() const { return enabled_.load(); }
    // What macOS mounts.
    std::string guest_url() const { return "http://10.0.2.2:" + std::to_string(port_) + "/" + token_ + "/"; }

private:
    void accept_loop();
    void serve(uintptr_t sock);

    uint16_t port_;
    std::string token_;
    uintptr_t listen_ = ~static_cast<uintptr_t>(0);
    std::atomic<bool> running_{false}, enabled_{true};
    std::atomic<int> clients_{0};
    std::thread acceptor_;
};

} // namespace wsm::runtime