#!/usr/bin/env python3 """Manifold HTTP layer: human UI (server-rendered, zero third-party requests) and the agent-facing JSON API. Started via `manifold.py serve`. """ import base64 import difflib import hashlib import html as html_mod import json import math import os import re import time import urllib.parse import sys import threading from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from manifold import ( auto_peer_limit as M_auto_peer_limit, push_sender as M_push_sender, ARTICLE_RE, CODE_LIMIT, FP_RE, ID_RE, MAX_LINE, MAX_PARENTS, NOTE_LIMIT, OBJECT_LIMIT, PROTOCOL, SOFTWARE, TEXT_LIMIT, __version__, default_key, obj_id, gossip_all, rate_limits_from, sync_peer, sync_all, Store, RateLimiter, Key, ACCOUNT_NAME_RE, FRONTPAGE, ) MAX_JSON_BODY = 8 << 20 # push batches def esc(s) -> str: return html_mod.escape(str(s), quote=True) def uq(s) -> str: return urllib.parse.quote(str(s), safe="") # ------------------------------------------------------------ markdown-lite -- # Minimal, XSS-safe markdown subset: headings, hr, fenced code, lists, # quotes, paragraphs, `code`, **bold**, *italic*, [links](http.../relative). # Everything is HTML-escaped before inline formatting; link targets must be # http(s) or site-relative, so javascript:/data: never becomes href. def _inline(text: str) -> str: s = esc(text) codes = [] def _stash(m): codes.append(m.group(1)) return "\x00%d\x00" % (len(codes) - 1) s = re.sub(r"`([^`]+)`", _stash, s) s = re.sub(r"\*\*([^*]+)\*\*", r"\1", s) s = re.sub(r"(?\1", s) def _link(m): label, target = m.group(1), m.group(2) if re.match(r"^https?://", target, re.I) or target.startswith("/"): return '%s' % (esc(target), label) return '%s (%s)' % (label, esc(target)) s = re.sub(r"\[([^\]]+)\]\(([^)\s]+)\)", _link, s) s = re.sub(r"\x00(\d+)\x00", lambda m: "%s" % codes[int(m.group(1))], s) return s def render_markdown(text: str) -> str: # Strip NULs first: they have no markdown meaning and would collide with # the \x00N\x00 inline-code stash placeholders below. out = [] lines = text.replace("\r\n", "\n").replace("\x00", "").split("\n") i = 0 list_kind = None para = [] def flush_para(): if para: out.append("

%s

" % "
".join(_inline(x) for x in para)) para.clear() def close_list(): nonlocal list_kind if list_kind: out.append("" % list_kind) list_kind = None while i < len(lines): line = lines[i] m = re.match(r"^```", line) if m: flush_para() close_list() i += 1 buf = [] while i < len(lines) and not re.match(r"^```", lines[i]): buf.append(lines[i]) i += 1 out.append("
%s
" % esc("\n".join(buf))) i += 1 continue if not line.strip(): flush_para() close_list() i += 1 continue m = re.match(r"^(#{1,4})\s+(.*)$", line) if m: flush_para() close_list() lvl = len(m.group(1)) out.append("%s" % (lvl + 1, _inline(m.group(2)), lvl + 1)) i += 1 continue if re.match(r"^\s*---+\s*$", line): flush_para() close_list() out.append("
") i += 1 continue m = re.match(r"^\s*[-*]\s+(.*)$", line) if m: flush_para() if list_kind != "ul": close_list() out.append("