我有这个函数:
void read_request(int fd) {
int size = 50, pos = 0, b;
char* buffer = calloc(size, 1);
while (strncmp(buffer + (pos - 4 < 0 ? 0 : pos - 4), "\r\n\r\n", 4)) {
if ((b = read(fd, buffer + pos, size - pos)) == -1) {
perror("read() error");
exit(-1);
}
pos += b;
if (pos >= size) {
size *= 2;
buffer = realloc(buffer, size);
}
}
fwrite(buffer, 1, pos, stdout);
free(buffer);
}
它从浏览器请求中读取HTTP标头并打印出来。它工作得很好,直到我把西里尔符号放入URL,例如:http://127.0.0.1/тест
。所有ASCII符号将照常打印,但тест
打印为十六进制值:
GET /%D1%82%D0%B5%D1%81%D1%82 HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Upgrade-Insecure-Requests: 1
如何将其打印为正常的可读性文本?
发布于 2021-06-26 03:12:47
%D1%82%D0%B5%D1%81%D1%82是url编码的。web浏览器url在将unicode字符串发送到服务器之前对其进行编码。
我不确定如果你取消url编码,你会看到什么。这可能取决于终端是否正确地显示unicode。
https://stackoverflow.com/questions/68135985
复制相似问题