From patchwork Fri Jul 3 17:53:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Morris X-Patchwork-Id: 1703 Return-Path: Delivered-To: patchwork@archlinux.org Received: from apollo.archlinux.org (localhost [127.0.0.1]) by apollo.archlinux.org (Postfix) with ESMTP id 57F8519B6DBF0 for ; Fri, 3 Jul 2020 17:53:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on apollo.archlinux.org X-Spam-Level: X-Spam-Status: No, score=-1.7 required=5.0 tests=DKIM_ADSP_CUSTOM_MED=0.001, DKIM_INVALID=1,DKIM_SIGNED=0.1,FREEMAIL_FROM=0.5,MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3,SPF_HELO_NONE=0.001,T_DMARC_POLICY_NONE=0.01, T_DMARC_SIMPLE_DKIM=0.01 autolearn=ham autolearn_force=no version=3.4.4 X-Spam-BL-Results: [127.0.9.2] Received: from orion.archlinux.org (orion.archlinux.org [IPv6:2a01:4f8:160:6087::1]) by apollo.archlinux.org (Postfix) with ESMTPS for ; Fri, 3 Jul 2020 17:53:24 +0000 (UTC) Received: from orion.archlinux.org (localhost [127.0.0.1]) by orion.archlinux.org (Postfix) with ESMTP id 4DA221D357F072; Fri, 3 Jul 2020 17:53:22 +0000 (UTC) Received: from luna.archlinux.org (luna.archlinux.org [IPv6:2a01:4f8:160:3033::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits)) (No client certificate requested) (Authenticated sender: luna) by orion.archlinux.org (Postfix) with ESMTPSA id 2539D1D357F06C; Fri, 3 Jul 2020 17:53:22 +0000 (UTC) Authentication-Results: orion.archlinux.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CbDYxuN2 Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 1950529CB9; Fri, 3 Jul 2020 17:53:22 +0000 (UTC) Authentication-Results: luna.archlinux.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CbDYxuN2 Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 4BD4A210F6 for ; Fri, 3 Jul 2020 17:53:19 +0000 (UTC) Received: from orion.archlinux.org (orion.archlinux.org [IPv6:2a01:4f8:160:6087::1]) by luna.archlinux.org (Postfix) with ESMTPS for ; Fri, 3 Jul 2020 17:53:19 +0000 (UTC) Received: from orion.archlinux.org (localhost [127.0.0.1]) by orion.archlinux.org (Postfix) with ESMTP id 8F8131D357F067 for ; Fri, 3 Jul 2020 17:53:17 +0000 (UTC) Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by orion.archlinux.org (Postfix) with ESMTPS for ; Fri, 3 Jul 2020 17:53:17 +0000 (UTC) Received: by mail-pg1-x543.google.com with SMTP id d4so15394692pgk.4 for ; Fri, 03 Jul 2020 10:53:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=MB+4LkgtAPFd/2O/w4HC4me1X9XpYb0E5Wi6EMnf5o8=; b=CbDYxuN2vP2Ql9Pjml1sBpHpeYzAc/NYZMp7EyLDTqvRWLr2P6mzX8DwWKTr6PUqdY zbv54xBP1YzKCVp8iWXSujc4HjxOeAfSXcYlKqe2rI2VQvjqPmzCe9nRxMdIvdIOIu4S KbpcGToW1w2mVmCXVAjkWmiKvMPXrjkPFXOGAK9oFx1bHZ54+3q8EbknI9BlUhR4tteu cS0fMPDzcBE1Bt7Ce2Dv3u+cgEn6nSUuh0l3rZb5K3i3fsNlYEnrOd2LlRyrxmM9653k CsUpvd4k5Bzwa0x+tp6h4fdEyoB1b72awL/0zzEOIAvN8P9UFa2nHBoqmggs9G29SsGU ohzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=MB+4LkgtAPFd/2O/w4HC4me1X9XpYb0E5Wi6EMnf5o8=; b=BZLQbxcZZ1PuHNcrlhIJHV8MVwVMT28pMvHPF/iHN5ftKYZPzBEjZYFIy5pshSf942 Ns1HUgL4phC3I7V08g0+89UNNDTjjrcGvu8/duHua54rkTn1KP6v4o29fq/FSmT1UBpM /Xvy1jyhHC4jTFuAraprrVmYv69cO0n/CCz53+XEPrSWMBcH5hQR2VeeZwp7F7TKslij GT/3AMGX/S0wCvFaz7o58d8MA2Jb9FtVOC9nW7jZ5nWUhbuVUs8WxgQioLTUGflBdpgY lx+umjaOBEGm475K9DeHgf+WhrSXA2Rm09r5Xz81RwCo1vN5H1jWM/UrEmObAP1UcZoF jcXw== X-Gm-Message-State: AOAM5320xquwiaCROmd3raRHhQPeuWaJCStxuK2RP0C3G8OPkmT91Eka GxsBPw7gVwXFpjuSSHxknaejAImWG+M= X-Google-Smtp-Source: ABdhPJwTs/iz4QNBDQ5AM8K9gSTcBUr/Fjb7Ar2ayGT23uRyDvsr3sObDwYM7ji9ZqHXlq8IbZzEpA== X-Received: by 2002:a63:dd4d:: with SMTP id g13mr29625617pgj.179.1593798794981; Fri, 03 Jul 2020 10:53:14 -0700 (PDT) Received: from localhost.localdomain ([2600:1:9a26:fe89:3014:81cd:1b73:a575]) by smtp.gmail.com with ESMTPSA id m14sm11869680pgn.83.2020.07.03.10.53.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Jul 2020 10:53:14 -0700 (PDT) From: Kevin Morris To: aur-dev@archlinux.org Subject: [PATCH] Add POST support for RPC API Date: Fri, 3 Jul 2020 10:53:03 -0700 Message-Id: <20200703175303.4366-1-kevr.gtalk@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-BeenThere: aur-dev@archlinux.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: "Arch User Repository \(AUR\) Development" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: aur-dev-bounces@archlinux.org Sender: "aur-dev" When we received a POST method from a client, deduce our `$data` by determining which `Content-Type` is used. Supported `Content-Type`s ========================= * `application/json` * `multipart/form-data` `application/json` POST Example, searching for 'test': ``` curl -X POST -H 'Content-Type: application/data' \ --data '{"v": "5", "type": "search", "arg": "test"}' \ https://aur.archlinux.org/rpc/ ``` `multipart/form-data` POST Example, search for 'test': ``` curl -X POST -H 'Content-Type: multipart/form-data' \ -F v=5 -F type=search -F arg=test \ https://aur.archlinux.org/rpc/ ``` This change was written as a solution to https://bugs.archlinux.org/task/49089. NOTE: This commit is not final; would like a review and a bit more consideration on `multipart/form-data` and ensuring that we're handling everything we need to. Signed-off-by: Kevin Morris --- web/html/rpc.php | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/web/html/rpc.php b/web/html/rpc.php index 64c95622..41762aa3 100644 --- a/web/html/rpc.php +++ b/web/html/rpc.php @@ -2,16 +2,47 @@ set_include_path(get_include_path() . PATH_SEPARATOR . '../lib'); include_once("aurjson.class.php"); -if ( $_SERVER['REQUEST_METHOD'] != 'GET' ) { +$exposed_methods = array('GET', 'POST'); +$request_method = strtoupper($_SERVER['REQUEST_METHOD']); + +if ( !in_array($request_method, $exposed_methods) ) { header('HTTP/1.1 405 Method Not Allowed'); exit(); } -if ( isset($_GET['type']) ) { +if ( $request_method === 'GET' ) { + $data = $_GET; +} else { + // Otherwise, we were given a POST method, and we'll do some more + // work to deduce our data input. + + // Extract Content-Type; remove any trailing arguments from the string. + $content_type = $_SERVER['CONTENT_TYPE']; + if ( strpos($content_type, ';') ) { + // Example: `multipart/form-data; boundary blahblah` is extracted as + // `multipart/form-data`. + $content_type = explode(';', $content_type)[0]; + } + $content_type = rtrim(trim($content_type)); + + if ($content_type === 'application/json') { + $json = file_get_contents('php://input'); + $data = json_decode($json, true); + } elseif ($content_type === 'multipart/form-data') { + $data = $_POST; + } else { + header('HTTP/1.1 400 Bad Request'); + echo "Error: Unsupported Content-Type header.\n"; + exit(); + } +} + +if ( isset($data['type']) ) { $rpc_o = new AurJSON(); - echo $rpc_o->handle($_GET); + echo $rpc_o->handle($data); } else { echo file_get_contents('../../doc/rpc.html'); } + ?>