]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
Rework setting HTTP errors in CUPS (fixes #893) 1009/head
authorZdenek Dohnal <zdohnal@redhat.com>
Tue, 23 Jul 2024 12:03:30 +0000 (14:03 +0200)
committerZdenek Dohnal <zdohnal@redhat.com>
Tue, 23 Jul 2024 12:03:30 +0000 (14:03 +0200)
Sometimes errno is not set when we want to report HTTP error, so we
should use `http->error` if available or internal server error. In cases
of internal HTTP related errors where we don't have HTTP connection
available (before setting of HTTP connection or in callbacks which
process IPP messages), use `_cupsSetError()`.

Fixes #893

cups/cups-private.h
cups/getdevices.c
cups/getputfile.c
cups/http-addr.c
cups/http.c
cups/ipp.c
cups/json.c
cups/ppd-util.c
cups/request.c

index 7df686dccdf1e387d022ab4621ef02c7c00b877b..0f66e7ce1dd6349ce2a08f9d2cacba79e2d868ac 100644 (file)
@@ -268,7 +268,7 @@ extern const char   *_cupsGSSServiceName(void) _CUPS_PRIVATE;
 extern int             _cupsNextDelay(int current, int *previous) _CUPS_PRIVATE;
 extern void            _cupsSetDefaults(void) _CUPS_INTERNAL;
 extern void            _cupsSetError(ipp_status_t status, const char *message, int localize) _CUPS_PRIVATE;
-extern void            _cupsSetHTTPError(http_status_t status) _CUPS_INTERNAL;
+extern void            _cupsSetHTTPError(http_t *http, http_status_t status) _CUPS_INTERNAL;
 #  ifdef HAVE_GSSAPI
 extern int             _cupsSetNegotiateAuthString(http_t *http, const char *method, const char *resource) _CUPS_PRIVATE;
 #  endif // HAVE_GSSAPI
index 738b983c8de88768181ffca471d9019b9c3e0807..363144e29f150734af03f19d55b37911fd65351c 100644 (file)
@@ -152,7 +152,7 @@ cupsGetDevices(
 
   if (status != HTTP_STATUS_OK)
   {
-    _cupsSetHTTPError(status);
+    _cupsSetHTTPError(http, status);
     return (cupsGetError());
   }
 
index cf263a417a93973f2887f3c31434a67c4ee25ee0..a65f310c24a20a3e36e46eb5655827c03dbe112a 100644 (file)
@@ -193,7 +193,7 @@ cupsGetFd(http_t     *http,         /* I - Connection to server or @code CUPS_HTTP_DEFA
   }
   else
   {
-    _cupsSetHTTPError(status);
+    _cupsSetHTTPError(http, status);
     httpFlush(http);
   }
 
@@ -491,7 +491,7 @@ cupsPutFd(http_t     *http,         /* I - Connection to server or @code CUPS_HTTP_DEFA
 
   if (status != HTTP_STATUS_CREATED)
   {
-    _cupsSetHTTPError(status);
+    _cupsSetHTTPError(http, status);
     httpFlush(http);
   }
 
index 8f5f758855267fc6a5b291f759262f7085a30dac..c61d3699506be6637665dfb4303ba1009c06fbbf 100644 (file)
@@ -207,7 +207,7 @@ httpAddrListen(http_addr_t *addr,   // I - Address to bind to
   // Create the socket and set options...
   if ((fd = socket(addr->addr.sa_family, SOCK_STREAM, 0)) < 0)
   {
-    _cupsSetHTTPError(HTTP_STATUS_ERROR);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
     return (-1);
   }
 
@@ -260,7 +260,7 @@ httpAddrListen(http_addr_t *addr,   // I - Address to bind to
 
   if (status)
   {
-    _cupsSetHTTPError(HTTP_STATUS_ERROR);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
 
     close(fd);
 
@@ -270,7 +270,7 @@ httpAddrListen(http_addr_t *addr,   // I - Address to bind to
   // Listen...
   if (listen(fd, INT_MAX))
   {
-    _cupsSetHTTPError(HTTP_STATUS_ERROR);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
 
     close(fd);
 
index 7ea29d7ede42655adee7c1864164db05a0001226..124530a9dff21af2a7660e7a68c3b265a9c67a9c 100644 (file)
@@ -136,7 +136,7 @@ httpAcceptConnection(int fd,                // I - Listen socket file descriptor
 
   if ((http->fd = accept(fd, (struct sockaddr *)&(http->addrlist->addr), &addrlen)) < 0)
   {
-    _cupsSetHTTPError(HTTP_STATUS_ERROR);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
     httpClose(http);
 
     return (NULL);
index 1f8cf7ed0aa4b66fde3f54f42bb12b0f99fa3aad..752d177cd201bc5fe6caeac97c71d9ac0afe3540 100644 (file)
@@ -2849,7 +2849,7 @@ ippReadIO(void        *src,               // I - Data source
            attr = ipp->current = ipp_add_attr(ipp, NULL, ipp->curtag, IPP_TAG_ZERO, 1);
            if (!attr)
            {
-             _cupsSetHTTPError(HTTP_STATUS_ERROR);
+             _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to allocate IPP attribute."), 1);
              DEBUG_puts("1ippReadIO: unable to allocate attribute.");
              goto rollback;
            }
@@ -2874,7 +2874,7 @@ ippReadIO(void        *src,               // I - Data source
 
            if ((attr = ipp->current = ipp_add_attr(ipp, (char *)buffer, ipp->curtag, tag, 1)) == NULL)
            {
-             _cupsSetHTTPError(HTTP_STATUS_ERROR);
+             _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to allocate IPP attribute."), 1);
              DEBUG_puts("1ippReadIO: unable to allocate attribute.");
              goto rollback;
            }
@@ -3181,7 +3181,7 @@ ippReadIO(void        *src,               // I - Data source
                {
                  if ((value->unknown.data = malloc((size_t)n)) == NULL)
                  {
-                   _cupsSetHTTPError(HTTP_STATUS_ERROR);
+                   _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to allocate IPP attribute."), 1);
                    DEBUG_puts("1ippReadIO: Unable to allocate value");
                    goto rollback;
                  }
@@ -5989,7 +5989,7 @@ ipp_set_value(ipp_t           *ipp,       // IO - IPP message
   // Reallocate memory...
   if ((temp = realloc(temp, sizeof(ipp_attribute_t) + (size_t)(alloc_values - 1) * sizeof(_ipp_value_t))) == NULL)
   {
-    _cupsSetHTTPError(HTTP_STATUS_ERROR);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to reallocate IPP attribute value."), 1);
     DEBUG_puts("4ipp_set_value: Unable to resize attribute.");
     return (NULL);
   }
index f71deb2c8cc893e9a2cc0693d4296d192152b14f..7f751c96481e6e1b0a45097f0ec857b1129f21c2 100644 (file)
@@ -1317,7 +1317,7 @@ cupsJSONImportURL(
   else
   {
     // Save the last HTTP status as a CUPS error...
-    _cupsSetHTTPError(status);
+    _cupsSetHTTPError(http, status);
   }
 
   // Flush any remaining data...
index ca55f97b1a6e44d4f48c65d8e08b8c245fe869f2..b03e6887b653fadde46a0994e910f40835eb97cd 100644 (file)
@@ -433,7 +433,7 @@ cupsGetPPD3(http_t     *http,               /* I  - HTTP connection or @code CUPS_HTTP_DEFAUL
   }
   else if (status != HTTP_STATUS_NOT_MODIFIED)
   {
-    _cupsSetHTTPError(status);
+    _cupsSetHTTPError(http2, status);
 
     if (buffer[0])
       unlink(buffer);
index f34989fc62580078abbd5f3ef251948adf0b890a..c7a15f35d61f62cac091db66ffd9d815bb1a146f 100644 (file)
@@ -248,7 +248,7 @@ cupsDoIORequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
         (status >= HTTP_STATUS_BAD_REQUEST && status != HTTP_STATUS_UNAUTHORIZED &&
         status != HTTP_STATUS_UPGRADE_REQUIRED))
     {
-      _cupsSetHTTPError(status);
+      _cupsSetHTTPError(http, status);
       break;
     }
 
@@ -445,7 +445,7 @@ cupsGetResponse(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
 
     httpFlush(http);
 
-    _cupsSetHTTPError(status);
+    _cupsSetHTTPError(http, status);
 
    /*
     * Then handle encryption and authentication...
@@ -829,7 +829,7 @@ cupsSendRequest(http_t     *http,   /* I - Connection to server or @code CUPS_HTTP
     {
       int temp_status;                 /* Temporary status */
 
-      _cupsSetHTTPError(status);
+      _cupsSetHTTPError(http, status);
 
       do
       {
@@ -985,7 +985,7 @@ cupsWriteRequestData(
       _httpUpdate(http, &status);
       if (status >= HTTP_STATUS_MULTIPLE_CHOICES)
       {
-        _cupsSetHTTPError(status);
+        _cupsSetHTTPError(http, status);
 
        do
        {
@@ -1151,7 +1151,8 @@ _cupsSetError(ipp_status_t status,        /* I - IPP status code */
  */
 
 void
-_cupsSetHTTPError(http_status_t status)        /* I - HTTP status code */
+_cupsSetHTTPError(http_t       *http,  /* I - HTTP connection */
+                 http_status_t status) /* I - HTTP status code */
 {
   switch (status)
   {
@@ -1196,8 +1197,8 @@ _cupsSetHTTPError(http_status_t status)   /* I - HTTP status code */
         break;
 
     case HTTP_STATUS_ERROR :
-       _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
-        break;
+       _cupsSetError(IPP_STATUS_ERROR_INTERNAL, http->error != 0 ? strerror(http->error) : "Internal Server Error", 0);
+       break;
 
     default :
        DEBUG_printf("4_cupsSetHTTPError: HTTP error %d mapped to IPP_STATUS_ERROR_SERVICE_UNAVAILABLE!", status);