master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24/* <DESC>
25 * Set your system time from a remote HTTP server's Date: header.
26 * </DESC>
27 */
28/* This example code only builds as-is on Windows.
29 *
30 * Synchronising your computer clock via Internet time server usually relies
31 * on DAYTIME, TIME, or NTP protocols. These protocols provide good accurate
32 * time synchronization but it does not work well through a firewall/proxy.
33 * Some adjustment has to be made to the firewall/proxy for these protocols to
34 * work properly.
35 *
36 * There is an indirect method. Since most webserver provide server time in
37 * their HTTP header, therefore you could synchronise your computer clock
38 * using HTTP protocol which has no problem with firewall/proxy.
39 *
40 * For this software to work, you should take note of these items.
41 * 1. Your firewall/proxy must allow your computer to surf Internet.
42 * 2. Webserver system time must in sync with the NTP time server,
43 * or at least provide an accurate time keeping.
44 * 3. Webserver HTTP header does not provide the milliseconds units,
45 * so there is no way to get an accurate time.
46 * 4. This software could only provide an accuracy of +- a few seconds,
47 * as Round-Trip delay time is not taken into consideration.
48 * Compensation of network, firewall/proxy delay cannot be done by dividing
49 * the Round-Trip delay time by half.
50 * 5. Win32 SetSystemTime() API sets your computer clock according to
51 * GMT/UTC time. Therefore your computer timezone must be properly set.
52 * 6. Webserver data should not be cached by the proxy server. Some
53 * webserver provide Cache-Control to prevent caching.
54 *
55 * Usage:
56 * This software synchronises your computer clock only when you issue
57 * it with --synctime. By default, it only display the webserver's clock.
58 */
59#ifdef _MSC_VER
60#ifndef _CRT_SECURE_NO_WARNINGS
61#define _CRT_SECURE_NO_WARNINGS /* for _snprintf(), fopen(), gmtime(),
62 localtime(), sscanf() */
63#endif
64#endif
66#include <stdio.h>
67#include <string.h>
68#include <time.h>
70#include <curl/curl.h>
72#ifdef _WIN32
73#if (defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0602)) || \
74 defined(WINAPI_FAMILY)
75# include <winapifamily.h>
76# if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && \
77 !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
78# define CURL_WINDOWS_UWP
79# endif
80#include <windows.h>
81#endif
82#endif
84#if defined(_MSC_VER) && (_MSC_VER < 1900)
85#define snprintf _snprintf
86#endif
88#define SYNCTIME_UA "synctime/1.0"
90struct conf {
91 char http_proxy[256];
92 char proxy_user[256];
93 char timeserver[256];
94};
96static int ShowAllHeader;
97static int AutoSyncTime;
98#if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
99static SYSTEMTIME SYSTime;
100static SYSTEMTIME LOCALTime;
102static const char * const DayStr[] = {
103 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
104};
105static const char * const MthStr[] = {
106 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
107 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
108};
109#endif
111static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *stream)
112{
113 fwrite(ptr, size, nmemb, stream);
114 return nmemb * size;
115}
117/* Remember: do not assume headers are passed on null-terminated! */
118static size_t SyncTime_CURL_WriteHeader(void *ptr, size_t size, size_t nmemb,
119 void *stream)
120{
121 (void)stream;
123 if(ShowAllHeader == 1)
124 fprintf(stderr, "%.*s", (int)nmemb, (char *)ptr);
126 if((nmemb >= 5) && !strncmp((const char *)ptr, "Date:", 5)) {
127 if(ShowAllHeader == 0)
128 fprintf(stderr, "HTTP Server. %.*s", (int)nmemb, (char *)ptr);
130 if(AutoSyncTime == 1) {
131#if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
132 char TmpStr1[26], TmpStr2[26];
133 int RetVal = 0;
134 char *field = ptr;
135 *TmpStr1 = 0;
136 *TmpStr2 = 0;
137 if(nmemb && (field[nmemb - 1] == '\n')) {
138 char header[100];
139 size_t len = nmemb < sizeof(header) ? nmemb : sizeof(header) - 1;
140 memcpy(header, field, len);
141 header[len] = 0; /* null-terminate local copy */
142 RetVal = sscanf(header, "Date: %25s %hu %25s %hu %hu:%hu:%hu",
143 TmpStr1, &SYSTime.wDay, TmpStr2, &SYSTime.wYear,
144 &SYSTime.wHour, &SYSTime.wMinute,
145 &SYSTime.wSecond);
146 }
148 if(RetVal == 7) {
149 int i;
150 SYSTime.wMilliseconds = 500; /* adjust to midpoint, 0.5 sec */
151 for(i = 0; i < 12; i++) {
152 if(!strcmp(MthStr[i], TmpStr2)) {
153 SYSTime.wMonth = (WORD)(i + 1);
154 break;
155 }
156 }
157 AutoSyncTime = 3; /* Computer clock is adjusted */
158 }
159 else {
160 AutoSyncTime = 0; /* Error in sscanf() fields conversion */
161 }
162#endif
163 }
164 }
166 if((nmemb >= 12) && !strncmp((const char *)ptr, "X-Cache: HIT", 12)) {
167 fprintf(stderr, "ERROR: HTTP Server data is cached."
168 " Server Date is no longer valid.\n");
169 AutoSyncTime = 0;
170 }
171 return nmemb * size;
172}
174static void SyncTime_CURL_Init(CURL *curl, const char *proxy_port,
175 const char *proxy_user_password)
176{
177 if(strlen(proxy_port) > 0)
178 curl_easy_setopt(curl, CURLOPT_PROXY, proxy_port);
180 if(strlen(proxy_user_password) > 0)
181 curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxy_user_password);
183 curl_easy_setopt(curl, CURLOPT_USERAGENT, SYNCTIME_UA);
184 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
185 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, SyncTime_CURL_WriteHeader);
186}
188static CURLcode SyncTime_CURL_FetchHead(CURL *curl, const char *URL_Str)
189{
190 CURLcode result;
192 curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
193 curl_easy_setopt(curl, CURLOPT_URL, URL_Str);
195 result = curl_easy_perform(curl);
197 return result; /* CURLE_OK */
198}
200static void showUsage(void)
201{
202 fprintf(stderr, "synctime: Synchronising computer clock with time server"
203 " using HTTP protocol.\n");
204 fprintf(stderr, "Usage : synctime [Option]\n");
205 fprintf(stderr, "Options :\n");
206 fprintf(stderr, " --server=WEBSERVER Use this time server instead"
207 " of default.\n");
208 fprintf(stderr, " --showall Show all HTTP header.\n");
209 fprintf(stderr, " --synctime Synchronising computer clock"
210 " with time server.\n");
211 fprintf(stderr, " --proxy-user=USER[:PASS] Set proxy username and"
212 " password.\n");
213 fprintf(stderr, " --proxy=HOST[:PORT] Use HTTP proxy on given"
214 " port.\n");
215 fprintf(stderr, " --help Print this help.\n");
216 fprintf(stderr, "\n");
217}
219int main(int argc, const char *argv[])
220{
221 CURLcode result;
222 CURL *curl;
223 struct conf conf;
224 int RetValue;
226 ShowAllHeader = 0; /* Do not show HTTP Header */
227 AutoSyncTime = 0; /* Do not synchronise computer clock */
228 RetValue = 0; /* Successful Exit */
230 memset(&conf, 0, sizeof(conf));
232 if(argc > 1) {
233 int OptionIndex = 1;
234 while(OptionIndex < argc) {
235 if(!strncmp(argv[OptionIndex], "--server=", 9))
236 snprintf(conf.timeserver, sizeof(conf.timeserver) - 1, "%s",
237 &argv[OptionIndex][9]);
239 if(!strcmp(argv[OptionIndex], "--showall"))
240 ShowAllHeader = 1;
242 if(!strcmp(argv[OptionIndex], "--synctime"))
243 AutoSyncTime = 1;
245 if(!strncmp(argv[OptionIndex], "--proxy-user=", 13))
246 snprintf(conf.proxy_user, sizeof(conf.proxy_user) - 1, "%s",
247 &argv[OptionIndex][13]);
249 if(!strncmp(argv[OptionIndex], "--proxy=", 8))
250 snprintf(conf.http_proxy, sizeof(conf.http_proxy) - 1, "%s",
251 &argv[OptionIndex][8]);
253 if(!strcmp(argv[OptionIndex], "--help") ||
254 !strcmp(argv[OptionIndex], "/?")) {
255 showUsage();
256 return 0;
257 }
258 OptionIndex++;
259 }
260 }
262 if(*conf.timeserver == 0) /* Use default server for time information */
263 snprintf(conf.timeserver, sizeof(conf.timeserver) - 1, "%s",
264 "https://www.ntp.org/");
266 /* Init CURL before usage */
267 result = curl_global_init(CURL_GLOBAL_ALL);
268 if(result != CURLE_OK)
269 return (int)result;
271 curl = curl_easy_init();
272 if(curl) {
273 struct tm *lt;
274 struct tm *gmt;
275 time_t tt;
276 time_t tt_local;
277 time_t tt_gmt;
278 double tzonediffFloat;
279 int tzonediffWord;
280 char timeBuf[61] = "";
281 char tzoneBuf[16];
283 SyncTime_CURL_Init(curl, conf.http_proxy, conf.proxy_user);
285 /* Calculating time diff between GMT and localtime */
286 tt = time(0);
287 lt = localtime(&tt);
288 tt_local = mktime(lt);
289 gmt = gmtime(&tt);
290 tt_gmt = mktime(gmt);
291 tzonediffFloat = difftime(tt_local, tt_gmt);
292 tzonediffWord = (int)(tzonediffFloat / 3600.0);
294 if(tzonediffWord == (int)(tzonediffFloat / 3600.0))
295 snprintf(tzoneBuf, sizeof(tzoneBuf), "%+03d'00'", tzonediffWord);
296 else
297 snprintf(tzoneBuf, sizeof(tzoneBuf), "%+03d'30'", tzonediffWord);
299#if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
300 /* Get current system time and local time */
301 GetSystemTime(&SYSTime);
302 GetLocalTime(&LOCALTime);
303 snprintf(timeBuf, sizeof(timeBuf) - 1,
304 "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
305 DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
306 MthStr[LOCALTime.wMonth - 1], LOCALTime.wYear, LOCALTime.wHour,
307 LOCALTime.wMinute, LOCALTime.wSecond, LOCALTime.wMilliseconds);
308#endif
310 fprintf(stderr, "Fetch: %s\n\n", conf.timeserver);
311 fprintf(stderr, "Before HTTP. Date: %s%s\n\n", timeBuf, tzoneBuf);
313 /* HTTP HEAD command to the Webserver */
314 SyncTime_CURL_FetchHead(curl, conf.timeserver);
316#if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
317 GetLocalTime(&LOCALTime);
318 snprintf(timeBuf, sizeof(timeBuf) - 1,
319 "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
320 DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
321 MthStr[LOCALTime.wMonth - 1], LOCALTime.wYear, LOCALTime.wHour,
322 LOCALTime.wMinute, LOCALTime.wSecond, LOCALTime.wMilliseconds);
323 fprintf(stderr, "\nAfter HTTP. Date: %s%s\n", timeBuf, tzoneBuf);
325 if(AutoSyncTime == 3) {
326 /* Synchronising computer clock */
327 if(!SetSystemTime(&SYSTime)) { /* Set system time */
328 fprintf(stderr, "ERROR: Unable to set system time.\n");
329 RetValue = 1;
330 }
331 else {
332 /* Successfully re-adjusted computer clock */
333 GetLocalTime(&LOCALTime);
334 snprintf(timeBuf, sizeof(timeBuf) - 1,
335 "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
336 DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
337 MthStr[LOCALTime.wMonth - 1], LOCALTime.wYear,
338 LOCALTime.wHour, LOCALTime.wMinute, LOCALTime.wSecond,
339 LOCALTime.wMilliseconds);
340 fprintf(stderr, "\nNew System's Date: %s%s\n", timeBuf, tzoneBuf);
341 }
342 }
343#endif
345 /* Cleanup before exit */
346 memset(&conf, 0, sizeof(conf));
347 curl_easy_cleanup(curl);
348 }
350 curl_global_cleanup();
352 return RetValue;
353}
Diagnostics4 git calls · 31 ms
CallStatusSizeTime031 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=UPDATING git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=25 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2004.1 KiB · hit25 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=4213 git.response.cache=HIT git.response.duration_ms=25 git.fetch.wants=8e9adf1f3f85fe59b30a4cb44be7a89ed24eeaf3 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=4213 git.pkt.payload.max=4192 git.sideband.band1.bytes=4191 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e25942bde11ae0
- Request
- ae754793…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format