libcurl_debug

#ifndef LIBCURL_DEBUG_HPP
#define LIBCURL_DEBUG_HPP

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

struct data {
  char trace_ascii; /* 1 or 0 */
};

static
void dump(const char *text,
          FILE *stream, unsigned char *ptr, size_t size,
          char nohex)
{
  size_t i;
  size_t c;

  unsigned int width = 0x10;

  if(nohex)
    /* without the hex output, we can fit more on screen */
    width = 0x40;

  fprintf(stream, "%s, %10.10lu bytes (0x%8.8lx)\n",
          text, (unsigned long)size, (unsigned long)size);

  for(i = 0; i<size; i += width) {

    fprintf(stream, "%4.4lx: ", (unsigned long)i);

    if(!nohex) {
      /* hex not disabled, show it */
      for(c = 0; c < width; c++)
        if(i + c < size)
          fprintf(stream, "%02x ", ptr[i + c]);
        else
          fputs("   ", stream);
    }

    for(c = 0; (c < width) && (i + c < size); c++) {
      /* check for 0D0A; if found, skip past and start a new line of output */
      if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
         ptr[i + c + 1] == 0x0A) {
        i += (c + 2 - width);
        break;
      }
      fprintf(stream, "%c",
              (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
      /* check again for 0D0A, to avoid an extra \n if it's at width */
      if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
         ptr[i + c + 2] == 0x0A) {
        i += (c + 3 - width);
        break;
      }
    }
    fputc('\n', stream); /* newline */
  }
  fflush(stream);
}

static
int my_trace(CURL *handle, curl_infotype type,
             char *data, size_t size,
             void *userp)
{
  struct data *config = (struct data *)userp;
  const char *text;
  (void)handle; /* prevent compiler warning */

  switch(type) {
  case CURLINFO_TEXT:
    fprintf(stderr, "== Info: %s", data);
    /* FALLTHROUGH */
  default: /* in case a new one is introduced to shock us */
    return 0;

  case CURLINFO_HEADER_OUT:
    text = "=> Send header";
    break;
  case CURLINFO_DATA_OUT:
    text = "=> Send data";
    break;
  case CURLINFO_SSL_DATA_OUT:
    text = "=> Send SSL data";
    break;
  case CURLINFO_HEADER_IN:
    text = "<= Recv header";
    break;
  case CURLINFO_DATA_IN:
    text = "<= Recv data";
    break;
  case CURLINFO_SSL_DATA_IN:
    text = "<= Recv SSL data";
    break;
  }

  dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
  return 0;
}

int test(void)
{
  CURL *curl;
  CURLcode res;
  struct data config;

  config.trace_ascii = 1; /* enable ascii tracing */

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
    curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);

    /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    /* example.com is redirected, so we tell libcurl to follow redirection */
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}



#endif // LIBCURL_DEBUG_HPP

運行結果:

