cesanta / mongoose

Embedded Web Server

Home Page:https://mongoose.ws

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

question - change json field value via http proxy process

rhymerjr opened this issue · comments

Hi!

I will create an API proxy application based on your http-reverse-proxy example, whose task is to modify the answer under certain conditions!
My goal would be to override the value of a field in the response json structure.
As I saw in the documentation all JSON handler functions provide data return for the application.
None of them provide a pointer or reference to the retrieved value.
Also the pointer to string data in the struct mg_str is const... (hm->body.ptr)

So my question is what is the suggested way to change a json field value to another value?
How can this be done in a reasonable way? Is that possible?

Example response

Here I want to change the 'state' field of the json structure in the http message body from 4 to 2:

45ec726 3 main.c:45:forward_request FORWARDING: GET /bmenarrator/new_job.php
45ec727 3 sock.c:306:write_conn 4 7 snd 541/640 rcv 0/0 n=541 err=0
45ec727 2 sock.c:102:iolog
-- 4 10.67.32.177:45624 -> 10.67.18.57:80 541
0000 47 45 54 20 2f 62 6d 65 6e 61 72 72 61 74 6f 72 GET /bmenarrator
0010 2f 6e 65 77 5f 6a 6f 62 2e 70 68 70 3f 77 61 76 /new_job.php?wav
0020 5f 6e 61 6d 65 3d 74 74 70 5f 61 64 70 72 65 26 _name=ttp_adpre&
0030 73 72 74 5f 6e 61 6d 65 3d 74 74 70 5f 6e 61 72 srt_name=ttp_nar
0040 26 6f 75 74 5f 6e 61 6d 65 20 48 54 54 50 2f 31 &out_name HTTP/1
0050 2e 31 0d 0a 48 6f 73 74 3a 20 31 30 2e 36 37 2e .1..Host: 10.67.
0060 31 38 2e 35 37 0d 0a 43 6f 6e 6e 65 63 74 69 6f 18.57..Connectio
0070 6e 3a 20 6b 65 65 70 2d 61 6c 69 76 65 0d 0a 43 n: keep-alive..C
0080 61 63 68 65 2d 43 6f 6e 74 72 6f 6c 3a 20 6d 61 ache-Control: ma
0090 78 2d 61 67 65 3d 30 0d 0a 55 70 67 72 61 64 65 x-age=0..Upgrade
00a0 2d 49 6e 73 65 63 75 72 65 2d 52 65 71 75 65 73 -Insecure-Reques
00b0 74 73 3a 20 31 0d 0a 55 73 65 72 2d 41 67 65 6e ts: 1..User-Agen
00c0 74 3a 20 4d 6f 7a 69 6c 6c 61 2f 35 2e 30 20 28 t: Mozilla/5.0 (
00d0 57 69 6e 64 6f 77 73 20 4e 54 20 31 30 2e 30 3b Windows NT 10.0;
00e0 20 57 69 6e 36 34 3b 20 78 36 34 29 20 41 70 70 Win64; x64) App
00f0 6c 65 57 65 62 4b 69 74 2f 35 33 37 2e 33 36 20 leWebKit/537.36
0100 28 4b 48 54 4d 4c 2c 20 6c 69 6b 65 20 47 65 63 (KHTML, like Gec
0110 6b 6f 29 20 43 68 72 6f 6d 65 2f 31 32 36 2e 30 ko) Chrome/126.0
0120 2e 30 2e 30 20 53 61 66 61 72 69 2f 35 33 37 2e .0.0 Safari/537.
0130 33 36 0d 0a 41 63 63 65 70 74 3a 20 74 65 78 74 36..Accept: text
0140 2f 68 74 6d 6c 2c 61 70 70 6c 69 63 61 74 69 6f /html,applicatio
0150 6e 2f 78 68 74 6d 6c 2b 78 6d 6c 2c 61 70 70 6c n/xhtml+xml,appl
0160 69 63 61 74 69 6f 6e 2f 78 6d 6c 3b 71 3d 30 2e ication/xml;q=0.
0170 39 2c 69 6d 61 67 65 2f 61 76 69 66 2c 69 6d 61 9,image/avif,ima
0180 67 65 2f 77 65 62 70 2c 69 6d 61 67 65 2f 61 70 ge/webp,image/ap
0190 6e 67 2c 2a 2f 2a 3b 71 3d 30 2e 38 2c 61 70 70 ng,/;q=0.8,app
01a0 6c 69 63 61 74 69 6f 6e 2f 73 69 67 6e 65 64 2d lication/signed-
01b0 65 78 63 68 61 6e 67 65 3b 76 3d 62 33 3b 71 3d exchange;v=b3;q=
01c0 30 2e 37 0d 0a 41 63 63 65 70 74 2d 45 6e 63 6f 0.7..Accept-Enco
01d0 64 69 6e 67 3a 20 67 7a 69 70 2c 20 64 65 66 6c ding: gzip, defl
01e0 61 74 65 0d 0a 41 63 63 65 70 74 2d 4c 61 6e 67 ate..Accept-Lang
01f0 75 61 67 65 3a 20 68 75 2d 48 55 2c 68 75 3b 71 uage: hu-HU,hu;q
0200 3d 30 2e 39 2c 65 6e 2d 55 53 3b 71 3d 30 2e 38 =0.9,en-US;q=0.8
0210 2c 65 6e 3b 71 3d 30 2e 37 0d 0a 0d 0a ,en;q=0.7....
45ec72c 3 sock.c:295:read_conn 4 0x7 snd 0/640 rcv 0/128 n=128 err=0
45ec72c 2 sock.c:102:iolog
-- 4 10.67.32.177:45624 <- 10.67.18.57:80 128
0000 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d HTTP/1.1 200 OK.
0010 0a 44 61 74 65 3a 20 54 75 65 2c 20 30 32 20 4a .Date: Tue, 02 J
0020 75 6c 20 32 30 32 34 20 30 39 3a 34 38 3a 32 39 ul 2024 09:48:29
0030 20 47 4d 54 0d 0a 53 65 72 76 65 72 3a 20 41 70 GMT..Server: Ap
0040 61 63 68 65 2f 32 2e 34 2e 35 32 20 28 55 62 75 ache/2.4.52 (Ubu
0050 6e 74 75 29 0d 0a 43 6f 6e 74 65 6e 74 2d 4c 65 ntu)..Content-Le
0060 6e 67 74 68 3a 20 31 31 0d 0a 4b 65 65 70 2d 41 ngth: 11..Keep-A
0070 6c 69 76 65 3a 20 74 69 6d 65 6f 75 74 3d 35 2c live: timeout=5,
45ec72c 3 sock.c:295:read_conn 4 0x7 snd 0/640 rcv 128/256 n=79 err=0
45ec72c 2 sock.c:102:iolog
-- 4 10.67.32.177:45624 <- 10.67.18.57:80 79
0000 20 6d 61 78 3d 31 30 30 0d 0a 43 6f 6e 6e 65 63 max=100..Connec
0010 74 69 6f 6e 3a 20 4b 65 65 70 2d 41 6c 69 76 65 tion: Keep-Alive
0020 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 ..Content-Type:
0030 61 70 70 6c 69 63 61 74 69 6f 6e 2f 6a 73 6f 6e application/json
0040 0d 0a 0d 0a 7b 22 65 72 72 6f 72 22 3a 34 7d ....{"state":4}

Environment

mongoose-7.13

Linux version 6.1.0-22-amd64 (debian-kernel@lists.debian.org) (gcc-12 (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40) #1 SMP PREEMPT_DYNAMIC Debian 6.1.94-1 (2024-06-21)

g++ (GCC) 13.2.0