From patchwork Tue Sep 18 05:23:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Hilliard X-Patchwork-Id: 970941 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=googlegroups.com (client-ip=2607:f8b0:4864:20::63c; helo=mail-pl1-x63c.google.com; envelope-from=swupdate+bncbcl4hcw73qcbboeyqloqkgqejfihhfy@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.b="AiA4I27z"; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="B7PcrnEX"; dkim-atps=neutral Received: from mail-pl1-x63c.google.com (mail-pl1-x63c.google.com [IPv6:2607:f8b0:4864:20::63c]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42Drzb3LKZz9sCK for ; Tue, 18 Sep 2018 15:25:14 +1000 (AEST) Received: by mail-pl1-x63c.google.com with SMTP id 2-v6sf444309plc.11 for ; Mon, 17 Sep 2018 22:25:14 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1537248312; cv=pass; d=google.com; s=arc-20160816; b=T9MA1PpXmw/yEdtyfue1xnhHr7RBcucb94jMkpk6/1tRppM0sWPpRT9kg1xyTlwUJF bXWh8XnRhmAliIZVtuTqPKhS6gVSv0SmBTYYbcRdrldBPGe85NI1+ylZ8+lBJFnwDh50 E4mDcS0GbIMSvk3HCDt+h3fjps5s2UBIxxUEl+405ECbyS+2ClnDOaw+YbjgkIUDThFf GKyS8nD2ciiUH40EcMZsDTHdxiZpnYeiQT7SJj0wi25A1Z07all0h4QtblVF9BERinhJ JUqcO36ukrsLrAH88LvUcuCTVRRkCxZsza79vTZsMVNiZLsjYVZYV1ldRlEbjV6xk/zT Jd4Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:message-id:date:subject:cc:to:from :mime-version:sender:dkim-signature:dkim-signature; bh=nXhDUZwouMumfpexF2zwzJ8Y1jwBURAuY+dh88ffei8=; b=YhmCdvxjyL/B/qRDB6yaR1mV6mPbxvWes7kmayn2ht1sWiHfV5yz3iDTxNA5BCUfFo CFT5PC0du52PF2IQEfyMRmD4vjt1Mr/CvMjVPtgOSDm8gwU1G/Zh6IAxYP5kDIERyqKc HTi4PfDDo6OpdVy32wPynr7qVf91xk5Lri2Cka76wIFPz04zGAmonCronRT6dtL8mveW Jh7h4rn+N3Q/wbKFkiaJd1K4OGkXub1cxRbYjL99ATH7RzMdpPNwpm9HwRWWt6lx3nVQ kgtB1BW9X7hokBav4QkN3unqwSHT1jmtnKBAukJWq9VGfPjoY5hHd5GKF5PhJc/IsXcm 0H4w== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=SJG+i+SU; spf=pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::d41 as permitted sender) smtp.mailfrom=james.hilliard1@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:mime-version:from:to:cc:subject:date:message-id :x-original-sender:x-original-authentication-results:precedence :mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=nXhDUZwouMumfpexF2zwzJ8Y1jwBURAuY+dh88ffei8=; b=AiA4I27zr5F5I9ITm1RKokdWunCcXfj/Kywjb8jMSBXirS1pegGTkGnyeC6vHibi+W IJaRO/anEWfehxlB1hjTnSaqPF0eRk0JOUcjqN1KK2fPbe0we6kGUX1hZPoOaEDR+iWq 3zGAz03/13edXe0Hrew+ublRoYV7LT4Wwf3CiuTvQ9ZOYHc4rPmlpA4fcnb5L7NksWXY YPYLMbGsUaUxAurfl1hDrQk+dmbF8g8UWrWRcUSvSJBXwvIaBvR8/shbm3vQVmVvyHpN yOQYw5/qbcCzXr2wb5iy5417LrSpF29CT64Nj5RVdQVLbG2YsFqchXM+IcOJRULF2uJx taYA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:to:cc:subject:date:message-id:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-subscribe:list-unsubscribe; bh=nXhDUZwouMumfpexF2zwzJ8Y1jwBURAuY+dh88ffei8=; b=B7PcrnEXeEag4+smcx5dmOe3TSfpc0XGxok4dY5hxPpwZdlWDg/fPCETUbAuKX1K4S eZclJpDfYc6cneN5HfhEqM7njnfQld4C0bvQeVi8hq4WkBaaIWcsY6FoiTrJ34KeQYID YgWnhDgAMcVr8RWUJY8AyMfF1mwXhvpKlLteFsNy1uzHc6CmYe3XYK5YaqHVqx3de6LO yqH7cowapthuVw75iNoSr93H/B7dgA0oAwGFxc5o66fHQ3r7Id1K5flZry8EBpA9JMkN VNCSEs3yJWPzDl7mSw6QOC5nzDow3blpiE0hA5ajPooKxf7C/WCwrYmGTBoAj89i12pL lCMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=sender:x-gm-message-state:mime-version:from:to:cc:subject:date :message-id:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post :list-help:list-archive:list-subscribe:list-unsubscribe; bh=nXhDUZwouMumfpexF2zwzJ8Y1jwBURAuY+dh88ffei8=; b=rnv7B+KaH+/x4VGjfF4k68FXIL6gmTD2N7YpVAmUVqmQpw4+QvxKeO27Lx5xlELHtE qt59EMCgGNpMTpCauIoKn2pMdNmOfvI99KWIw0kdU7CeR2tczFfde2+hwOrhdbKd1N6q TRwu4rS19fpK+gKq2gFdyKB2vODHZdDHTVQv9j4T7eVmEJ4pQPPCsm0ZFeEybt/Kzscm LsbwLa02sLWORjHSCwbdQKXmGtUsDbo1vXKWp30OpH2PFhZD6tRyMdY5Y7IWPxT6BE/d M37IesAnZbC32pSfLTyj1IUAZ20IoytDreG0wji8QCWW8Fam7wkWva7l8LrffN5J754U bA2A== Sender: swupdate@googlegroups.com X-Gm-Message-State: APzg51CqUAe/X2gmZy2oZinCc588QGKNzHPpWYjlyqkiJr0EnjnrpIBK cUW/x9PCDNmX7SkkFMcuZ5A= X-Google-Smtp-Source: ANB0VdbYmmYgtO/Gd5yqIZsv20lvFBd2BzTXBOZaA+mA/evi64pHfanyAz0Asa8y0qrGA3nlLBjQFg== X-Received: by 2002:a17:902:109:: with SMTP id 9-v6mr101872plb.5.1537248312696; Mon, 17 Sep 2018 22:25:12 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: swupdate@googlegroups.com Received: by 2002:a17:902:8685:: with SMTP id g5-v6ls5289154plo.7.gmail; Mon, 17 Sep 2018 22:25:11 -0700 (PDT) X-Received: by 2002:a17:902:7088:: with SMTP id z8-v6mr1970946plk.38.1537248311859; Mon, 17 Sep 2018 22:25:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537248311; cv=none; d=google.com; s=arc-20160816; b=Gn+r+GymT22wzzZnmJjUw+wW2mIfGehkwdWF5s5V/yVQTf7TvWpvIIP51ucbHpPC1z E3qP2nE28ysfbN9U3pLXduPDjnAGDdwnCCaRUPt2W0HJ7YzfwX7JAksKPZGtZF+1gcZ5 c1QdQ8/RShn0Mei7JxCMs+ZB5SSDHrOWXO6TknpMQL9Cu2WGsAaFKvFIGnXguXwo7vfA Lep4d6PiP5vr9psUH87+k0p2p7zMaTrfHRBBBu1wLziN5i5wgP0FBNhiYe5VbkNXW1V7 +tkbQZ7iVsRbMaEDypJYyU3uR4Xuon+ymYGF02GGyTc3Q7hbFRYt4cn+SB/YUY6eBOCy /h/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:dkim-signature; bh=Tts0kx76K4B+SxVk6EiR/MlxTv1hT9WECJPvDrpuUNU=; b=n5ab136NFaLNOUo3Lzsa5knx08po+8i46psF2sJDY9mqKVEZuOjCbkGQYFNVj9QB+y OwS5eXZWEdgyxOmF3lYB5oX8UE8lbkFR+xRaQr94O83iK6d9569m6vTeEz7yKxd56NX6 jw9TyAwrQ9JqmJ36b7NKjU4bjyOMkJugtt7Ev2DkwVBaN5E1XcQ8FH3uVEvX/d51DYNo stGLrdFb+Vqktjvht4syN7v6ao5kSJEWaGjLz5t8mwofZM7Zi14l5/z5Z81UiT0ZoO1Y ce3N54GEsfgEbJG5lx92La/x8gcKrefwVuQAh/Gg/HnXe2RZvG2zXimfj1+l5aozAqTh FUoA== ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=SJG+i+SU; spf=pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::d41 as permitted sender) smtp.mailfrom=james.hilliard1@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-io1-xd41.google.com (mail-io1-xd41.google.com. [2607:f8b0:4864:20::d41]) by gmr-mx.google.com with ESMTPS id z26-v6si793558pgu.3.2018.09.17.22.25.11 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 17 Sep 2018 22:25:11 -0700 (PDT) Received-SPF: pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::d41 as permitted sender) client-ip=2607:f8b0:4864:20::d41; Received: by mail-io1-xd41.google.com with SMTP id q4-v6so522696iob.8 for ; Mon, 17 Sep 2018 22:25:11 -0700 (PDT) X-Received: by 2002:a6b:7117:: with SMTP id q23-v6mr13469912iog.37.1537248310720; Mon, 17 Sep 2018 22:25:10 -0700 (PDT) Received: from dragon.Home (75-166-96-40.hlrn.qwest.net. [75.166.96.40]) by smtp.gmail.com with ESMTPSA id 132-v6sm4336251itz.10.2018.09.17.22.25.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 17 Sep 2018 22:25:09 -0700 (PDT) From: james.hilliard1@gmail.com To: swupdate@googlegroups.com Cc: James Hilliard Subject: [swupdate] [PATCH] Add Download Endpoint Date: Tue, 18 Sep 2018 13:23:47 +0800 Message-Id: <1537248227-21218-1-git-send-email-james.hilliard1@gmail.com> X-Mailer: git-send-email 2.7.4 X-Original-Sender: james.hilliard1@gmail.com X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=SJG+i+SU; spf=pass (google.com: domain of james.hilliard1@gmail.com designates 2607:f8b0:4864:20::d41 as permitted sender) smtp.mailfrom=james.hilliard1@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , From: James Hilliard Signed-off-by: James Hilliard --- mongoose/mongoose_interface.c | 109 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/mongoose/mongoose_interface.c b/mongoose/mongoose_interface.c index 2521b30..534a027 100644 --- a/mongoose/mongoose_interface.c +++ b/mongoose/mongoose_interface.c @@ -58,6 +58,61 @@ struct file_upload_state { static struct mg_serve_http_opts s_http_server_opts; static void upload_handler(struct mg_connection *nc, int ev, void *p); +#ifdef CONFIG_DOWNLOAD +/* + * Downloader + */ +#include +#include "download_interface.h" +#include "channel.h" +#include "channel_curl.h" +#include "globals.h" + +struct downloader_thread_argvs{ + char *url; +}; + +#define DL_LOWSPEED_TIME 300 + +#define DL_DEFAULT_RETRIES 3 + +#define SETSTRING(p, v) do { \ + if (p) \ + free(p); \ + p = strdup(v); \ +} while (0) + +static channel_data_t channel_options = { + .source = SOURCE_DOWNLOADER, + .debug = false, + .retries = DL_DEFAULT_RETRIES, + .low_speed_timeout = DL_LOWSPEED_TIME +}; + +static void download_handler(struct mg_connection *nc, int ev, void *p); +static void *start_download_thread(void *argv); + + +static RECOVERY_STATUS download_from_url(channel_data_t* channel_data) +{ + channel_t *channel = channel_new(); + if (channel->open(channel, channel_data) != CHANNEL_OK) { + free(channel); + return FAILURE; + } + + RECOVERY_STATUS result = SUCCESS; + channel_op_res_t chanresult = channel->get_file(channel, channel_data); + if (chanresult != CHANNEL_OK) { + result = FAILURE; + } + ipc_wait_for_complete(NULL); + channel->close(channel); + free(channel); + return result; +} +#endif + /* * These functions are for V2 of the protocol */ @@ -430,6 +485,29 @@ static void upload_handler(struct mg_connection *nc, int ev, void *p) } } +#ifdef CONFIG_DOWNLOAD +static void download_handler(struct mg_connection *nc, int ev, void *p) +{ + struct http_message *hm = (struct http_message *) p; + + char url[1024]; + int ret = mg_get_http_var(&hm->body, "url", url, sizeof(url)); + fprintf(stderr,"Received URL %d\n",ret); + if (ret==-1) { + mg_http_send_error(nc, 500, NULL); + } else { + pthread_t thread_download; + pthread_create(&thread_download, NULL, start_download_thread, (void *)&url); + + mg_send_response_line(nc, 200, + "Content-Type: text/plain\r\n" + "Connection: close"); + mg_send(nc, "\r\n", 2); + nc->flags |= MG_F_SEND_AND_CLOSE; + } +} +#endif + static void ev_handler_v1(struct mg_connection __attribute__ ((__unused__)) *nc, int __attribute__ ((__unused__)) ev, void __attribute__ ((__unused__)) *ev_data) @@ -637,6 +715,9 @@ int start_mongoose(const char *cfgfname, int argc, char *argv[]) case MONGOOSE_API_V2: mg_register_http_endpoint(nc, "/restart", restart_handler); mg_register_http_endpoint(nc, "/upload", MG_CB(upload_handler, NULL)); +#ifdef CONFIG_DOWNLOAD + mg_register_http_endpoint(nc, "/download", download_handler); +#endif mg_start_thread(broadcast_message_thread, &mgr); mg_start_thread(broadcast_progress_thread, &mgr); break; @@ -654,3 +735,31 @@ int start_mongoose(const char *cfgfname, int argc, char *argv[]) return 0; } + +#ifdef CONFIG_DOWNLOAD +static void *start_download_thread(void *argv) +{ + char *url; + if (argv) { + url=(char*) argv; + + SETSTRING(channel_options.url, url); + + optind = 1; + + RECOVERY_STATUS result = download_from_url(&channel_options); + if (result != FAILURE) { + ipc_message msg; + if (ipc_postupdate(&msg) != 0) { + result = FAILURE; + } else { + result = msg.type == ACK ? result : FAILURE; + } + } + + if (channel_options.url != NULL) { + free(channel_options.url); + } + } +} +#endif