2008-07-01 22:57:50 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
2006-04-21 15:09:32 +00:00
|
|
|
* gtkcupsutils.h: Statemachine implementation of POST and GET
|
2007-04-29 03:31:03 +00:00
|
|
|
* cups calls which can be used to create a non-blocking cups API
|
2007-01-03 20:34:00 +00:00
|
|
|
* Copyright (C) 2006, 2007 Red Hat, Inc.
|
2006-04-21 15:09:32 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2006-04-21 15:09:32 +00:00
|
|
|
*/
|
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
#include "config.h"
|
2008-05-28 12:49:11 +00:00
|
|
|
#include <gtk/gtk.h>
|
2007-09-09 20:13:58 +00:00
|
|
|
#include "gtkcupsutils.h"
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2008-07-31 23:56:17 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
typedef void (*GtkCupsRequestStateFunc) (GtkCupsRequest *request);
|
|
|
|
|
|
|
|
static void _connect (GtkCupsRequest *request);
|
|
|
|
static void _post_send (GtkCupsRequest *request);
|
|
|
|
static void _post_write_request (GtkCupsRequest *request);
|
|
|
|
static void _post_write_data (GtkCupsRequest *request);
|
|
|
|
static void _post_check (GtkCupsRequest *request);
|
2009-04-21 12:24:32 +00:00
|
|
|
static void _post_auth (GtkCupsRequest *request);
|
2006-04-21 15:09:32 +00:00
|
|
|
static void _post_read_response (GtkCupsRequest *request);
|
|
|
|
|
|
|
|
static void _get_send (GtkCupsRequest *request);
|
|
|
|
static void _get_check (GtkCupsRequest *request);
|
2009-04-21 12:24:32 +00:00
|
|
|
static void _get_auth (GtkCupsRequest *request);
|
2006-04-21 15:09:32 +00:00
|
|
|
static void _get_read_data (GtkCupsRequest *request);
|
|
|
|
|
|
|
|
struct _GtkCupsResult
|
|
|
|
{
|
|
|
|
gchar *error_msg;
|
|
|
|
ipp_t *ipp_response;
|
2007-03-28 15:53:08 +00:00
|
|
|
GtkCupsErrorType error_type;
|
|
|
|
|
|
|
|
/* some error types like HTTP_ERROR have a status and a code */
|
|
|
|
int error_status;
|
|
|
|
int error_code;
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
guint is_error : 1;
|
|
|
|
guint is_ipp_response : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define _GTK_CUPS_MAX_ATTEMPTS 10
|
|
|
|
#define _GTK_CUPS_MAX_CHUNK_SIZE 8192
|
|
|
|
|
2006-12-11 04:08:42 +00:00
|
|
|
static GtkCupsRequestStateFunc post_states[] = {
|
|
|
|
_connect,
|
|
|
|
_post_send,
|
|
|
|
_post_write_request,
|
|
|
|
_post_write_data,
|
|
|
|
_post_check,
|
2009-04-21 12:24:32 +00:00
|
|
|
_post_auth,
|
2006-12-11 04:08:42 +00:00
|
|
|
_post_read_response
|
|
|
|
};
|
|
|
|
|
|
|
|
static GtkCupsRequestStateFunc get_states[] = {
|
|
|
|
_connect,
|
|
|
|
_get_send,
|
|
|
|
_get_check,
|
2009-04-21 12:24:32 +00:00
|
|
|
_get_auth,
|
2006-12-11 04:08:42 +00:00
|
|
|
_get_read_data
|
|
|
|
};
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2020-05-18 11:41:19 +00:00
|
|
|
static void G_GNUC_PRINTF (5, 6)
|
2007-03-28 15:53:08 +00:00
|
|
|
gtk_cups_result_set_error (GtkCupsResult *result,
|
|
|
|
GtkCupsErrorType error_type,
|
|
|
|
int error_status,
|
2020-05-18 11:41:19 +00:00
|
|
|
int error_code,
|
2007-03-28 15:53:08 +00:00
|
|
|
const char *error_msg,
|
2020-05-18 11:41:19 +00:00
|
|
|
...)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
result->is_ipp_response = FALSE;
|
|
|
|
result->is_error = TRUE;
|
2007-03-28 15:53:08 +00:00
|
|
|
result->error_type = error_type;
|
|
|
|
result->error_status = error_status;
|
|
|
|
result->error_code = error_code;
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
va_start (args, error_msg);
|
|
|
|
result->error_msg = g_strdup_vprintf (error_msg, args);
|
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkCupsRequest *
|
2009-04-21 12:24:32 +00:00
|
|
|
gtk_cups_request_new_with_username (http_t *connection,
|
|
|
|
GtkCupsRequestType req_type,
|
|
|
|
gint operation_id,
|
|
|
|
GIOChannel *data_io,
|
|
|
|
const char *server,
|
|
|
|
const char *resource,
|
|
|
|
const char *username)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
|
|
|
GtkCupsRequest *request;
|
|
|
|
cups_lang_t *language;
|
|
|
|
|
|
|
|
request = g_new0 (GtkCupsRequest, 1);
|
|
|
|
request->result = g_new0 (GtkCupsResult, 1);
|
|
|
|
|
|
|
|
request->result->error_msg = NULL;
|
|
|
|
request->result->ipp_response = NULL;
|
|
|
|
|
|
|
|
request->result->is_error = FALSE;
|
|
|
|
request->result->is_ipp_response = FALSE;
|
|
|
|
|
|
|
|
request->type = req_type;
|
|
|
|
request->state = GTK_CUPS_REQUEST_START;
|
|
|
|
|
2009-04-21 12:24:32 +00:00
|
|
|
request->password_state = GTK_CUPS_PASSWORD_NONE;
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
if (server)
|
|
|
|
request->server = g_strdup (server);
|
|
|
|
else
|
2007-01-03 20:34:00 +00:00
|
|
|
request->server = g_strdup (cupsServer ());
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (resource)
|
|
|
|
request->resource = g_strdup (resource);
|
|
|
|
else
|
|
|
|
request->resource = g_strdup ("/");
|
|
|
|
|
|
|
|
if (connection != NULL)
|
|
|
|
{
|
|
|
|
request->http = connection;
|
|
|
|
request->own_http = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-12 17:00:02 +00:00
|
|
|
request->http = httpConnect2 (request->server, ippPort (),
|
|
|
|
NULL, AF_UNSPEC,
|
|
|
|
cupsEncryption (),
|
|
|
|
1, 30000, NULL);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
if (request->http)
|
|
|
|
httpBlocking (request->http, 0);
|
|
|
|
|
|
|
|
request->own_http = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->last_status = HTTP_CONTINUE;
|
|
|
|
|
|
|
|
request->attempts = 0;
|
2006-06-21 18:16:58 +00:00
|
|
|
request->data_io = data_io;
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2007-01-03 20:34:00 +00:00
|
|
|
request->ipp_request = ippNew ();
|
2012-02-19 23:11:24 +00:00
|
|
|
ippSetOperation (request->ipp_request, operation_id);
|
|
|
|
ippSetRequestId (request->ipp_request, 1);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
language = cupsLangDefault ();
|
|
|
|
|
|
|
|
gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|
|
|
"attributes-charset",
|
|
|
|
NULL, "utf-8");
|
|
|
|
|
|
|
|
gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|
|
|
"attributes-natural-language",
|
|
|
|
NULL, language->language);
|
|
|
|
|
2009-04-21 12:24:32 +00:00
|
|
|
if (username != NULL)
|
|
|
|
gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_NAME,
|
|
|
|
"requesting-user-name",
|
|
|
|
NULL, username);
|
|
|
|
else
|
|
|
|
gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_NAME,
|
|
|
|
"requesting-user-name",
|
|
|
|
NULL, cupsUser ());
|
|
|
|
|
2009-09-24 13:34:56 +00:00
|
|
|
request->auth_info_required = NULL;
|
|
|
|
request->auth_info = NULL;
|
|
|
|
request->need_auth_info = FALSE;
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
cupsLangFree (language);
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
2009-04-21 12:24:32 +00:00
|
|
|
GtkCupsRequest *
|
|
|
|
gtk_cups_request_new (http_t *connection,
|
|
|
|
GtkCupsRequestType req_type,
|
|
|
|
gint operation_id,
|
|
|
|
GIOChannel *data_io,
|
|
|
|
const char *server,
|
|
|
|
const char *resource)
|
|
|
|
{
|
|
|
|
return gtk_cups_request_new_with_username (connection,
|
|
|
|
req_type,
|
|
|
|
operation_id,
|
|
|
|
data_io,
|
|
|
|
server,
|
|
|
|
resource,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
static void
|
|
|
|
gtk_cups_result_free (GtkCupsResult *result)
|
|
|
|
{
|
|
|
|
g_free (result->error_msg);
|
|
|
|
|
|
|
|
if (result->ipp_response)
|
|
|
|
ippDelete (result->ipp_response);
|
|
|
|
|
|
|
|
g_free (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_cups_request_free (GtkCupsRequest *request)
|
|
|
|
{
|
|
|
|
if (request->own_http)
|
2007-01-03 20:34:00 +00:00
|
|
|
{
|
|
|
|
if (request->http)
|
|
|
|
httpClose (request->http);
|
|
|
|
}
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
if (request->ipp_request)
|
|
|
|
ippDelete (request->ipp_request);
|
|
|
|
|
|
|
|
g_free (request->server);
|
|
|
|
g_free (request->resource);
|
2009-04-21 12:24:32 +00:00
|
|
|
if (request->password != NULL)
|
|
|
|
{
|
|
|
|
memset (request->password, 0, strlen (request->password));
|
|
|
|
g_free (request->password);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (request->username);
|
2009-09-24 13:34:56 +00:00
|
|
|
g_strfreev (request->auth_info_required);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
gtk_cups_result_free (request->result);
|
|
|
|
|
|
|
|
g_free (request);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2011-08-06 08:37:21 +00:00
|
|
|
gtk_cups_request_read_write (GtkCupsRequest *request, gboolean connect_only)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2011-08-06 08:37:21 +00:00
|
|
|
if (connect_only && request->state != GTK_CUPS_REQUEST_START)
|
|
|
|
return FALSE;
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2011-08-06 08:37:21 +00:00
|
|
|
do
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2011-08-06 08:37:21 +00:00
|
|
|
if (request->type == GTK_CUPS_POST)
|
|
|
|
post_states[request->state] (request);
|
|
|
|
else if (request->type == GTK_CUPS_GET)
|
|
|
|
get_states[request->state] (request);
|
2007-03-28 15:53:08 +00:00
|
|
|
|
2011-12-14 13:09:48 +00:00
|
|
|
if (gtk_cups_result_is_error (request->result))
|
|
|
|
request->state = GTK_CUPS_REQUEST_DONE;
|
|
|
|
|
2011-08-06 08:37:21 +00:00
|
|
|
if (request->attempts > _GTK_CUPS_MAX_ATTEMPTS &&
|
|
|
|
request->state != GTK_CUPS_REQUEST_DONE)
|
|
|
|
{
|
|
|
|
/* TODO: should add a status or error code for too many failed attempts */
|
|
|
|
gtk_cups_result_set_error (request->result,
|
|
|
|
GTK_CUPS_ERROR_GENERAL,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"Too many failed attempts");
|
|
|
|
|
|
|
|
request->state = GTK_CUPS_REQUEST_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request->state == GTK_CUPS_REQUEST_DONE)
|
|
|
|
{
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
2011-08-06 08:37:21 +00:00
|
|
|
/* We need to recheck using httpCheck if the poll_state is read, because
|
|
|
|
* Cups has an internal read buffer. And if this buffer is filled, we may
|
|
|
|
* never get a poll event again. */
|
|
|
|
while (request->poll_state == GTK_CUPS_HTTP_READ && request->http && httpCheck(request->http));
|
|
|
|
|
|
|
|
return FALSE;
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkCupsPollState
|
|
|
|
gtk_cups_request_get_poll_state (GtkCupsRequest *request)
|
|
|
|
{
|
|
|
|
return request->poll_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GtkCupsResult *
|
|
|
|
gtk_cups_request_get_result (GtkCupsRequest *request)
|
|
|
|
{
|
|
|
|
return request->result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_cups_request_ipp_add_string (GtkCupsRequest *request,
|
2006-12-11 04:08:42 +00:00
|
|
|
ipp_tag_t group,
|
|
|
|
ipp_tag_t tag,
|
|
|
|
const char *name,
|
|
|
|
const char *charset,
|
|
|
|
const char *value)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
|
|
|
ippAddString (request->ipp_request,
|
|
|
|
group,
|
|
|
|
tag,
|
|
|
|
name,
|
|
|
|
charset,
|
|
|
|
value);
|
|
|
|
}
|
|
|
|
|
2006-05-04 13:43:32 +00:00
|
|
|
void
|
2006-12-11 04:08:42 +00:00
|
|
|
gtk_cups_request_ipp_add_strings (GtkCupsRequest *request,
|
|
|
|
ipp_tag_t group,
|
|
|
|
ipp_tag_t tag,
|
|
|
|
const char *name,
|
|
|
|
int num_values,
|
|
|
|
const char *charset,
|
|
|
|
const char *const *values)
|
2006-05-04 13:43:32 +00:00
|
|
|
{
|
|
|
|
ippAddStrings (request->ipp_request,
|
|
|
|
group,
|
|
|
|
tag,
|
|
|
|
name,
|
|
|
|
num_values,
|
|
|
|
charset,
|
|
|
|
values);
|
|
|
|
}
|
|
|
|
|
2009-04-21 12:24:32 +00:00
|
|
|
const char *
|
|
|
|
gtk_cups_request_ipp_get_string (GtkCupsRequest *request,
|
|
|
|
ipp_tag_t tag,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
ipp_attribute_t *attribute = NULL;
|
|
|
|
|
|
|
|
if (request != NULL && request->ipp_request != NULL)
|
|
|
|
attribute = ippFindAttribute (request->ipp_request,
|
|
|
|
name,
|
|
|
|
tag);
|
|
|
|
|
2012-02-19 23:11:24 +00:00
|
|
|
if (attribute != NULL && ippGetCount (attribute) > 0)
|
|
|
|
return ippGetString (attribute, 0, NULL);
|
2009-04-21 12:24:32 +00:00
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-05-04 13:43:32 +00:00
|
|
|
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
ipp_tag_t value_tag;
|
|
|
|
} ipp_option_t;
|
|
|
|
|
2006-12-11 04:08:42 +00:00
|
|
|
static const ipp_option_t ipp_options[] = {
|
|
|
|
{ "blackplot", IPP_TAG_BOOLEAN },
|
|
|
|
{ "brightness", IPP_TAG_INTEGER },
|
|
|
|
{ "columns", IPP_TAG_INTEGER },
|
|
|
|
{ "copies", IPP_TAG_INTEGER },
|
|
|
|
{ "finishings", IPP_TAG_ENUM },
|
|
|
|
{ "fitplot", IPP_TAG_BOOLEAN },
|
|
|
|
{ "gamma", IPP_TAG_INTEGER },
|
|
|
|
{ "hue", IPP_TAG_INTEGER },
|
|
|
|
{ "job-k-limit", IPP_TAG_INTEGER },
|
|
|
|
{ "job-page-limit", IPP_TAG_INTEGER },
|
|
|
|
{ "job-priority", IPP_TAG_INTEGER },
|
|
|
|
{ "job-quota-period", IPP_TAG_INTEGER },
|
|
|
|
{ "landscape", IPP_TAG_BOOLEAN },
|
|
|
|
{ "media", IPP_TAG_KEYWORD },
|
|
|
|
{ "mirror", IPP_TAG_BOOLEAN },
|
|
|
|
{ "natural-scaling", IPP_TAG_INTEGER },
|
|
|
|
{ "number-up", IPP_TAG_INTEGER },
|
|
|
|
{ "orientation-requested", IPP_TAG_ENUM },
|
|
|
|
{ "page-bottom", IPP_TAG_INTEGER },
|
|
|
|
{ "page-left", IPP_TAG_INTEGER },
|
|
|
|
{ "page-ranges", IPP_TAG_RANGE },
|
|
|
|
{ "page-right", IPP_TAG_INTEGER },
|
|
|
|
{ "page-top", IPP_TAG_INTEGER },
|
|
|
|
{ "penwidth", IPP_TAG_INTEGER },
|
|
|
|
{ "ppi", IPP_TAG_INTEGER },
|
|
|
|
{ "prettyprint", IPP_TAG_BOOLEAN },
|
|
|
|
{ "printer-resolution", IPP_TAG_RESOLUTION },
|
|
|
|
{ "print-quality", IPP_TAG_ENUM },
|
|
|
|
{ "saturation", IPP_TAG_INTEGER },
|
|
|
|
{ "scaling", IPP_TAG_INTEGER },
|
|
|
|
{ "sides", IPP_TAG_KEYWORD },
|
2009-05-13 16:28:42 +00:00
|
|
|
{ "wrap", IPP_TAG_BOOLEAN },
|
|
|
|
{ "number-up-layout", IPP_TAG_INTEGER }
|
2006-12-11 04:08:42 +00:00
|
|
|
};
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
static ipp_tag_t
|
|
|
|
_find_option_tag (const gchar *option)
|
|
|
|
{
|
|
|
|
int lower_bound, upper_bound, num_options;
|
|
|
|
int current_option;
|
|
|
|
ipp_tag_t result;
|
|
|
|
|
|
|
|
result = IPP_TAG_ZERO;
|
|
|
|
|
|
|
|
lower_bound = 0;
|
2007-01-03 20:34:00 +00:00
|
|
|
upper_bound = num_options = (int) G_N_ELEMENTS (ipp_options) - 1;
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int match;
|
|
|
|
current_option = (int) (((upper_bound - lower_bound) / 2) + lower_bound);
|
|
|
|
|
2007-01-03 20:34:00 +00:00
|
|
|
match = strcasecmp (option, ipp_options[current_option].name);
|
2006-04-21 15:09:32 +00:00
|
|
|
if (match == 0)
|
|
|
|
{
|
|
|
|
result = ipp_options[current_option].value_tag;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else if (match < 0)
|
|
|
|
{
|
|
|
|
upper_bound = current_option - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lower_bound = current_option + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (upper_bound == lower_bound && upper_bound == current_option)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if (upper_bound < 0)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if (lower_bound > num_options)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if (upper_bound < lower_bound)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-01 23:47:45 +00:00
|
|
|
/*
|
|
|
|
* Note that this function uses IPP_TAG_JOB, so it is
|
|
|
|
* only suitable for IPP Group 2 attributes.
|
|
|
|
* See RFC 2911.
|
|
|
|
*/
|
2006-04-21 15:09:32 +00:00
|
|
|
void
|
|
|
|
gtk_cups_request_encode_option (GtkCupsRequest *request,
|
2006-12-11 04:08:42 +00:00
|
|
|
const gchar *option,
|
2007-01-01 23:47:45 +00:00
|
|
|
const gchar *value)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
|
|
|
ipp_tag_t option_tag;
|
|
|
|
|
2007-01-03 20:34:00 +00:00
|
|
|
g_return_if_fail (option != NULL);
|
|
|
|
g_return_if_fail (value != NULL);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
option_tag = _find_option_tag (option);
|
|
|
|
|
|
|
|
if (option_tag == IPP_TAG_ZERO)
|
|
|
|
{
|
|
|
|
option_tag = IPP_TAG_NAME;
|
|
|
|
if (strcasecmp (value, "true") == 0 ||
|
|
|
|
strcasecmp (value, "false") == 0)
|
|
|
|
{
|
|
|
|
option_tag = IPP_TAG_BOOLEAN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 11:41:19 +00:00
|
|
|
switch ((guint)option_tag)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
|
|
|
case IPP_TAG_INTEGER:
|
|
|
|
case IPP_TAG_ENUM:
|
|
|
|
ippAddInteger (request->ipp_request,
|
2007-01-01 23:47:45 +00:00
|
|
|
IPP_TAG_JOB,
|
2006-04-21 15:09:32 +00:00
|
|
|
option_tag,
|
|
|
|
option,
|
|
|
|
strtol (value, NULL, 0));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPP_TAG_BOOLEAN:
|
|
|
|
{
|
|
|
|
char b;
|
2007-01-03 20:34:00 +00:00
|
|
|
|
|
|
|
if (strcasecmp (value, "true") == 0 ||
|
|
|
|
strcasecmp (value, "on") == 0 ||
|
|
|
|
strcasecmp (value, "yes") == 0)
|
2006-04-21 15:09:32 +00:00
|
|
|
b = 1;
|
2007-01-03 20:34:00 +00:00
|
|
|
else
|
|
|
|
b = 0;
|
|
|
|
|
|
|
|
ippAddBoolean (request->ipp_request,
|
|
|
|
IPP_TAG_JOB,
|
|
|
|
option,
|
|
|
|
b);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IPP_TAG_RANGE:
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int lower;
|
|
|
|
int upper;
|
|
|
|
|
|
|
|
if (*value == '-')
|
|
|
|
{
|
|
|
|
lower = 1;
|
|
|
|
s = (char *)value;
|
|
|
|
}
|
|
|
|
else
|
2007-01-03 20:34:00 +00:00
|
|
|
lower = strtol (value, &s, 0);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
if (*s == '-')
|
|
|
|
{
|
|
|
|
if (s[1])
|
2007-01-03 20:34:00 +00:00
|
|
|
upper = strtol (s + 1, NULL, 0);
|
2006-04-21 15:09:32 +00:00
|
|
|
else
|
|
|
|
upper = 2147483647;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
upper = lower;
|
|
|
|
|
|
|
|
ippAddRange (request->ipp_request,
|
2007-01-01 23:47:45 +00:00
|
|
|
IPP_TAG_JOB,
|
2006-04-21 15:09:32 +00:00
|
|
|
option,
|
|
|
|
lower,
|
|
|
|
upper);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IPP_TAG_RESOLUTION:
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int xres;
|
|
|
|
int yres;
|
|
|
|
ipp_res_t units;
|
|
|
|
|
2007-01-03 20:34:00 +00:00
|
|
|
xres = strtol (value, &s, 0);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
if (*s == 'x')
|
2007-01-03 20:34:00 +00:00
|
|
|
yres = strtol (s + 1, &s, 0);
|
2006-04-21 15:09:32 +00:00
|
|
|
else
|
|
|
|
yres = xres;
|
|
|
|
|
2007-01-03 20:34:00 +00:00
|
|
|
if (strcasecmp (s, "dpc") == 0)
|
2006-04-21 15:09:32 +00:00
|
|
|
units = IPP_RES_PER_CM;
|
|
|
|
else
|
|
|
|
units = IPP_RES_PER_INCH;
|
|
|
|
|
|
|
|
ippAddResolution (request->ipp_request,
|
2007-01-01 23:47:45 +00:00
|
|
|
IPP_TAG_JOB,
|
2006-04-21 15:09:32 +00:00
|
|
|
option,
|
|
|
|
units,
|
|
|
|
xres,
|
|
|
|
yres);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2006-12-11 04:08:42 +00:00
|
|
|
{
|
|
|
|
char *values;
|
|
|
|
char *s;
|
|
|
|
int in_quotes;
|
|
|
|
char *next;
|
|
|
|
GPtrArray *strings;
|
|
|
|
|
|
|
|
values = g_strdup (value);
|
|
|
|
strings = NULL;
|
|
|
|
in_quotes = 0;
|
|
|
|
|
|
|
|
for (s = values, next = s; *s != '\0'; s++)
|
|
|
|
{
|
|
|
|
if (in_quotes != 2 && *s == '\'')
|
|
|
|
{
|
|
|
|
/* skip quoted value */
|
|
|
|
if (in_quotes == 0)
|
|
|
|
in_quotes = 1;
|
|
|
|
else
|
|
|
|
in_quotes = 0;
|
|
|
|
}
|
|
|
|
else if (in_quotes != 1 && *s == '\"')
|
|
|
|
{
|
|
|
|
/* skip quoted value */
|
|
|
|
if (in_quotes == 0)
|
|
|
|
in_quotes = 2;
|
|
|
|
else
|
|
|
|
in_quotes = 0;
|
|
|
|
}
|
|
|
|
else if (in_quotes == 0 && *s == ',')
|
|
|
|
{
|
|
|
|
/* found delimiter, add to value array */
|
|
|
|
*s = '\0';
|
|
|
|
if (strings == NULL)
|
|
|
|
strings = g_ptr_array_new ();
|
|
|
|
g_ptr_array_add (strings, next);
|
|
|
|
next = s + 1;
|
|
|
|
}
|
|
|
|
else if (in_quotes == 0 && *s == '\\' && s[1] != '\0')
|
|
|
|
{
|
|
|
|
/* skip escaped character */
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strings == NULL)
|
|
|
|
{
|
|
|
|
/* single value */
|
|
|
|
ippAddString (request->ipp_request,
|
2007-01-01 23:47:45 +00:00
|
|
|
IPP_TAG_JOB,
|
2006-12-11 04:08:42 +00:00
|
|
|
option_tag,
|
|
|
|
option,
|
|
|
|
NULL,
|
|
|
|
value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* multiple values */
|
|
|
|
|
|
|
|
/* add last value */
|
|
|
|
g_ptr_array_add (strings, next);
|
|
|
|
|
|
|
|
ippAddStrings (request->ipp_request,
|
2007-01-01 23:47:45 +00:00
|
|
|
IPP_TAG_JOB,
|
2006-12-11 04:08:42 +00:00
|
|
|
option_tag,
|
|
|
|
option,
|
|
|
|
strings->len,
|
|
|
|
NULL,
|
|
|
|
(const char **) strings->pdata);
|
|
|
|
g_ptr_array_free (strings, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (values);
|
|
|
|
}
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-04 13:29:24 +00:00
|
|
|
void
|
|
|
|
gtk_cups_request_set_ipp_version (GtkCupsRequest *request,
|
|
|
|
gint major,
|
|
|
|
gint minor)
|
|
|
|
{
|
|
|
|
ippSetVersion (request->ipp_request, major, minor);
|
|
|
|
}
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
_connect (GtkCupsRequest *request)
|
|
|
|
{
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
2011-08-06 08:37:21 +00:00
|
|
|
request->bytes_received = 0;
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
if (request->http == NULL)
|
|
|
|
{
|
2019-04-12 17:00:02 +00:00
|
|
|
request->http = httpConnect2 (request->server, ippPort (),
|
|
|
|
NULL, AF_UNSPEC,
|
|
|
|
cupsEncryption (),
|
|
|
|
1, 30000, NULL);
|
2006-04-21 15:09:32 +00:00
|
|
|
if (request->http == NULL)
|
|
|
|
request->attempts++;
|
|
|
|
|
|
|
|
if (request->http)
|
|
|
|
httpBlocking (request->http, 0);
|
|
|
|
|
|
|
|
request->own_http = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request->attempts = 0;
|
|
|
|
request->state++;
|
|
|
|
|
|
|
|
/* we always write to the socket after we get
|
|
|
|
the connection */
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_WRITE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_post_send (GtkCupsRequest *request)
|
|
|
|
{
|
|
|
|
gchar length[255];
|
|
|
|
struct stat data_info;
|
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
GTK_NOTE (PRINTING,
|
|
|
|
g_print ("CUPS Backend: %s\n", G_STRFUNC));
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
request->poll_state = GTK_CUPS_HTTP_WRITE;
|
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
if (request->data_io != NULL)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2006-06-21 18:16:58 +00:00
|
|
|
fstat (g_io_channel_unix_get_fd (request->data_io), &data_info);
|
2007-11-22 06:04:41 +00:00
|
|
|
sprintf (length, "%lu", (unsigned long) (ippLength (request->ipp_request) + data_info.st_size));
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
else
|
2007-01-03 20:34:00 +00:00
|
|
|
sprintf (length, "%lu", (unsigned long) ippLength (request->ipp_request));
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2007-01-03 20:34:00 +00:00
|
|
|
httpClearFields (request->http);
|
|
|
|
httpSetField (request->http, HTTP_FIELD_CONTENT_LENGTH, length);
|
2007-09-07 19:25:13 +00:00
|
|
|
httpSetField (request->http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
|
2007-09-07 18:32:36 +00:00
|
|
|
httpSetField (request->http, HTTP_FIELD_AUTHORIZATION, httpGetAuthString (request->http));
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2007-01-03 20:34:00 +00:00
|
|
|
if (httpPost (request->http, request->resource))
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2019-04-12 17:00:02 +00:00
|
|
|
int res;
|
|
|
|
|
|
|
|
res = httpReconnect2 (request->http, 30000, NULL);
|
|
|
|
|
|
|
|
if (res)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
|
|
|
request->state = GTK_CUPS_POST_DONE;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
|
2007-03-28 15:53:08 +00:00
|
|
|
/* TODO: should add a status or error code for failed post */
|
|
|
|
gtk_cups_result_set_error (request->result,
|
|
|
|
GTK_CUPS_ERROR_GENERAL,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"Failed Post");
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
request->attempts++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->attempts = 0;
|
|
|
|
|
|
|
|
request->state = GTK_CUPS_POST_WRITE_REQUEST;
|
2012-02-19 23:11:24 +00:00
|
|
|
ippSetState (request->ipp_request, IPP_IDLE);
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_post_write_request (GtkCupsRequest *request)
|
|
|
|
{
|
|
|
|
ipp_state_t ipp_status;
|
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
GTK_NOTE (PRINTING,
|
|
|
|
g_print ("CUPS Backend: %s\n", G_STRFUNC));
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
request->poll_state = GTK_CUPS_HTTP_WRITE;
|
|
|
|
|
2007-01-03 20:34:00 +00:00
|
|
|
ipp_status = ippWrite (request->http, request->ipp_request);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
if (ipp_status == IPP_ERROR)
|
|
|
|
{
|
2007-03-28 15:53:08 +00:00
|
|
|
int cups_error = cupsLastError ();
|
2006-04-21 15:09:32 +00:00
|
|
|
request->state = GTK_CUPS_POST_DONE;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
|
2007-01-03 20:34:00 +00:00
|
|
|
gtk_cups_result_set_error (request->result,
|
2007-03-28 15:53:08 +00:00
|
|
|
GTK_CUPS_ERROR_IPP,
|
|
|
|
ipp_status,
|
|
|
|
cups_error,
|
2007-01-03 20:34:00 +00:00
|
|
|
"%s",
|
2007-03-28 15:53:08 +00:00
|
|
|
ippErrorString (cups_error));
|
2006-04-21 15:09:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ipp_status == IPP_DATA)
|
|
|
|
{
|
2006-06-21 18:16:58 +00:00
|
|
|
if (request->data_io != NULL)
|
2006-04-21 15:09:32 +00:00
|
|
|
request->state = GTK_CUPS_POST_WRITE_DATA;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request->state = GTK_CUPS_POST_CHECK;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_READ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_post_write_data (GtkCupsRequest *request)
|
|
|
|
{
|
2006-06-21 18:16:58 +00:00
|
|
|
gsize bytes;
|
2006-04-21 15:09:32 +00:00
|
|
|
char buffer[_GTK_CUPS_MAX_CHUNK_SIZE];
|
|
|
|
http_status_t http_status;
|
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
GTK_NOTE (PRINTING,
|
|
|
|
g_print ("CUPS Backend: %s\n", G_STRFUNC));
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
request->poll_state = GTK_CUPS_HTTP_WRITE;
|
|
|
|
|
|
|
|
if (httpCheck (request->http))
|
2007-01-03 20:34:00 +00:00
|
|
|
http_status = httpUpdate (request->http);
|
2006-04-21 15:09:32 +00:00
|
|
|
else
|
|
|
|
http_status = request->last_status;
|
|
|
|
|
|
|
|
request->last_status = http_status;
|
|
|
|
|
|
|
|
|
|
|
|
if (http_status == HTTP_CONTINUE || http_status == HTTP_OK)
|
|
|
|
{
|
2006-06-21 18:16:58 +00:00
|
|
|
GIOStatus io_status;
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
/* send data */
|
2006-06-21 18:16:58 +00:00
|
|
|
io_status =
|
|
|
|
g_io_channel_read_chars (request->data_io,
|
|
|
|
buffer,
|
|
|
|
_GTK_CUPS_MAX_CHUNK_SIZE,
|
|
|
|
&bytes,
|
|
|
|
&error);
|
|
|
|
|
|
|
|
if (io_status == G_IO_STATUS_ERROR)
|
|
|
|
{
|
|
|
|
request->state = GTK_CUPS_POST_DONE;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
|
2007-03-28 15:53:08 +00:00
|
|
|
gtk_cups_result_set_error (request->result,
|
|
|
|
GTK_CUPS_ERROR_IO,
|
|
|
|
io_status,
|
|
|
|
error->code,
|
2007-01-03 20:34:00 +00:00
|
|
|
"Error reading from cache file: %s",
|
|
|
|
error->message);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
g_error_free (error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (bytes == 0 && io_status == G_IO_STATUS_EOF)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
|
|
|
request->state = GTK_CUPS_POST_CHECK;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_READ;
|
|
|
|
|
|
|
|
request->attempts = 0;
|
|
|
|
return;
|
|
|
|
}
|
2006-06-21 18:16:58 +00:00
|
|
|
|
|
|
|
|
2007-01-03 20:34:00 +00:00
|
|
|
if (httpWrite2 (request->http, buffer, bytes) < bytes)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2007-01-03 20:34:00 +00:00
|
|
|
int http_errno;
|
|
|
|
|
|
|
|
http_errno = httpError (request->http);
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
request->state = GTK_CUPS_POST_DONE;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
|
2007-03-28 15:53:08 +00:00
|
|
|
gtk_cups_result_set_error (request->result,
|
|
|
|
GTK_CUPS_ERROR_HTTP,
|
|
|
|
http_status,
|
|
|
|
http_errno,
|
2007-01-03 20:34:00 +00:00
|
|
|
"Error writing to socket in Post %s",
|
|
|
|
g_strerror (http_errno));
|
2006-04-21 15:09:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2009-04-21 12:24:32 +00:00
|
|
|
else if (http_status == HTTP_UNAUTHORIZED)
|
|
|
|
{
|
|
|
|
request->state = GTK_CUPS_POST_CHECK;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_READ;
|
|
|
|
|
|
|
|
request->attempts = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
|
|
|
request->attempts++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-21 12:24:32 +00:00
|
|
|
static void
|
|
|
|
_post_auth (GtkCupsRequest *request)
|
|
|
|
{
|
|
|
|
if (request->password_state == GTK_CUPS_PASSWORD_HAS)
|
|
|
|
{
|
|
|
|
if (request->password == NULL)
|
|
|
|
{
|
|
|
|
request->state = GTK_CUPS_POST_DONE;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
|
|
|
|
gtk_cups_result_set_error (request->result,
|
|
|
|
GTK_CUPS_ERROR_AUTH,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
"Canceled by user");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
request->state = GTK_CUPS_POST_CHECK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_get_auth (GtkCupsRequest *request)
|
|
|
|
{
|
|
|
|
if (request->password_state == GTK_CUPS_PASSWORD_HAS)
|
|
|
|
{
|
|
|
|
if (request->password == NULL)
|
|
|
|
{
|
|
|
|
request->state = GTK_CUPS_GET_DONE;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
|
|
|
|
gtk_cups_result_set_error (request->result,
|
|
|
|
GTK_CUPS_ERROR_AUTH,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
"Canceled by user");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
request->state = GTK_CUPS_GET_CHECK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Very ugly hack: cups has a stupid synchronous password callback
|
|
|
|
* that doesn't even take the request or user data parameters, so
|
|
|
|
* we have to use a static variable to pass the password to it.
|
|
|
|
* Not threadsafe !
|
|
|
|
* The callback sets cups_password to NULL to signal that the
|
|
|
|
* password has been used.
|
|
|
|
*/
|
2011-12-14 13:09:48 +00:00
|
|
|
static char *cups_password = NULL;
|
|
|
|
static char *cups_username = NULL;
|
2009-04-21 12:24:32 +00:00
|
|
|
|
|
|
|
static const char *
|
|
|
|
passwordCB (const char *prompt)
|
|
|
|
{
|
|
|
|
char *pwd = cups_password;
|
|
|
|
cups_password = NULL;
|
|
|
|
|
|
|
|
cupsSetUser (cups_username);
|
|
|
|
|
|
|
|
return pwd;
|
|
|
|
}
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
static void
|
|
|
|
_post_check (GtkCupsRequest *request)
|
|
|
|
{
|
|
|
|
http_status_t http_status;
|
|
|
|
|
|
|
|
http_status = request->last_status;
|
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
GTK_NOTE (PRINTING,
|
|
|
|
g_print ("CUPS Backend: %s - status %i\n", G_STRFUNC, http_status));
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
request->poll_state = GTK_CUPS_HTTP_READ;
|
|
|
|
|
|
|
|
if (http_status == HTTP_CONTINUE)
|
|
|
|
{
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
else if (http_status == HTTP_UNAUTHORIZED)
|
|
|
|
{
|
2009-04-21 12:24:32 +00:00
|
|
|
int auth_result = -1;
|
|
|
|
httpFlush (request->http);
|
|
|
|
|
|
|
|
if (request->password_state == GTK_CUPS_PASSWORD_APPLIED)
|
|
|
|
{
|
2011-12-14 13:09:48 +00:00
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
2009-04-21 12:24:32 +00:00
|
|
|
request->password_state = GTK_CUPS_PASSWORD_NOT_VALID;
|
|
|
|
request->state = GTK_CUPS_POST_AUTH;
|
|
|
|
request->need_password = TRUE;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Negotiate */
|
|
|
|
if (strncmp (httpGetField (request->http, HTTP_FIELD_WWW_AUTHENTICATE), "Negotiate", 9) == 0)
|
|
|
|
{
|
|
|
|
auth_result = cupsDoAuthentication (request->http, "POST", request->resource);
|
|
|
|
}
|
|
|
|
/* Basic, BasicDigest, Digest and PeerCred */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (request->password_state == GTK_CUPS_PASSWORD_NONE)
|
|
|
|
{
|
|
|
|
cups_username = request->username;
|
|
|
|
cupsSetPasswordCB (passwordCB);
|
|
|
|
|
|
|
|
/* This call success for PeerCred authentication */
|
|
|
|
auth_result = cupsDoAuthentication (request->http, "POST", request->resource);
|
|
|
|
|
|
|
|
if (auth_result != 0)
|
|
|
|
{
|
|
|
|
/* move to AUTH state to let the backend
|
|
|
|
* ask for a password
|
|
|
|
*/
|
2011-12-14 13:09:48 +00:00
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
2009-04-21 12:24:32 +00:00
|
|
|
request->state = GTK_CUPS_POST_AUTH;
|
|
|
|
request->need_password = TRUE;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cups_password = request->password;
|
|
|
|
cups_username = request->username;
|
|
|
|
|
|
|
|
auth_result = cupsDoAuthentication (request->http, "POST", request->resource);
|
|
|
|
|
|
|
|
if (cups_password != NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (request->password != NULL)
|
|
|
|
{
|
|
|
|
memset (request->password, 0, strlen (request->password));
|
|
|
|
g_free (request->password);
|
|
|
|
request->password = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->password_state = GTK_CUPS_PASSWORD_APPLIED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auth_result ||
|
2019-04-12 17:00:02 +00:00
|
|
|
httpReconnect2 (request->http, 30000, NULL))
|
2009-04-21 12:24:32 +00:00
|
|
|
{
|
|
|
|
/* if the password has been used, reset password_state
|
|
|
|
* so that we ask for a new one next time around
|
|
|
|
*/
|
|
|
|
if (cups_password == NULL)
|
|
|
|
request->password_state = GTK_CUPS_PASSWORD_NONE;
|
|
|
|
|
|
|
|
request->state = GTK_CUPS_POST_DONE;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
gtk_cups_result_set_error (request->result,
|
|
|
|
GTK_CUPS_ERROR_AUTH,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"Not authorized");
|
|
|
|
return;
|
|
|
|
}
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2009-04-21 12:24:32 +00:00
|
|
|
if (request->data_io != NULL)
|
|
|
|
g_io_channel_seek_position (request->data_io, 0, G_SEEK_SET, NULL);
|
|
|
|
|
|
|
|
request->state = GTK_CUPS_POST_CONNECT;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_WRITE;
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
else if (http_status == HTTP_ERROR)
|
|
|
|
{
|
2006-12-27 06:50:31 +00:00
|
|
|
int error = httpError (request->http);
|
2006-04-21 15:09:32 +00:00
|
|
|
#ifdef G_OS_WIN32
|
2006-12-27 06:50:31 +00:00
|
|
|
if (error != WSAENETDOWN && error != WSAENETUNREACH)
|
2006-04-21 15:09:32 +00:00
|
|
|
#else
|
2006-12-27 06:50:31 +00:00
|
|
|
if (error != ENETDOWN && error != ENETUNREACH)
|
2006-04-21 15:09:32 +00:00
|
|
|
#endif /* G_OS_WIN32 */
|
|
|
|
{
|
|
|
|
request->attempts++;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request->state = GTK_CUPS_POST_DONE;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
|
2007-03-28 15:53:08 +00:00
|
|
|
gtk_cups_result_set_error (request->result,
|
|
|
|
GTK_CUPS_ERROR_HTTP,
|
|
|
|
http_status,
|
|
|
|
error,
|
|
|
|
"Unknown HTTP error");
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (http_status == HTTP_UPGRADE_REQUIRED)
|
|
|
|
{
|
|
|
|
/* Flush any error message... */
|
|
|
|
httpFlush (request->http);
|
|
|
|
|
2007-06-29 18:41:23 +00:00
|
|
|
cupsSetEncryption (HTTP_ENCRYPT_REQUIRED);
|
|
|
|
request->state = GTK_CUPS_POST_CONNECT;
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
/* Reconnect... */
|
2019-04-12 17:00:02 +00:00
|
|
|
httpReconnect2 (request->http, 30000, NULL);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
/* Upgrade with encryption... */
|
2007-01-03 20:34:00 +00:00
|
|
|
httpEncryption (request->http, HTTP_ENCRYPT_REQUIRED);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
request->attempts++;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
else if (http_status != HTTP_OK)
|
|
|
|
{
|
|
|
|
int http_errno;
|
|
|
|
|
|
|
|
http_errno = httpError (request->http);
|
|
|
|
|
|
|
|
if (http_errno == EPIPE)
|
|
|
|
request->state = GTK_CUPS_POST_CONNECT;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request->state = GTK_CUPS_POST_DONE;
|
2007-03-28 15:53:08 +00:00
|
|
|
gtk_cups_result_set_error (request->result,
|
|
|
|
GTK_CUPS_ERROR_HTTP,
|
|
|
|
http_status,
|
|
|
|
http_errno,
|
2007-01-03 20:34:00 +00:00
|
|
|
"HTTP Error in POST %s",
|
|
|
|
g_strerror (http_errno));
|
2009-04-21 12:24:32 +00:00
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2007-01-03 20:34:00 +00:00
|
|
|
httpFlush (request->http);
|
2006-04-21 15:09:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
request->last_status = HTTP_CONTINUE;
|
2013-12-12 17:11:39 +00:00
|
|
|
|
|
|
|
httpFlush (request->http);
|
|
|
|
if (request->own_http)
|
|
|
|
httpClose (request->http);
|
2006-04-21 15:09:32 +00:00
|
|
|
request->http = NULL;
|
2013-12-12 17:11:39 +00:00
|
|
|
|
|
|
|
return;
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request->state = GTK_CUPS_POST_READ_RESPONSE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
again:
|
|
|
|
http_status = HTTP_CONTINUE;
|
|
|
|
|
|
|
|
if (httpCheck (request->http))
|
|
|
|
http_status = httpUpdate (request->http);
|
|
|
|
|
|
|
|
request->last_status = http_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_post_read_response (GtkCupsRequest *request)
|
|
|
|
{
|
|
|
|
ipp_state_t ipp_status;
|
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
GTK_NOTE (PRINTING,
|
|
|
|
g_print ("CUPS Backend: %s\n", G_STRFUNC));
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
request->poll_state = GTK_CUPS_HTTP_READ;
|
|
|
|
|
|
|
|
if (request->result->ipp_response == NULL)
|
|
|
|
request->result->ipp_response = ippNew();
|
|
|
|
|
|
|
|
ipp_status = ippRead (request->http,
|
|
|
|
request->result->ipp_response);
|
|
|
|
|
|
|
|
if (ipp_status == IPP_ERROR)
|
|
|
|
{
|
2007-03-28 15:53:08 +00:00
|
|
|
int ipp_error = cupsLastError ();
|
|
|
|
gtk_cups_result_set_error (request->result,
|
|
|
|
GTK_CUPS_ERROR_IPP,
|
|
|
|
ipp_status,
|
|
|
|
ipp_error,
|
|
|
|
"%s",
|
|
|
|
ippErrorString (ipp_error));
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
ippDelete (request->result->ipp_response);
|
|
|
|
request->result->ipp_response = NULL;
|
|
|
|
|
|
|
|
request->state = GTK_CUPS_POST_DONE;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
}
|
|
|
|
else if (ipp_status == IPP_DATA)
|
|
|
|
{
|
|
|
|
request->state = GTK_CUPS_POST_DONE;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_get_send (GtkCupsRequest *request)
|
|
|
|
{
|
2006-06-21 18:16:58 +00:00
|
|
|
GTK_NOTE (PRINTING,
|
|
|
|
g_print ("CUPS Backend: %s\n", G_STRFUNC));
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
request->poll_state = GTK_CUPS_HTTP_WRITE;
|
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
if (request->data_io == NULL)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2007-03-28 15:53:08 +00:00
|
|
|
gtk_cups_result_set_error (request->result,
|
|
|
|
GTK_CUPS_ERROR_IO,
|
|
|
|
G_IO_STATUS_ERROR,
|
|
|
|
G_IO_CHANNEL_ERROR_FAILED,
|
2007-01-03 20:34:00 +00:00
|
|
|
"Get requires an open io channel");
|
2007-03-28 15:53:08 +00:00
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
request->state = GTK_CUPS_GET_DONE;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-01-03 20:34:00 +00:00
|
|
|
httpClearFields (request->http);
|
2009-04-21 12:24:32 +00:00
|
|
|
httpSetField (request->http, HTTP_FIELD_AUTHORIZATION, httpGetAuthString (request->http));
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2007-01-03 20:34:00 +00:00
|
|
|
if (httpGet (request->http, request->resource))
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2019-04-12 17:00:02 +00:00
|
|
|
int reconnect;
|
|
|
|
|
|
|
|
reconnect = httpReconnect2 (request->http, 30000, NULL);
|
|
|
|
if (reconnect)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
|
|
|
request->state = GTK_CUPS_GET_DONE;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
2007-03-28 15:53:08 +00:00
|
|
|
|
|
|
|
/* TODO: should add a status or error code for failed GET */
|
|
|
|
gtk_cups_result_set_error (request->result,
|
|
|
|
GTK_CUPS_ERROR_GENERAL,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"Failed Get");
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
request->attempts++;
|
|
|
|
return;
|
|
|
|
}
|
2009-04-21 12:24:32 +00:00
|
|
|
|
|
|
|
if (httpCheck (request->http))
|
|
|
|
request->last_status = httpUpdate (request->http);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
request->attempts = 0;
|
|
|
|
|
|
|
|
request->state = GTK_CUPS_GET_CHECK;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_READ;
|
|
|
|
|
2012-02-19 23:11:24 +00:00
|
|
|
ippSetState (request->ipp_request, IPP_IDLE);
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_get_check (GtkCupsRequest *request)
|
|
|
|
{
|
|
|
|
http_status_t http_status;
|
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
GTK_NOTE (PRINTING,
|
|
|
|
g_print ("CUPS Backend: %s\n", G_STRFUNC));
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
http_status = request->last_status;
|
|
|
|
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_READ;
|
|
|
|
|
|
|
|
if (http_status == HTTP_CONTINUE)
|
|
|
|
{
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
else if (http_status == HTTP_UNAUTHORIZED)
|
|
|
|
{
|
2009-04-21 12:24:32 +00:00
|
|
|
int auth_result = -1;
|
|
|
|
httpFlush (request->http);
|
2007-03-28 15:53:08 +00:00
|
|
|
|
2009-04-21 12:24:32 +00:00
|
|
|
if (request->password_state == GTK_CUPS_PASSWORD_APPLIED)
|
|
|
|
{
|
2011-12-14 13:09:48 +00:00
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
2009-04-21 12:24:32 +00:00
|
|
|
request->password_state = GTK_CUPS_PASSWORD_NOT_VALID;
|
|
|
|
request->state = GTK_CUPS_GET_AUTH;
|
|
|
|
request->need_password = TRUE;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Negotiate */
|
|
|
|
if (strncmp (httpGetField (request->http, HTTP_FIELD_WWW_AUTHENTICATE), "Negotiate", 9) == 0)
|
|
|
|
{
|
|
|
|
auth_result = cupsDoAuthentication (request->http, "GET", request->resource);
|
|
|
|
}
|
|
|
|
/* Basic, BasicDigest, Digest and PeerCred */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (request->password_state == GTK_CUPS_PASSWORD_NONE)
|
|
|
|
{
|
|
|
|
cups_username = request->username;
|
|
|
|
cupsSetPasswordCB (passwordCB);
|
|
|
|
|
|
|
|
/* This call success for PeerCred authentication */
|
|
|
|
auth_result = cupsDoAuthentication (request->http, "GET", request->resource);
|
|
|
|
|
|
|
|
if (auth_result != 0)
|
|
|
|
{
|
|
|
|
/* move to AUTH state to let the backend
|
|
|
|
* ask for a password
|
|
|
|
*/
|
2011-12-14 13:09:48 +00:00
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
2009-04-21 12:24:32 +00:00
|
|
|
request->state = GTK_CUPS_GET_AUTH;
|
|
|
|
request->need_password = TRUE;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cups_password = request->password;
|
|
|
|
cups_username = request->username;
|
|
|
|
|
|
|
|
auth_result = cupsDoAuthentication (request->http, "GET", request->resource);
|
|
|
|
|
|
|
|
if (cups_password != NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (request->password != NULL)
|
|
|
|
{
|
|
|
|
memset (request->password, 0, strlen (request->password));
|
|
|
|
g_free (request->password);
|
|
|
|
request->password = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->password_state = GTK_CUPS_PASSWORD_APPLIED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auth_result ||
|
2019-04-12 17:00:02 +00:00
|
|
|
httpReconnect2 (request->http, 30000, NULL))
|
2009-04-21 12:24:32 +00:00
|
|
|
{
|
|
|
|
/* if the password has been used, reset password_state
|
|
|
|
* so that we ask for a new one next time around
|
|
|
|
*/
|
|
|
|
if (cups_password == NULL)
|
|
|
|
request->password_state = GTK_CUPS_PASSWORD_NONE;
|
|
|
|
|
|
|
|
request->state = GTK_CUPS_GET_DONE;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
gtk_cups_result_set_error (request->result,
|
|
|
|
GTK_CUPS_ERROR_AUTH,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"Not authorized");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-14 13:09:48 +00:00
|
|
|
request->state = GTK_CUPS_GET_CONNECT;
|
2009-04-21 12:24:32 +00:00
|
|
|
request->last_status = HTTP_CONTINUE;
|
|
|
|
|
|
|
|
return;
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
else if (http_status == HTTP_UPGRADE_REQUIRED)
|
|
|
|
{
|
|
|
|
/* Flush any error message... */
|
|
|
|
httpFlush (request->http);
|
|
|
|
|
2007-06-29 18:41:23 +00:00
|
|
|
cupsSetEncryption (HTTP_ENCRYPT_REQUIRED);
|
2009-04-21 12:24:32 +00:00
|
|
|
request->state = GTK_CUPS_GET_CONNECT;
|
2007-06-29 18:41:23 +00:00
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
/* Reconnect... */
|
2019-04-12 17:00:02 +00:00
|
|
|
httpReconnect2 (request->http, 30000, NULL);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
/* Upgrade with encryption... */
|
2007-01-03 20:34:00 +00:00
|
|
|
httpEncryption (request->http, HTTP_ENCRYPT_REQUIRED);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
request->attempts++;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
else if (http_status != HTTP_OK)
|
|
|
|
{
|
|
|
|
int http_errno;
|
|
|
|
|
|
|
|
http_errno = httpError (request->http);
|
|
|
|
|
|
|
|
if (http_errno == EPIPE)
|
|
|
|
request->state = GTK_CUPS_GET_CONNECT;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request->state = GTK_CUPS_GET_DONE;
|
2007-03-28 15:53:08 +00:00
|
|
|
gtk_cups_result_set_error (request->result,
|
|
|
|
GTK_CUPS_ERROR_HTTP,
|
|
|
|
http_status,
|
|
|
|
http_errno,
|
2007-01-03 20:34:00 +00:00
|
|
|
"HTTP Error in GET %s",
|
|
|
|
g_strerror (http_errno));
|
2006-04-21 15:09:32 +00:00
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
2007-01-03 20:34:00 +00:00
|
|
|
httpFlush (request->http);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
request->last_status = HTTP_CONTINUE;
|
2013-12-12 17:11:39 +00:00
|
|
|
|
|
|
|
httpFlush (request->http);
|
|
|
|
if (request->own_http)
|
|
|
|
httpClose (request->http);
|
2006-04-21 15:09:32 +00:00
|
|
|
request->http = NULL;
|
|
|
|
|
2013-12-12 17:11:39 +00:00
|
|
|
return;
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request->state = GTK_CUPS_GET_READ_DATA;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
again:
|
|
|
|
http_status = HTTP_CONTINUE;
|
|
|
|
|
|
|
|
if (httpCheck (request->http))
|
|
|
|
http_status = httpUpdate (request->http);
|
|
|
|
|
|
|
|
request->last_status = http_status;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_get_read_data (GtkCupsRequest *request)
|
|
|
|
{
|
|
|
|
char buffer[_GTK_CUPS_MAX_CHUNK_SIZE];
|
2006-06-21 18:16:58 +00:00
|
|
|
gsize bytes;
|
|
|
|
gsize bytes_written;
|
|
|
|
GIOStatus io_status;
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
GTK_NOTE (PRINTING,
|
|
|
|
g_print ("CUPS Backend: %s\n", G_STRFUNC));
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
request->poll_state = GTK_CUPS_HTTP_READ;
|
2006-06-21 18:16:58 +00:00
|
|
|
|
2007-01-03 20:34:00 +00:00
|
|
|
bytes = httpRead2 (request->http, buffer, sizeof (buffer));
|
2011-08-06 08:37:21 +00:00
|
|
|
request->bytes_received += bytes;
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
GTK_NOTE (PRINTING,
|
2010-09-27 18:48:43 +00:00
|
|
|
g_print ("CUPS Backend: %"G_GSIZE_FORMAT" bytes read\n", bytes));
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
io_status =
|
|
|
|
g_io_channel_write_chars (request->data_io,
|
|
|
|
buffer,
|
|
|
|
bytes,
|
|
|
|
&bytes_written,
|
|
|
|
&error);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
if (io_status == G_IO_STATUS_ERROR)
|
|
|
|
{
|
2009-04-21 12:24:32 +00:00
|
|
|
request->state = GTK_CUPS_GET_DONE;
|
2006-04-21 15:09:32 +00:00
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
2020-05-18 11:41:19 +00:00
|
|
|
|
2007-03-28 15:53:08 +00:00
|
|
|
gtk_cups_result_set_error (request->result,
|
|
|
|
GTK_CUPS_ERROR_IO,
|
|
|
|
io_status,
|
2020-05-18 11:41:19 +00:00
|
|
|
error->code,
|
|
|
|
"%s",
|
2007-03-28 15:53:08 +00:00
|
|
|
error->message);
|
2006-06-21 18:16:58 +00:00
|
|
|
g_error_free (error);
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
2011-08-06 08:37:21 +00:00
|
|
|
|
|
|
|
/* Stop if we do not expect any more data or EOF was received. */
|
|
|
|
if (httpGetLength2 (request->http) <= request->bytes_received || bytes == 0)
|
|
|
|
{
|
|
|
|
request->state = GTK_CUPS_GET_DONE;
|
|
|
|
request->poll_state = GTK_CUPS_HTTP_IDLE;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gtk_cups_request_is_done (GtkCupsRequest *request)
|
|
|
|
{
|
|
|
|
return (request->state == GTK_CUPS_REQUEST_DONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gtk_cups_result_is_error (GtkCupsResult *result)
|
|
|
|
{
|
|
|
|
return result->is_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipp_t *
|
|
|
|
gtk_cups_result_get_response (GtkCupsResult *result)
|
|
|
|
{
|
|
|
|
return result->ipp_response;
|
|
|
|
}
|
|
|
|
|
2007-03-28 15:53:08 +00:00
|
|
|
GtkCupsErrorType
|
|
|
|
gtk_cups_result_get_error_type (GtkCupsResult *result)
|
|
|
|
{
|
|
|
|
return result->error_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
gtk_cups_result_get_error_status (GtkCupsResult *result)
|
|
|
|
{
|
|
|
|
return result->error_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
gtk_cups_result_get_error_code (GtkCupsResult *result)
|
|
|
|
{
|
|
|
|
return result->error_code;
|
|
|
|
}
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
const char *
|
|
|
|
gtk_cups_result_get_error_string (GtkCupsResult *result)
|
|
|
|
{
|
|
|
|
return result->error_msg;
|
|
|
|
}
|
|
|
|
|
2008-07-31 23:56:17 +00:00
|
|
|
/* This function allocates new instance of GtkCupsConnectionTest() and creates
|
|
|
|
* a socket for communication with a CUPS server 'server'.
|
|
|
|
*/
|
|
|
|
GtkCupsConnectionTest *
|
2014-07-11 09:33:30 +00:00
|
|
|
gtk_cups_connection_test_new (const char *server,
|
|
|
|
const int port)
|
2008-07-31 23:56:17 +00:00
|
|
|
{
|
|
|
|
GtkCupsConnectionTest *result = NULL;
|
|
|
|
gchar *port_str = NULL;
|
|
|
|
|
|
|
|
result = g_new (GtkCupsConnectionTest, 1);
|
|
|
|
|
2014-07-11 09:33:30 +00:00
|
|
|
if (port >= 0)
|
|
|
|
port_str = g_strdup_printf ("%d", port);
|
|
|
|
else
|
|
|
|
port_str = g_strdup_printf ("%d", ippPort ());
|
2008-07-31 23:56:17 +00:00
|
|
|
|
|
|
|
if (server != NULL)
|
|
|
|
result->addrlist = httpAddrGetList (server, AF_UNSPEC, port_str);
|
|
|
|
else
|
|
|
|
result->addrlist = httpAddrGetList (cupsServer (), AF_UNSPEC, port_str);
|
|
|
|
|
|
|
|
g_free (port_str);
|
|
|
|
|
|
|
|
result->socket = -1;
|
|
|
|
result->current_addr = NULL;
|
2010-06-03 08:13:17 +00:00
|
|
|
result->last_wrong_addr = NULL;
|
2008-09-17 22:07:10 +00:00
|
|
|
result->at_init = GTK_CUPS_CONNECTION_NOT_AVAILABLE;
|
2008-07-31 23:56:17 +00:00
|
|
|
|
2008-09-17 22:07:10 +00:00
|
|
|
result->at_init = gtk_cups_connection_test_get_state (result);
|
2008-07-31 23:56:17 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* A non-blocking test whether it is possible to connect to a CUPS server specified
|
|
|
|
* inside of GtkCupsConnectionTest structure.
|
|
|
|
* - you need to check it more then once.
|
|
|
|
* The connection is closed after a successful connection.
|
|
|
|
*/
|
2008-09-17 22:07:10 +00:00
|
|
|
GtkCupsConnectionState
|
|
|
|
gtk_cups_connection_test_get_state (GtkCupsConnectionTest *test)
|
2008-07-31 23:56:17 +00:00
|
|
|
{
|
2008-09-17 22:07:10 +00:00
|
|
|
GtkCupsConnectionState result = GTK_CUPS_CONNECTION_NOT_AVAILABLE;
|
|
|
|
http_addrlist_t *iter;
|
|
|
|
gint error_code;
|
|
|
|
gint flags;
|
|
|
|
gint code;
|
2008-07-31 23:56:17 +00:00
|
|
|
|
|
|
|
if (test == NULL)
|
2008-09-17 22:07:10 +00:00
|
|
|
return GTK_CUPS_CONNECTION_NOT_AVAILABLE;
|
2008-07-31 23:56:17 +00:00
|
|
|
|
2008-09-17 22:07:10 +00:00
|
|
|
if (test->at_init == GTK_CUPS_CONNECTION_AVAILABLE)
|
2008-07-31 23:56:17 +00:00
|
|
|
{
|
2008-09-17 22:07:10 +00:00
|
|
|
test->at_init = GTK_CUPS_CONNECTION_NOT_AVAILABLE;
|
|
|
|
return GTK_CUPS_CONNECTION_AVAILABLE;
|
2008-07-31 23:56:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (test->socket == -1)
|
|
|
|
{
|
2010-06-03 08:13:17 +00:00
|
|
|
if (test->last_wrong_addr != NULL && test->last_wrong_addr->next != NULL)
|
|
|
|
iter = test->last_wrong_addr->next;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
test->last_wrong_addr = NULL;
|
|
|
|
iter = test->addrlist;
|
|
|
|
}
|
|
|
|
|
2008-07-31 23:56:17 +00:00
|
|
|
while (iter)
|
|
|
|
{
|
|
|
|
test->socket = socket (iter->addr.addr.sa_family,
|
|
|
|
SOCK_STREAM,
|
|
|
|
0);
|
|
|
|
|
|
|
|
if (test->socket >= 0)
|
|
|
|
{
|
|
|
|
flags = fcntl (test->socket, F_GETFL);
|
|
|
|
|
|
|
|
if (flags != -1)
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
|
|
|
|
fcntl (test->socket, F_SETFL, flags);
|
|
|
|
|
|
|
|
test->current_addr = iter;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
iter = iter->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (test->socket >= 0)
|
|
|
|
{
|
|
|
|
code = connect (test->socket,
|
|
|
|
&test->current_addr->addr.addr,
|
|
|
|
httpAddrLength (&test->current_addr->addr));
|
|
|
|
|
2008-09-17 22:07:10 +00:00
|
|
|
error_code = errno;
|
|
|
|
|
2009-05-19 14:08:21 +00:00
|
|
|
if (code == 0 || error_code == EISCONN)
|
2008-07-31 23:56:17 +00:00
|
|
|
{
|
|
|
|
close (test->socket);
|
|
|
|
test->socket = -1;
|
|
|
|
test->current_addr = NULL;
|
2008-09-17 22:07:10 +00:00
|
|
|
result = GTK_CUPS_CONNECTION_AVAILABLE;
|
2008-07-31 23:56:17 +00:00
|
|
|
}
|
|
|
|
else
|
2008-09-17 22:07:10 +00:00
|
|
|
{
|
|
|
|
if (error_code == EALREADY || error_code == EINPROGRESS)
|
|
|
|
result = GTK_CUPS_CONNECTION_IN_PROGRESS;
|
|
|
|
else
|
2010-06-03 08:13:17 +00:00
|
|
|
{
|
|
|
|
close (test->socket);
|
|
|
|
test->socket = -1;
|
|
|
|
test->last_wrong_addr = test->current_addr;
|
|
|
|
result = GTK_CUPS_CONNECTION_NOT_AVAILABLE;
|
|
|
|
}
|
2008-09-17 22:07:10 +00:00
|
|
|
}
|
2008-07-31 23:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function frees memory used by the GtkCupsConnectionTest structure.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_cups_connection_test_free (GtkCupsConnectionTest *test)
|
|
|
|
{
|
|
|
|
if (test == NULL)
|
2008-08-06 11:59:35 +00:00
|
|
|
return;
|
2008-07-31 23:56:17 +00:00
|
|
|
|
|
|
|
test->current_addr = NULL;
|
2010-06-03 08:13:17 +00:00
|
|
|
test->last_wrong_addr = NULL;
|
2008-07-31 23:56:17 +00:00
|
|
|
httpAddrFreeList (test->addrlist);
|
|
|
|
if (test->socket != -1)
|
|
|
|
{
|
|
|
|
close (test->socket);
|
|
|
|
test->socket = -1;
|
|
|
|
}
|
|
|
|
g_free (test);
|
|
|
|
}
|