]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - test/ippserver.c
Update copyright/license info on test files, and remove old SVN keywords.
[thirdparty/cups.git] / test / ippserver.c
index ef1e5e3797936640971f7d3f81b5be6544f4fdd6..46e689fa7432fd0d2d2a7e7d2c0d34618b6af59e 100644 (file)
@@ -1,17 +1,10 @@
 /*
- * "$Id$"
- *
  * Sample IPP Everywhere server for CUPS.
  *
- * Copyright 2010-2015 by Apple Inc.
- *
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- * which should have been included with this file.  If this file is
- * file is missing or damaged, see the license at "http://www.cups.org/".
+ * Copyright © 2010-2018 by Apple Inc.
  *
- * This file is subject to the Apple OS-Developed Software exception.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more
+ * information.
  */
 
 /*
@@ -463,6 +456,7 @@ static AvahiClient  *DNSSDClient = NULL;
 #endif /* HAVE_DNSSD */
 
 static int             KeepFiles = 0,
+                       MaxVersion = 20,
                        Verbosity = 0;
 
 
@@ -535,6 +529,23 @@ main(int  argc,                            /* I - Number of command-line args */
               pin = 1;
               break;
 
+          case 'V' : /* -V max-version */
+             i ++;
+             if (i >= argc)
+               usage(1);
+
+              if (!strcmp(argv[i], "2.2"))
+                MaxVersion = 22;
+             else if (!strcmp(argv[i], "2.1"))
+                MaxVersion = 21;
+             else if (!strcmp(argv[i], "2.0"))
+                MaxVersion = 20;
+             else if (!strcmp(argv[i], "1.1"))
+                MaxVersion = 11;
+             else
+               usage(1);
+              break;
+
          case 'a' : /* -a attributes-file */
              i ++;
              if (i >= argc)
@@ -680,7 +691,7 @@ main(int  argc,                             /* I - Number of command-line args */
 #ifdef WIN32
     if ((tmpdir = getenv("TEMP")) == NULL)
       tmpdir = "C:/TEMP";
-#elif defined(__APPLE__)
+#elif defined(__APPLE__) && !TARGET_OS_IOS
     if ((tmpdir = getenv("TMPDIR")) == NULL)
       tmpdir = "/private/tmp";
 #else
@@ -1021,7 +1032,7 @@ create_job(_ipp_client_t *client) /* I - Client */
     * Only accept a single job at a time...
     */
 
-    _cupsRWLockWrite(&(client->printer->rwlock));
+    _cupsRWUnlock(&(client->printer->rwlock));
     return (NULL);
   }
 
@@ -1086,7 +1097,10 @@ create_job(_ipp_client_t *client)        /* I - Client */
   ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
   ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, uri);
   ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-uuid", NULL, uuid);
-  ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri", NULL, client->printer->uri);
+  if ((attr = ippFindAttribute(client->request, "printer-uri", IPP_TAG_URI)) != NULL)
+    ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri", NULL, ippGetString(attr, 0, NULL));
+  else
+    ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri", NULL, client->printer->uri);
   ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-creation", (int)(job->created - client->printer->start_time));
 
   cupsArrayAdd(client->printer->jobs, job);