== Info:   Trying 93.184.216.34:443...
== Info: Connected to example.com (93.184.216.34) port 443 (#0)
== Info: ALPN, offering http/1.1
=> Send SSL data, 0000000005 bytes (0x00000005)
0000: .....
== Info: TLSv1.3 (OUT), TLS handshake, Client hello (1):
=> Send SSL data, 0000000512 bytes (0x00000200)
0000: .......= ..l...XtL..D...f..?1.+..l.p. .2...Ff.e.%.V...?....8..[
0040: ...J)+d.>.......,.0.........+./...$.(.k.#.'.g.....9.....3.....=.
0080: <.5./.....u.........example.com........................3t.......
00c0: ..http/1.1.........1.....*.(....................................
0100: .....+........-.....3.&.$... .......6v..z...s.d.+..A~/l.<.L]_...
0140: ................................................................
0180: ................................................................
01c0: ................................................................
<= Recv SSL data, 0000000005 bytes (0x00000005)
0000: ....X
== Info: TLSv1.3 (IN), TLS handshake, Server hello (2):
<= Recv SSL data, 0000000088 bytes (0x00000058)
0000: ...T...!.t..a......e......z..^......3. .2...Ff.e.%.V...?....8..[
0040: ...J)+d......+.....3....
=> Send SSL data, 0000000005 bytes (0x00000005)
0000: .....
== Info: TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
=> Send SSL data, 0000000001 bytes (0x00000001)
0000: .
=> Send SSL data, 0000000005 bytes (0x00000005)
0000: .....
== Info: TLSv1.3 (OUT), TLS handshake, Client hello (1):
=> Send SSL data, 0000000512 bytes (0x00000200)
0000: .......= ..l...XtL..D...f..?1.+..l.p. .2...Ff.e.%.V...?....8..[
0040: ...J)+d.>.......,.0.........+./...$.(.k.#.'.g.....9.....3.....=.
0080: <.5./.....u.........example.com........................3t.......
00c0: ..http/1.1.........1.....*.(....................................
0100: .....+........-.....3.G.E...A.....Z..b..Z..s....OnK.x..&...[....
0140: ..a-G...UF.....1.....S,.....~...................................
0180: ................................................................
01c0: ................................................................
<= Recv SSL data, 0000000005 bytes (0x00000005)
0000: .....
<= Recv SSL data, 0000000005 bytes (0x00000005)
0000: .....
== Info: TLSv1.3 (IN), TLS handshake, Server hello (2):
<= Recv SSL data, 0000000155 bytes (0x0000009b)
0000: .......Kq....tV..D]..sZ.#............. .2...Ff.e.%.V...?....8..[
0040: ...J)+d....O.+.....3.E...A..0._.^..]......1*....wg.....L........
0080: S6...{...#r]).(......!..#/.
<= Recv SSL data, 0000000005 bytes (0x00000005)
0000: ....*
<= Recv SSL data, 0000000001 bytes (0x00000001)
0000: .
== Info: TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
<= Recv SSL data, 0000000025 bytes (0x00000019)
0000: .................http/1.1
<= Recv SSL data, 0000000005 bytes (0x00000005)
0000: .....
<= Recv SSL data, 0000000001 bytes (0x00000001)
0000: .
== Info: TLSv1.3 (IN), TLS handshake, Certificate (11):
<= Recv SSL data, 0000004006 bytes (0x00000fa6)
0000: ..........D0..@0..(.........x.H...M.+.k`8.0...*.H........0M1.0..
0040: .U....US1.0...U....DigiCert Inc1'0%..U....DigiCert SHA2 Secure S
0080: erver CA0...181128000000Z..201202120000Z0..1.0...U....US1.0...U.
00c0: ...California1.0...U....Los Angeles1<0:..U...3Internet Corporati
0100: on for Assigned Names and Numbers1.0...U....Technology1.0...U...
0140: .www.example.org0.."0...*.H.............0...........t.. r.e..Z]J
0180: .:.f..)+.......q..B....v1u.'.MPju.{S^'....`:..E.kE3\... ..`..LE.
01c0: ...>w....e4.".t..B..!...Dtm.....`.q...cA..4{$IF.....H......E....
0200: ....q.(.X3n..:.jr`..Y.c.zIB{.?......~......P.u... ...ml7.(L.?|.
0240: ....fkSS.....m..d.........-h..?....!d-.i+.x.we.T..........0...0.
0280: ..U.#..0.....a..1a./(..F8.,....0...U......f.b.......6.v....m..0.
02c0: ...U...z0x..www.example.org..example.com..example.edu..example.n
0300: et..example.org..www.example.com..www.example.edu..www.example.n
0340: et0...U...........0...U.%..0...+.........+.......0k..U...d0b0/.-
0380: .+.)http://crl3.digicert.com/ssca-sha2-g6.crl0/.-.+.)http://crl4
03c0: .digicert.com/ssca-sha2-g6.crl0L..U. .E0C07..`.H...l..0*0(..+...
0400: ......https://www.digicert.com/CPS0...g.....0|..+........p0n0$..
0440: +.....0...http://ocsp.digicert.com0F..+.....0..:http://cacerts.d
0480: igicert.com/DigiCertSHA2SecureServerCA.crt0...U.......0.0....+.
04c0: ....y......o...k.i.w.......X......gp.<5.......w.........g\1.F...
0500: ..H0F.!..d..!...H.v.K.F.W'..{.;.JWBl...l.!.....0.d..L|nXSW...EO
0540: ..F..p...BB.v..u..Y|..C._..n.V.GV6.J.`....^......g\1.......G0E. 
0580: o.w.....c-....A.@../.f.._s.H.P..!....H.....D%.<...+.|..'{..X...
05c0: .JN.v.oSv.1.1.....Q..w.......).....7.....g\1.......G0E.!..y.C...
0600: ..O...z....D;.....}..c).-. MwZ.I.J.h..a... .1....q.[.V.=,r.0...*
0640: [email protected].{UH..k.......x...)...i....x........Yz
0680: q..n.....-..=U.3....5..jhfL.V!.[.H..5!.].u.zF,"'.o.:.......&s.].
06c0: 5.j.'...tt.7..;..0.....u!....g.0..S.M............X........._....
0700: ]...]../.X....b{.....irc.e........ [email protected]..=6$O....p
0740: ..'..*.u............0...0..|...........n.u..C.rK...0...*.H......
0780: ..0a1.0...U....US1.0...U....DigiCert Inc1.0...U....www.digicert.
07c0: com1 0...U....DigiCert Global Root CA0...130308120000Z..23030812
0800: 0000Z0M1.0...U....US1.0...U....DigiCert Inc1'0%..U....DigiCert S
0840: HA2 Secure Server CA0.."0...*.H.............0..........X.M..0..5
0880: [n<...,\....qC.d%.....M.f..sn..6.d.7...A.......sM.3.....S.+..uH-
08c0: .V7{.12.......]K.GF.*....y.....j.;......eN....z..\~U1..9..../..j
0900: ..._.WtS;5....D.........k).D.KX.m.K...s...H..Eu..71....T.;..?9^.
0940: ..\~...A..EfG..e...).N.......r.0......w..[(W......EX.........Z0.
0980: .V0...U.......0.......0...U...........04..+........(0&0$..+.....
09c0: 0...http://ocsp.digicert.com0{..U...t0r07.5.3.1http://crl3.digic
0a00: ert.com/DigiCertGlobalRootCA.crl07.5.3.1http://crl4.digicert.com
0a40: /DigiCertGlobalRootCA.crl0=..U. .60402..U. .0*0(..+.........http
0a80: s://www.digicert.com/CPS0...U........a..1a./(..F8.,....0...U.#..
0ac0: 0.....P5V.L.f........=.U0...*.H.............#>.K.1B..~B\.D.i.h.]
0b00: K..!lK.m......S...*e.9O...n\..$&.........MJ..B.x....m.!.S6`Lv..8
0b40: ..Q......M'd...>q.H..3m..........\[email protected]. ..n.....}.
0b80: Y....o-...._&.,.~.7...9......4..!h..s..2.8%.U....h...A4.|.P..:..
0bc0: .......X%..w.-n.R..t..I..;.4(.x.......m..\2......0...0..........
0c00: .;.V.BF..uj.Y..J0...*.H........0a1.0...U....US1.0...U....DigiCer
0c40: t Inc1.0...U....www.digicert.com1 0...U....DigiCert Global Root 
0c80: CA0...061110000000Z..311110000000Z0a1.0...U....US1.0...U....Digi
0cc0: Cert Inc1.0...U....www.digicert.com1 0...U....DigiCert Global Ro
0d00: ot CA0.."0...*.H.............0.........;..r.....W.P...w........[
0d40: .. ....N0.S.C.i.W..."...@..........;qF..f...v'..{...}..H.....z.9
0d80: .eJ].......(.tzx.Y.hn\#2K.N..Zm.p.w........D.X2.u.....G.'j..3.
0dc0: I.`.._.:....JL}>.O_lv^.K7...".m....j.....d..[).2......B...A2....
0e00: .....X?...I(.p.1......L.N...J=^.....'......c0a0...U...........0
0e40: ...U.......0....0...U........P5V.L.f........=.U0...U.#..0.....P5
0e80: V.L.f........=.U0...*.H...............7.H.....D.OR......yy..$..K
0ec0: +..-.......X..m.zt..)....p...L.....p........c...`....[.....S..c.
0f00: ?...f.bf..nA..-..wJ..X.+Y.@#.-(.E>yT.&...H.7...y`......n.D.8/I..
0f40: E>*.6S.:P.....WIla!....x<,:.k..........8l..l..d.w%W0..$.....G|..
0f80: $..0.-...E.P............4_..<.....m...
=> Send SSL data, 0000000005 bytes (0x00000005)
0000: .....
== Info: TLSv1.3 (OUT), TLS alert, unknown CA (560):
=> Send SSL data, 0000000002 bytes (0x00000002)
0000: .0
== Info: SSL certificate problem: self signed certificate in certificate chain
== Info: Closing connection 0
curl_easy_perform() failed: SSL peer certificate or SSH remote key was not OK
Hello World!
Press <RETURN> to close this window...

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章