c语言curl发送get请求,请求的url长度问题

先来一段运行错误的代码 命名为 代码段A

#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <string.h>
int curl_recv_callback(void *buffer, size_t size, size_t memeb) {
  printf("进入回调方法:size_t:%zu memeb:%zu\n", size, memeb);
  char *response_content = buffer;
  printf("%s", response_content);
  return 0;
}
int main(void) {
  char *base_url = "http://127.0.0.1:10011/bill-tts-server-agent/api/synthesis";
  char *voiceType = "biaobei";
  char *voiceName = "标准合成_甜美女声_楠楠";
  char *language = "ZH";
  char *text = "北京合光人工智能机器人技术有限公司";
  char *speed = "5";
  char *volume = "5";
  char *pitch = "5";
  char *audioType = "6";
  char *rate = "1";

  int curl_length = 0;
  curl_length += strlen(base_url);
  curl_length += strlen("?voiceType=&voiceName=&language=&text=&speed=&volume=&pitch=&audioType=&rate=");
  curl_length += strlen(voiceType);
  curl_length += strlen(voiceName);
  curl_length += strlen(language);
  curl_length += strlen(text);
  curl_length += strlen(speed);
  curl_length += strlen(volume);
  curl_length += strlen(pitch);
  curl_length += strlen(audioType);
  curl_length += strlen(rate);

  char *dest_url = malloc(curl_length);
  strcat(dest_url, base_url);
  strcat(dest_url, "?voiceType=");
  strcat(dest_url, voiceType);
  strcat(dest_url, "&voiceName=");
  strcat(dest_url, voiceName);
  strcat(dest_url, "&language=");
  strcat(dest_url, language);
  strcat(dest_url, "&text=");
  strcat(dest_url, text);
  strcat(dest_url, "&speed=");
  strcat(dest_url, speed);
  strcat(dest_url, "&volume=");
  strcat(dest_url, volume);
  strcat(dest_url, "&pitch=");
  strcat(dest_url, pitch);
  strcat(dest_url, "&audioType=");
  strcat(dest_url, audioType);
  strcat(dest_url, "&rate=");
  strcat(dest_url, rate);
  printf("%s\n", dest_url);
  CURL* curl = curl_easy_init();
  curl_easy_setopt(curl, CURLOPT_URL, dest_url);
  curl_easy_perform(curl);
  curl_easy_cleanup(curl);
  return EXIT_SUCCESS;
}

上面的代码编译正常,但是运行时错误如下

http://127.0.0.1:10011/bill-tts-server-agent/api/synthesis?voiceType=biaobei&voiceName=标准合成_甜美女声_楠楠&language=ZH&text=北京合光人工智能机器人技术有限公司&speed=5&volume=5&pitch=5&audioType=6&rate=1
a.out: malloc.c:2401: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)

可以看到请求的URL是

http://127.0.0.1:10011/bill-tts-server-agent/api/synthesis?voiceType=biaobei&voiceName=标准合成_甜美女声_楠楠&language=ZH&text=北京合光人工智能机器人技术有限公司&speed=5&volume=5&pitch=5&audioType=6&rate=1

我怀疑url的长度问题,于是修改了下面的代码

char *text = "hi";

重新编译运行,请求的url是

http://127.0.0.1:10011/bill-tts-server-agent/api/synthesis?voiceType=biaobei&voiceName=标准合成_甜美女声_楠楠&language=ZH&text=hi&speed=5&volume=5&pitch=5&audioType=6&rate=1

运行正常

通过上面的案例我认为是请求的url长度过长,于是我又编写了下面的代码来验证上面的问题

#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>

int main(void) {
  char *dest_url =
      "http://127.0.0.1:10011/bill-tts-server-agent/api/synthesis?voiceType=biaobei&voiceName=标准合成_甜美女声_楠楠&language=ZH&text=北京合光人工智能机器人技术有限公司&speed=5&volume=5&pitch=5&audioType=6&rate=1";
  printf("%s\n",dest_url);
  CURL* curl = curl_easy_init();
  curl_easy_setopt(curl, CURLOPT_URL, dest_url);
  curl_easy_perform(curl);
  curl_easy_cleanup(curl);
  return EXIT_SUCCESS;
}

竟然编译和运行都正常,
那么运行代码段A出现下面的错误的原因是什么呢?

a.out: malloc.c:2401: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)

经过一遍又一遍的尝试,我发现是下面代码的问题

char *text = "北京合光人工智能机器人技术有限公司";

只要我修改字符的中值为其他值,比如

char *text = "北京合光人工智能机人技术有限公司";

就不会在运行是出现错误,
并且出现错误时,curl还并没有发送请求

本质原因
是strlen计算字符串长度,不会包括\0,因此malloc申请 用于拷贝该字符串 来存储的空间时size需要+1。

我设置

char *dest_url = malloc(curl_length+1);

果然不会在出现上面的问题

阅读 570
评论 更新于 1月18日
    1 个回答

    上面问题已经解决

    评论 赞赏 1月20日