@@ -1323,9 +1337,10 @@ create_printer(const char *servername,   /* I - Server hostname (NULL for default)
   };
   static const char * const versions[] =/* ipp-versions-supported values */
   {
-    "1.0",
     "1.1",
-    "2.0"
+    "2.0",
+    "2.1",
+    "2.2"
   };
   static const char * const features[] =/* ipp-features-supported values */
   {
@@ -1737,7 +1752,12 @@ create_printer(const char *servername,   /* I - Server hostname (NULL for default)
 
   /* ipp-versions-supported */
   if (!ippFindAttribute(printer->attrs, "ipp-versions-supported", IPP_TAG_ZERO))
-    ippAddStrings(printer->attrs, IPP_TAG_PRINTER, IPP_CONST_TAG(IPP_TAG_KEYWORD), "ipp-versions-supported", sizeof(versions) / sizeof(versions[0]), NULL, versions);
+  {
+    int num_versions = MaxVersion == 11 ? 1 : MaxVersion == 20 ? 2 : MaxVersion == 21 ? 3 : 4;
+                                       /* Number of supported versions */
+
+    ippAddStrings(printer->attrs, IPP_TAG_PRINTER, IPP_CONST_TAG(IPP_TAG_KEYWORD), "ipp-versions-supported", num_versions, NULL, versions);
+  }
 
   /* job-account-id-default */
   if (!ippFindAttribute(printer->attrs, "job-account-id-default", IPP_TAG_ZERO))
@@ -1770,9 +1790,6 @@ create_printer(const char *servername,    /* I - Server hostname (NULL for default)
   if (!ippFindAttribute(printer->attrs, "job-password-supported", IPP_TAG_ZERO))
     ippAddInteger(printer->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "job-password-supported", 4);
 
-  /* job-preferred-attributes-supported */
-  ippAddBoolean(printer->attrs, IPP_TAG_PRINTER, "job-preferred-attributes-supported", 0);
-
   /* job-priority-default */
   if (!ippFindAttribute(printer->attrs, "job-priority-default", IPP_TAG_ZERO))
     ippAddInteger(printer->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "job-priority-default", 50);
@@ -1980,6 +1997,9 @@ create_printer(const char *servername,    /* I - Server hostname (NULL for default)
   if (!ippFindAttribute(printer->attrs, "pdl-override-supported", IPP_TAG_ZERO))
     ippAddString(printer->attrs, IPP_TAG_PRINTER, IPP_CONST_TAG(IPP_TAG_KEYWORD), "pdl-override-supported", NULL, "attempted");
 
+  /* preferred-attributes-supported */
+  ippAddBoolean(printer->attrs, IPP_TAG_PRINTER, "preferred-attributes-supported", 0);
+
   /* print-color-mode-default */
   if (!ippFindAttribute(printer->attrs, "print-color-mode-default", IPP_TAG_ZERO))
     ippAddString(printer->attrs, IPP_TAG_PRINTER, IPP_CONST_TAG(IPP_TAG_KEYWORD), "print-color-mode-default", NULL, "auto");
@@ -3681,7 +3701,7 @@ ipp_get_printer_attributes(
       {
         if (printer->state_reasons & bit)
        {
-         snprintf(reason, sizeof(reason), "%s-%s", _ipp_preason_strings[0], printer->state == IPP_PSTATE_IDLE ? "report" : printer->state == IPP_PSTATE_PROCESSING ? "warning" : "error");
+         snprintf(reason, sizeof(reason), "%s-%s", _ipp_preason_strings[i], printer->state == IPP_PSTATE_IDLE ? "report" : printer->state == IPP_PSTATE_PROCESSING ? "warning" : "error");
          if (attr)
            ippSetString(client->response, &attr, ippGetCount(attr), reason);
          else
@@ -3762,6 +3782,7 @@ ipp_print_job(_ipp_client_t *client)      /* I - Client */
                        buffer[4096];   /* Copy buffer */
   ssize_t              bytes;          /* Bytes read */
   cups_array_t         *ra;            /* Attributes to send in response */
+  _cups_thread_t        t;              /* Thread */
 
 
  /*
@@ -3872,7 +3893,13 @@ ipp_print_job(_ipp_client_t *client)     /* I - Client */
   * Process the job...
   */
 
-  if (!_cupsThreadCreate((_cups_thread_func_t)process_job, job))
+  t = _cupsThreadCreate((_cups_thread_func_t)process_job, job);
+
+  if (t)
+  {
+    _cupsThreadDetach(t);
+  }
+  else
   {
     job->state = IPP_JSTATE_ABORTED;
     respond_ipp(client, IPP_STATUS_ERROR_INTERNAL, "Unable to process job.");
@@ -4179,17 +4206,7 @@ ipp_print_uri(_ipp_client_t *client)     /* I - Client */
   * Process the job...
   */
 
-#if 0
-  if (!_cupsThreadCreate((_cups_thread_func_t)process_job, job))
-  {
-    job->state = IPP_JSTATE_ABORTED;
-    respond_ipp(client, IPP_STATUS_ERROR_INTERNAL, "Unable to process job.");
-    return;
-  }
-
-#else
   process_job(job);
-#endif /* 0 */
 
  /*
   * Return the job info...
@@ -4381,17 +4398,7 @@ ipp_send_document(_ipp_client_t *client)/* I - Client */
   * Process the job...
   */
 
-#if 0
-  if (!_cupsThreadCreate((_cups_thread_func_t)process_job, job))
-  {
-    job->state = IPP_JSTATE_ABORTED;
-    respond_ipp(client, IPP_STATUS_ERROR_INTERNAL, "Unable to process job.");
-    return;
-  }
-
-#else
   process_job(job);
-#endif /* 0 */
 
  /*
   * Return the job info...
@@ -4750,17 +4757,7 @@ ipp_send_uri(_ipp_client_t *client)      /* I - Client */
   * Process the job...
   */
 
-#if 0
-  if (!_cupsThreadCreate((_cups_thread_func_t)process_job, job))
-  {
-    job->state = IPP_JSTATE_ABORTED;
-    respond_ipp(client, IPP_STATUS_ERROR_INTERNAL, "Unable to process job.");
-    return;
-  }
-
-#else
   process_job(job);
-#endif /* 0 */
 
  /*
   * Return the job info...
@@ -5006,7 +5003,6 @@ load_attributes(const char *filename,     /* I - File to load */
                break;
 
              ippSetCollection(attrs, &attrptr, ippGetCount(attrptr), col);
-             lastcol = attrptr;
            }
            while (!strcmp(token, "{"));
            break;
@@ -5823,15 +5819,24 @@ process_ipp(_ipp_client_t *client)      /* I - Client */
     * Return an error, since we only support IPP 1.x and 2.x.
     */
 
-    respond_ipp(client, IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED,
-                "Bad request version number %d.%d.", major, minor);
+    respond_ipp(client, IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED, "Bad request version number %d.%d.", major, minor);
+  }
+  else if ((major * 10 + minor) > MaxVersion)
+  {
+    if (httpGetState(client->http) != HTTP_STATE_POST_SEND)
+      httpFlush(client->http);         /* Flush trailing (junk) data */
+
+    respond_http(client, HTTP_STATUS_BAD_REQUEST, NULL, NULL, 0);
+    return (0);
   }
   else if (ippGetRequestId(client->request) <= 0)
-    respond_ipp(client, IPP_STATUS_ERROR_BAD_REQUEST, "Bad request-id %d.",
-                ippGetRequestId(client->request));
+  {
+    respond_ipp(client, IPP_STATUS_ERROR_BAD_REQUEST, "Bad request-id %d.", ippGetRequestId(client->request));
+  }
   else if (!ippFirstAttribute(client->request))
-    respond_ipp(client, IPP_STATUS_ERROR_BAD_REQUEST,
-                "No attributes in request.");
+  {
+    respond_ipp(client, IPP_STATUS_ERROR_BAD_REQUEST, "No attributes in request.");
+  }
   else
   {
    /*
@@ -6828,7 +6833,13 @@ run_printer(_ipp_printer_t *printer)     /* I - Printer */
     {
       if ((client = create_client(printer, printer->ipv4)) != NULL)
       {
-       if (!_cupsThreadCreate((_cups_thread_func_t)process_client, client))
+        _cups_thread_t t = _cupsThreadCreate((_cups_thread_func_t)process_client, client);
+
+        if (t)
+        {
+          _cupsThreadDetach(t);
+        }
+        else
        {
          perror("Unable to create client thread");
          delete_client(client);
@@ -6840,7 +6851,13 @@ run_printer(_ipp_printer_t *printer)     /* I - Printer */
     {
       if ((client = create_client(printer, printer->ipv6)) != NULL)
       {
-       if (!_cupsThreadCreate((_cups_thread_func_t)process_client, client))
+        _cups_thread_t t = _cupsThreadCreate((_cups_thread_func_t)process_client, client);
+
+        if (t)
+        {
+          _cupsThreadDetach(t);
+        }
+        else
        {
          perror("Unable to create client thread");
          delete_client(client);
@@ -6888,8 +6905,7 @@ usage(int status)                 /* O - Exit status */
 {
   if (!status)
   {
-    puts(CUPS_SVERSION " - Copyright 2010-2015 by Apple Inc. All rights "
-         "reserved.");
+    puts(CUPS_SVERSION " - Copyright (c) 2010-2018 by Apple Inc. All rights reserved.");
     puts("");
   }
 
@@ -6899,6 +6915,7 @@ usage(int status)                 /* O - Exit status */
   puts("-2                      Supports 2-sided printing (default=1-sided)");
   puts("-M manufacturer         Manufacturer name (default=Test)");
   puts("-P                      PIN printing mode");
+  puts("-V max-version          Set maximum supported IPP version");
   puts("-a attributes-file      Load printer attributes from file");
   puts("-c command              Run command for every print job");
   printf("-d spool-directory      Spool directory "
@@ -7014,7 +7031,7 @@ valid_doc_attributes(
     attr = ippAddString(client->request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format", NULL, format);
   }
 
-  if (!strcmp(format, "application/octet-stream") && (ippGetOperation(client->request) == IPP_OP_PRINT_JOB || ippGetOperation(client->request) == IPP_OP_SEND_DOCUMENT))
+  if (format && !strcmp(format, "application/octet-stream") && (ippGetOperation(client->request) == IPP_OP_PRINT_JOB || ippGetOperation(client->request) == IPP_OP_SEND_DOCUMENT))
   {
    /*
     * Auto-type the file using the first 8 bytes of the file...
@@ -7385,8 +7402,3 @@ valid_job_attributes(
 
   return (valid);
 }
-
-
-/*
- * End of "$Id$".
- */