Пример #1
0
/*
 * Sends a formatted response message to Sock with the given
 * status code and content type. The value of ContentType can
 * be NULL if no ContentType is required.
 */
static void httpSendStatusLine(Ssock_Handle ssock, int status, const char * contentType)
{
    char buf[MAXRESPONSESIZE];
    const char * msg;
    int len;

    Log_print1(Diags_ANALYSIS, "sendStatus> %d", (IArg)status);

    if (status < 0 || status > 999) {
        status = 999;
    }

    msg = getStatusMessage(status);

    if (contentType) {
        len = esnprintf(buf, sizeof(buf), "%s %3d %s\r\n%s%s\r\n", HTTP_VER,
                        status, msg, CONTENT_TYPE, contentType);
    }
    else {
        len = esnprintf(buf, sizeof(buf), "%s %3d %s\r\n\r\n", HTTP_VER,
                        status, msg);
    }

    if (len == sizeof(buf)) {
        Log_print1(Diags_WARNING, "sendStatus> Buffer size too small: %d", sizeof(buf));
    }

    Ssock_send(ssock, buf, len, 0);
}
Пример #2
0
void HTTPSrv_sendErrorResponse(Ssock_Handle ssock, int status)
{
    /* send a default response if there is no user callback */
    if (!HTTPSrv_errorResponseHook || !HTTPSrv_errorResponseHook(ssock, status)) {
        char buf[33];
        int len;

        len = esnprintf(buf, sizeof(buf), "Failed: %d %s", status,
                        getStatusMessage(status));
        //Log_print2(Diags_ANALYSIS, "len = %d: %s\n", len, (xdc_IArg)buf);
        HTTPSrv_sendResponse(ssock, status, CONTENT_TYPE_PLAIN, len, buf);
    }
}
Пример #3
0
/*
 * Write out the entiry length tag, and terminates the header
 * with an additional CRLF.
 *
 * Since the header is terminated, this must be the last tag
 * written. Entity data should follow immediately.
 */
static void httpSendEntityLength(Ssock_Handle ssock, int32_t entityLen)
{
    char buf[32]; /* sizeof("Content-length: ") + 10 + 4 + 1]; */
    int len;

    len = esnprintf(buf, sizeof(buf), "%s%d\r\n\r\n", CONTENT_LENGTH, entityLen);
    if (len == sizeof(buf)) {
        Log_print1(Diags_WARNING, "sendEntityLength> Buffer size too small: %d",
                   sizeof(buf));
    }

    Ssock_send(ssock, buf, len, 0);
}
Пример #4
0
int
main(int argc, char *argv[])
{
	struct sigaction act;
	struct timespec start, current, diff, intspec, wait;
	size_t i, len;
	int sflag, ret;
	char status[MAXLEN];
	const char *res;

	sflag = 0;
	ARGBEGIN {
		case 's':
			sflag = 1;
			break;
		default:
			usage();
	} ARGEND

	if (argc) {
		usage();
	}

	memset(&act, 0, sizeof(act));
	act.sa_handler = terminate;
	sigaction(SIGINT,  &act, NULL);
	sigaction(SIGTERM, &act, NULL);

	if (!sflag && !(dpy = XOpenDisplay(NULL))) {
		die("XOpenDisplay: Failed to open display");
	}

	while (!done) {
		if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) {
			die("clock_gettime:");
		}

		status[0] = '\0';
		for (i = len = 0; i < LEN(args); i++) {
			if (!(res = args[i].func(args[i].args))) {
				res = unknown_str;
			}
			if ((ret = esnprintf(status + len, sizeof(status) - len,
			                    args[i].fmt, res)) < 0) {
				break;
			}
			len += ret;
		}

		if (sflag) {
			puts(status);
			fflush(stdout);
			if (ferror(stdout))
				die("puts:");
		} else {
			if (XStoreName(dpy, DefaultRootWindow(dpy), status)
                            < 0) {
				die("XStoreName: Allocation failed");
			}
			XFlush(dpy);
		}

		if (!done) {
			if (clock_gettime(CLOCK_MONOTONIC, &current) < 0) {
				die("clock_gettime:");
			}
			difftimespec(&diff, &current, &start);

			intspec.tv_sec = interval / 1000;
			intspec.tv_nsec = (interval % 1000) * 1E6;
			difftimespec(&wait, &intspec, &diff);

			if (wait.tv_sec >= 0) {
				if (nanosleep(&wait, NULL) < 0 &&
				    errno != EINTR) {
					die("nanosleep:");
				}
			}
		}
	}

	if (!sflag) {
		XStoreName(dpy, DefaultRootWindow(dpy), NULL);
		if (XCloseDisplay(dpy) < 0) {
			die("XCloseDisplay: Failed to close display");
		}
	}

	return 0;
}