I am attempting to send JSON to Clangd on my desktop using Python. I see in the terminal window that Clangd says:
I[15:52:46.580] Warning: Missing Content-Length header, or zero-length message.
"Content-Length": 53
{
"jsonrpc": "2.0",
"id": 1,
"method": "exit"
}
But that is not the case. Python code:
import argparse
import sys
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--process_num", type=int, help="Clangd process number", required=True)
parser.add_argument("-d", "--dry_run", action="store_true", help="Dry run")
args = parser.parse_args()
filepath = f"/proc/{args.process_num}/fd/0"
my_content = """\
"Content-Length": 53
{
"jsonrpc": "2.0",
"id": 1,
"method": "exit"
}"""
if args.dry_run:
print(my_content.encode("utf-8"))
sys.exit(0)
with open(filepath, 'wb') as f:
f.write(my_content.encode("utf-8"))
Is there something in the formatting I am missing?
Update:
Made changes based on insight from HighCommander:
import argparse
import sys
import json
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--process_num", type=int, help="Clangd process number", required=True)
parser.add_argument("-d", "--dry_run", action="store_true", help="Dry run")
args = parser.parse_args()
filepath = f"/proc/{args.process_num}/fd/0"
my_object = {}
my_object["jsonrpc"] = "2.0"
my_object["id"] = 1
my_object["method"] = "exit"
my_content = json.dumps(my_object).encode()
my_header = "Content-Length: {}\r\n\r\n".format(len(my_content)).encode("ascii")
if args.dry_run:
print(my_header)
print(my_content)
sys.exit(0)
with open(filepath, 'wb') as f:
f.write(my_header)
f.write(my_content)
I get the missing Content-Length/zero length message print out from Clangd
/proc/<pid>/fd/0of an existing clangd process. I've never seen it done this way, I've only seen it done in a way where the clangd process is launched by the client as a subprocess. Maybe get it to work that way first, to rule out protocol format issues?